askrobots / postblob Public

An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.

0 downloads

postblob/add_ask_endpoint.sql
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/postblob.git
HTTPS
https://gitstable.com/git/askrobots/postblob.git
SSH
ssh://git@gitstable.com:2222/askrobots/postblob.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 docs: Add screenshots to README ae44e69 1 year, 1 month ago History
add_ask_endpoint.sql · 2.0 KB · 36 lines
1-- Add /ask endpoint to handle_request function
2-- This endpoint handles natural language queries
3
4-- First, let's check what we need to add by looking at the current structure
5-- The /ask endpoint should parse the query parameter and call the ask() function
6
7-- Update the handle_request function to add the /ask endpoint
8UPDATE code_files
9SET content = regexp_replace(
10 content,
11 -- Find the stats endpoint and add ask before it
12 '(WHEN p_path = ''/stats'' AND p_method = ''GET'' THEN)',
13 E'WHEN p_path LIKE ''/ask%'' AND p_method = ''GET'' THEN\n -- Natural language query endpoint\n DECLARE\n v_query text;\n BEGIN\n -- Extract query parameter from path (e.g., /ask?q=show+blobs)\n IF p_path ~ ''\\?q='' THEN\n v_query := substring(p_path from ''\\?q=(.*)$'');\n -- URL decode the query\n v_query := replace(v_query, ''+'', '' '');\n v_query := replace(v_query, ''%20'', '' '');\n \n -- Call the ask function\n RETURN jsonb_build_object(\n ''status'', 200,\n ''body'', ask(v_query)\n );\n ELSE\n RETURN jsonb_build_object(\n ''status'', 400,\n ''body'', jsonb_build_object(''error'', ''Missing query parameter'')\n );\n END IF;\n END;\n\n \\1',
14 'g'
15),
16version = version + 1,
17updated_at = now()
18WHERE path = 'functions/handle_request.sql';
19
20-- Hot reload the function (manually since hot_reload has issues)
21\o /dev/null
22SELECT content FROM code_files WHERE path = 'functions/handle_request.sql' \gset
23\o
24
25DROP FUNCTION IF EXISTS handle_request(text, text, jsonb, jsonb, uuid);
26
27DO $$
28DECLARE
29 v_sql text;
30BEGIN
31 SELECT content INTO v_sql FROM code_files WHERE path = 'functions/handle_request.sql';
32 EXECUTE v_sql;
33END $$;
34
35-- Return status
36SELECT 'Added /ask endpoint to handle_request!' as result;