An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.
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 |
| 8 | UPDATE code_files |
| 9 | SET 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 | ), |
| 16 | version = version + 1, |
| 17 | updated_at = now() |
| 18 | WHERE path = 'functions/handle_request.sql'; |
| 19 | |
| 20 | -- Hot reload the function (manually since hot_reload has issues) |
| 21 | \o /dev/null |
| 22 | SELECT content FROM code_files WHERE path = 'functions/handle_request.sql' \gset |
| 23 | \o |
| 24 | |
| 25 | DROP FUNCTION IF EXISTS handle_request(text, text, jsonb, jsonb, uuid); |
| 26 | |
| 27 | DO $$ |
| 28 | DECLARE |
| 29 | v_sql text; |
| 30 | BEGIN |
| 31 | SELECT content INTO v_sql FROM code_files WHERE path = 'functions/handle_request.sql'; |
| 32 | EXECUTE v_sql; |
| 33 | END $$; |
| 34 | |
| 35 | -- Return status |
| 36 | SELECT 'Added /ask endpoint to handle_request!' as result; |