askrobots / postblob Public

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

0 downloads

postblob/debug_routes2.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, 2 months ago History
debug_routes2.sql · 3.1 KB · 106 lines
1-- More debug for 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 RAISE NOTICE 'code_routes called with method=%, path=%', p_method, p_path;
14
15 -- Route: GET /code (Editor UI)
16 IF p_method = 'GET' AND p_path = '/code' THEN
17 SELECT content INTO v_content
18 FROM code_files
19 WHERE path = 'ui/code_editor.html';
20
21 RETURN jsonb_build_object(
22 'status', 200,
23 'headers', jsonb_build_object('Content-Type', 'text/html'),
24 'body', v_content,
25 'html', true
26 );
27
28 -- Route: GET /code/list
29 ELSIF p_method = 'GET' AND p_path = '/code/list' THEN
30 v_crud_result := code_crud('LIST', NULL);
31 RETURN jsonb_build_object(
32 'status', 200,
33 'body', v_crud_result
34 );
35
36 -- Route: GET /code/file
37 ELSIF p_method = 'GET' AND p_path LIKE '/code/file%' THEN
38 -- Better URL parameter parsing - try different patterns
39 v_path := substring(p_path from '/code/file\?path=(.+)$');
40
41 IF v_path IS NULL THEN
42 -- Try without escape
43 v_path := substring(p_path from '/code/file?path=(.+)$');
44 END IF;
45
46 RAISE NOTICE 'Parsed path: %', v_path;
47
48 -- URL decode
49 v_path := replace(v_path, '%2F', '/');
50 v_path := replace(v_path, '%20', ' ');
51 v_path := replace(v_path, '+', ' ');
52
53 RAISE NOTICE 'Decoded path: %', v_path;
54
55 -- Get the file
56 v_crud_result := code_crud('GET', v_path);
57
58 -- Check if we got an error
59 IF v_crud_result ? 'error' THEN
60 RETURN jsonb_build_object(
61 'status', 404,
62 'body', v_crud_result
63 );
64 ELSE
65 RETURN jsonb_build_object(
66 'status', 200,
67 'body', v_crud_result
68 );
69 END IF;
70
71 -- Route: POST /code/save
72 ELSIF p_method = 'POST' AND p_path = '/code/save' THEN
73 v_crud_result := code_crud('PUT', p_body->>'path', p_body->>'content');
74 RETURN jsonb_build_object(
75 'status', 200,
76 'body', v_crud_result
77 );
78
79 -- Route: POST /code/execute
80 ELSIF p_method = 'POST' AND p_path = '/code/execute' THEN
81 v_crud_result := execute_code(
82 COALESCE(p_body->>'language', 'sql'),
83 p_body->>'code',
84 p_body->>'save_as'
85 );
86 RETURN jsonb_build_object(
87 'status', 200,
88 'body', v_crud_result
89 );
90
91 -- Route: POST /code/reload
92 ELSIF p_method = 'POST' AND p_path = '/code/reload' THEN
93 v_crud_result := hot_reload(p_body->>'path');
94 RETURN jsonb_build_object(
95 'status', 200,
96 'body', v_crud_result
97 );
98
99 ELSE
100 RETURN jsonb_build_object(
101 'status', 404,
102 'body', jsonb_build_object('error', 'Route not found', 'path', p_path)
103 );
104 END IF;
105END;
106$$ LANGUAGE plpgsql;