askrobots / postblob Public

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

0 downloads

postblob/debug_code_routes.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
debug_code_routes.sql · 2.7 KB · 95 lines
1-- Add debug output to code_routes
2CREATE OR REPLACE FUNCTION code_routes(
3 p_method text,
4 p_path text,
5 p_body jsonb DEFAULT '{}'::jsonb
6) RETURNS jsonb AS $$
7DECLARE
8 v_result jsonb;
9 v_path text;
10 v_content text;
11 v_crud_result jsonb;
12BEGIN
13 -- Route: GET /code (Editor UI)
14 IF p_method = 'GET' AND p_path = '/code' THEN
15 SELECT content INTO v_content
16 FROM code_files
17 WHERE path = 'ui/code_editor.html';
18
19 RETURN jsonb_build_object(
20 'status', 200,
21 'headers', jsonb_build_object('Content-Type', 'text/html'),
22 'body', v_content,
23 'html', true
24 );
25
26 -- Route: GET /code/list
27 ELSIF p_method = 'GET' AND p_path = '/code/list' THEN
28 v_crud_result := code_crud('LIST', NULL);
29 RETURN jsonb_build_object(
30 'status', 200,
31 'body', v_crud_result
32 );
33
34 -- Route: GET /code/file
35 ELSIF p_method = 'GET' AND p_path LIKE '/code/file%' THEN
36 -- Better URL parameter parsing
37 v_path := substring(p_path from '/code/file\?path=(.+)$');
38
39 -- URL decode
40 v_path := replace(v_path, '%2F', '/');
41 v_path := replace(v_path, '%20', ' ');
42 v_path := replace(v_path, '+', ' ');
43
44 -- Get the file
45 v_crud_result := code_crud('GET', v_path);
46
47 -- Check if we got an error
48 IF v_crud_result ? 'error' THEN
49 RETURN jsonb_build_object(
50 'status', 404,
51 'body', v_crud_result
52 );
53 ELSE
54 RETURN jsonb_build_object(
55 'status', 200,
56 'body', v_crud_result
57 );
58 END IF;
59
60 -- Route: POST /code/save
61 ELSIF p_method = 'POST' AND p_path = '/code/save' THEN
62 v_crud_result := code_crud('PUT', p_body->>'path', p_body->>'content');
63 RETURN jsonb_build_object(
64 'status', 200,
65 'body', v_crud_result
66 );
67
68 -- Route: POST /code/execute
69 ELSIF p_method = 'POST' AND p_path = '/code/execute' THEN
70 v_crud_result := execute_code(
71 COALESCE(p_body->>'language', 'sql'),
72 p_body->>'code',
73 p_body->>'save_as'
74 );
75 RETURN jsonb_build_object(
76 'status', 200,
77 'body', v_crud_result
78 );
79
80 -- Route: POST /code/reload
81 ELSIF p_method = 'POST' AND p_path = '/code/reload' THEN
82 v_crud_result := hot_reload(p_body->>'path');
83 RETURN jsonb_build_object(
84 'status', 200,
85 'body', v_crud_result
86 );
87
88 ELSE
89 RETURN jsonb_build_object(
90 'status', 404,
91 'body', jsonb_build_object('error', 'Route not found', 'path', p_path)
92 );
93 END IF;
94END;
95$$ LANGUAGE plpgsql;