An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.
fix_all.sql · 4.2 KB · 129 lines
| 1 | -- Fix code_routes to handle file loading properly |
| 2 | CREATE OR REPLACE FUNCTION code_routes( |
| 3 | p_method text, |
| 4 | p_path text, |
| 5 | p_body jsonb DEFAULT '{}'::jsonb |
| 6 | ) RETURNS jsonb AS $$ |
| 7 | DECLARE |
| 8 | v_result jsonb; |
| 9 | v_file_path text; |
| 10 | v_content text; |
| 11 | v_crud_result jsonb; |
| 12 | BEGIN |
| 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?path=... |
| 35 | ELSIF p_method = 'GET' AND p_path LIKE '/code/file%' THEN |
| 36 | -- Extract path parameter |
| 37 | IF position('?path=' in p_path) > 0 THEN |
| 38 | v_file_path := substring(p_path from position('?path=' in p_path) + 6); |
| 39 | ELSE |
| 40 | v_file_path := NULL; |
| 41 | END IF; |
| 42 | |
| 43 | -- URL decode |
| 44 | IF v_file_path IS NOT NULL THEN |
| 45 | v_file_path := replace(v_file_path, '%2F', '/'); |
| 46 | v_file_path := replace(v_file_path, '%20', ' '); |
| 47 | v_file_path := replace(v_file_path, '+', ' '); |
| 48 | |
| 49 | -- Remove any trailing parameters |
| 50 | IF position('&' in v_file_path) > 0 THEN |
| 51 | v_file_path := substring(v_file_path from 1 for position('&' in v_file_path) - 1); |
| 52 | END IF; |
| 53 | END IF; |
| 54 | |
| 55 | -- Get the file |
| 56 | v_crud_result := code_crud('GET', v_file_path); |
| 57 | |
| 58 | RETURN jsonb_build_object( |
| 59 | 'status', 200, |
| 60 | 'body', v_crud_result |
| 61 | ); |
| 62 | |
| 63 | -- Route: POST /code/save |
| 64 | ELSIF p_method = 'POST' AND p_path = '/code/save' THEN |
| 65 | v_crud_result := code_crud('PUT', p_body->>'path', p_body->>'content'); |
| 66 | RETURN jsonb_build_object( |
| 67 | 'status', 200, |
| 68 | 'body', v_crud_result |
| 69 | ); |
| 70 | |
| 71 | -- Route: POST /code/execute |
| 72 | ELSIF p_method = 'POST' AND p_path = '/code/execute' THEN |
| 73 | v_crud_result := execute_code( |
| 74 | COALESCE(p_body->>'language', 'sql'), |
| 75 | p_body->>'code', |
| 76 | p_body->>'save_as' |
| 77 | ); |
| 78 | RETURN jsonb_build_object( |
| 79 | 'status', 200, |
| 80 | 'body', v_crud_result |
| 81 | ); |
| 82 | |
| 83 | -- Route: POST /code/reload |
| 84 | ELSIF p_method = 'POST' AND p_path = '/code/reload' THEN |
| 85 | v_crud_result := hot_reload(p_body->>'path'); |
| 86 | RETURN jsonb_build_object( |
| 87 | 'status', 200, |
| 88 | 'body', v_crud_result |
| 89 | ); |
| 90 | |
| 91 | ELSE |
| 92 | -- Return NULL to indicate route not handled |
| 93 | RETURN NULL; |
| 94 | END IF; |
| 95 | END; |
| 96 | $$ LANGUAGE plpgsql; |
| 97 | |
| 98 | -- Update the main page to include /code link |
| 99 | -- Find where API Endpoints header is and add Code Editor section before it |
| 100 | UPDATE code_files |
| 101 | SET content = regexp_replace( |
| 102 | content, |
| 103 | '(<div class="card">\s+<h2>🚀 API Endpoints</h2>)', |
| 104 | E'<div class="card"> |
| 105 | <h2>🔥 Hot Deploy Code Editor</h2> |
| 106 | <div class="endpoint"> |
| 107 | <span class="method GET">GET</span> |
| 108 | <span class="path">/code</span> |
| 109 | <div class="description">Live code editor - Edit functions while the server is running!</div> |
| 110 | </div> |
| 111 | <div style="margin-top: 20px;"> |
| 112 | <a href="/code" style="display: inline-block; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 12px 24px; border-radius: 6px; text-decoration: none; font-weight: bold;">Open Code Editor</a> |
| 113 | </div> |
| 114 | </div> |
| 115 | |
| 116 | \\1', |
| 117 | 'g' |
| 118 | ) |
| 119 | WHERE path = 'ui/code_editor.html' |
| 120 | RETURNING 'Updated editor' as result; |
| 121 | |
| 122 | -- Also update the main handle_request function to add the code editor link |
| 123 | -- This updates the home page HTML |
| 124 | DO $$ |
| 125 | BEGIN |
| 126 | -- We need to manually update the handle_request function |
| 127 | -- Let's create a simpler approach |
| 128 | RAISE NOTICE 'Homepage link will need to be added in handle_request function'; |
| 129 | END $$; |