An experimental web framework where PostgreSQL does everything: routing, HTML, file storage, jobs and sessions.
server.py · 12.7 KB · 373 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """ |
| 3 | PostBlob Server - Minimal Python wrapper for PostgreSQL-powered application |
| 4 | """ |
| 5 | |
| 6 | import json |
| 7 | import psycopg2 |
| 8 | from psycopg2.extras import RealDictCursor |
| 9 | from http.server import HTTPServer, BaseHTTPRequestHandler |
| 10 | import base64 |
| 11 | import sys |
| 12 | import select |
| 13 | import argparse |
| 14 | import os |
| 15 | import atexit |
| 16 | from urllib.parse import urlparse, parse_qs |
| 17 | |
| 18 | # Database connection |
| 19 | DATABASE_URL = "postgresql://localhost/postblob" |
| 20 | |
| 21 | class PostBlobHandler(BaseHTTPRequestHandler): |
| 22 | """HTTP request handler that delegates everything to PostgreSQL""" |
| 23 | |
| 24 | def setup(self): |
| 25 | super().setup() |
| 26 | self.conn = psycopg2.connect(DATABASE_URL) |
| 27 | self.conn.autocommit = True |
| 28 | |
| 29 | def finish(self): |
| 30 | self.conn.close() |
| 31 | super().finish() |
| 32 | |
| 33 | def do_GET(self): |
| 34 | self._handle_request('GET') |
| 35 | |
| 36 | def do_POST(self): |
| 37 | self._handle_request('POST') |
| 38 | |
| 39 | def do_PUT(self): |
| 40 | self._handle_request('PUT') |
| 41 | |
| 42 | def do_DELETE(self): |
| 43 | self._handle_request('DELETE') |
| 44 | |
| 45 | def _handle_request(self, method): |
| 46 | """Route all requests through PostgreSQL""" |
| 47 | try: |
| 48 | # Parse request |
| 49 | parsed_path = urlparse(self.path) |
| 50 | path = parsed_path.path |
| 51 | query_params = parse_qs(parsed_path.query) |
| 52 | |
| 53 | # For code and ask routes, we need the full path with query string |
| 54 | if path.startswith('/code') or path.startswith('/ask'): |
| 55 | path = self.path |
| 56 | |
| 57 | # Get request body if present |
| 58 | body = {} |
| 59 | if method in ['POST', 'PUT']: |
| 60 | content_length = int(self.headers.get('Content-Length', 0)) |
| 61 | if content_length > 0: |
| 62 | raw_body = self.rfile.read(content_length) |
| 63 | try: |
| 64 | body = json.loads(raw_body.decode('utf-8')) |
| 65 | except json.JSONDecodeError: |
| 66 | # Handle binary uploads |
| 67 | body = { |
| 68 | 'data': base64.b64encode(raw_body).decode('utf-8'), |
| 69 | 'filename': self.headers.get('X-Filename', 'upload'), |
| 70 | 'mimetype': self.headers.get('Content-Type', 'application/octet-stream') |
| 71 | } |
| 72 | |
| 73 | # Get headers |
| 74 | headers = {key: value for key, value in self.headers.items()} |
| 75 | |
| 76 | # Get session ID from cookie if present |
| 77 | session_id = None |
| 78 | if 'Cookie' in headers: |
| 79 | for cookie in headers['Cookie'].split(';'): |
| 80 | if cookie.strip().startswith('session_id='): |
| 81 | session_id = cookie.split('=')[1] |
| 82 | |
| 83 | # Try code routes first |
| 84 | with self.conn.cursor(cursor_factory=RealDictCursor) as cur: |
| 85 | # Check if it's a code route |
| 86 | if path.startswith('/code'): |
| 87 | with open('/tmp/debug.log', 'a') as f: |
| 88 | f.write(f"DEBUG: Calling code_routes with path={path}\n") |
| 89 | |
| 90 | cur.execute( |
| 91 | "SELECT code_routes(%s, %s, %s) as response", |
| 92 | (method, path, json.dumps(body)) |
| 93 | ) |
| 94 | result = cur.fetchone() |
| 95 | |
| 96 | with open('/tmp/debug.log', 'a') as f: |
| 97 | if result: |
| 98 | raw_response = result['response'] |
| 99 | f.write(f"DEBUG: Got result, type: {type(raw_response)}\n") |
| 100 | f.write(f"DEBUG: Is None: {raw_response is None}\n") |
| 101 | if raw_response is not None: |
| 102 | response = raw_response |
| 103 | f.write(f"DEBUG: Using code_routes response\n") |
| 104 | else: |
| 105 | response = None |
| 106 | f.write(f"DEBUG: code_routes returned None\n") |
| 107 | else: |
| 108 | response = None |
| 109 | f.write(f"DEBUG: No result from query\n") |
| 110 | |
| 111 | if response is None: |
| 112 | with open('/tmp/debug.log', 'a') as f: |
| 113 | f.write(f"DEBUG: Falling back to handle_request\n") |
| 114 | # Fall back to main handler |
| 115 | cur.execute( |
| 116 | "SELECT handle_request(%s, %s, %s, %s, %s) as response", |
| 117 | (method, path, json.dumps(headers), json.dumps(body), session_id) |
| 118 | ) |
| 119 | response = cur.fetchone()['response'] |
| 120 | else: |
| 121 | # Call main PostgreSQL function |
| 122 | cur.execute( |
| 123 | "SELECT handle_request(%s, %s, %s, %s, %s) as response", |
| 124 | (method, path, json.dumps(headers), json.dumps(body), session_id) |
| 125 | ) |
| 126 | response = cur.fetchone()['response'] |
| 127 | |
| 128 | # Parse response |
| 129 | if isinstance(response, str): |
| 130 | response = json.loads(response) |
| 131 | |
| 132 | # Send HTTP response |
| 133 | status = response.get('status', 200) |
| 134 | self.send_response(status) |
| 135 | |
| 136 | # Set response headers |
| 137 | response_headers = response.get('headers', {}) |
| 138 | for key, value in response_headers.items(): |
| 139 | self.send_header(key, value) |
| 140 | |
| 141 | # Default headers |
| 142 | if 'Content-Type' not in response_headers: |
| 143 | self.send_header('Content-Type', 'application/json') |
| 144 | |
| 145 | self.end_headers() |
| 146 | |
| 147 | # Send body |
| 148 | body = response.get('body', {}) |
| 149 | if response.get('html'): |
| 150 | # HTML response |
| 151 | self.wfile.write(body.encode('utf-8')) |
| 152 | elif response.get('binary'): |
| 153 | # Binary response (e.g., file download) |
| 154 | self.wfile.write(base64.b64decode(body)) |
| 155 | else: |
| 156 | # JSON response |
| 157 | self.wfile.write(json.dumps(body).encode('utf-8')) |
| 158 | |
| 159 | except Exception as e: |
| 160 | # Error response |
| 161 | self.send_response(500) |
| 162 | self.send_header('Content-Type', 'application/json') |
| 163 | self.end_headers() |
| 164 | error_response = { |
| 165 | 'error': str(e), |
| 166 | 'type': type(e).__name__ |
| 167 | } |
| 168 | self.wfile.write(json.dumps(error_response).encode('utf-8')) |
| 169 | |
| 170 | def log_message(self, format, *args): |
| 171 | """Custom log format""" |
| 172 | print(f"[{self.log_date_time_string()}] {format % args}") |
| 173 | |
| 174 | |
| 175 | def run_server(port=8000): |
| 176 | """Run the HTTP server""" |
| 177 | # Write PID file |
| 178 | pid_file = f"postblob_{port}.pid" |
| 179 | with open(pid_file, 'w') as f: |
| 180 | f.write(str(os.getpid())) |
| 181 | |
| 182 | # Register cleanup |
| 183 | def cleanup(): |
| 184 | if os.path.exists(pid_file): |
| 185 | os.remove(pid_file) |
| 186 | atexit.register(cleanup) |
| 187 | |
| 188 | server_address = ('', port) |
| 189 | httpd = HTTPServer(server_address, PostBlobHandler) |
| 190 | print(f"PostBlob server running on http://localhost:{port}") |
| 191 | print(f"PID: {os.getpid()} (written to {pid_file})") |
| 192 | print("Press Ctrl+C to stop") |
| 193 | |
| 194 | try: |
| 195 | httpd.serve_forever() |
| 196 | except KeyboardInterrupt: |
| 197 | print("\nShutting down server...") |
| 198 | httpd.shutdown() |
| 199 | cleanup() |
| 200 | |
| 201 | |
| 202 | def run_worker(): |
| 203 | """Run the background job worker""" |
| 204 | # Write PID file for worker |
| 205 | pid_file = "postblob_worker.pid" |
| 206 | with open(pid_file, 'w') as f: |
| 207 | f.write(str(os.getpid())) |
| 208 | |
| 209 | # Register cleanup |
| 210 | def cleanup(): |
| 211 | if os.path.exists(pid_file): |
| 212 | os.remove(pid_file) |
| 213 | atexit.register(cleanup) |
| 214 | |
| 215 | conn = psycopg2.connect(DATABASE_URL) |
| 216 | conn.autocommit = True |
| 217 | cur = conn.cursor() |
| 218 | |
| 219 | # Listen for job notifications |
| 220 | cur.execute("LISTEN new_job") |
| 221 | print("PostBlob job worker started...") |
| 222 | print(f"PID: {os.getpid()} (written to {pid_file})") |
| 223 | print("Listening for new jobs...") |
| 224 | |
| 225 | while True: |
| 226 | try: |
| 227 | # Check for notifications with timeout |
| 228 | if select.select([conn], [], [], 5) == ([], [], []): |
| 229 | # Timeout - check for any pending jobs |
| 230 | cur.execute(""" |
| 231 | SELECT id FROM jobs |
| 232 | WHERE status = 'pending' |
| 233 | ORDER BY created_at |
| 234 | LIMIT 1 |
| 235 | """) |
| 236 | job = cur.fetchone() |
| 237 | if job: |
| 238 | print(f"Processing pending job {job[0]}") |
| 239 | cur.execute("SELECT process_job(%s)", (job[0],)) |
| 240 | else: |
| 241 | # Got notification |
| 242 | conn.poll() |
| 243 | while conn.notifies: |
| 244 | notify = conn.notifies.pop(0) |
| 245 | job_id = int(notify.payload) |
| 246 | print(f"Processing job {job_id} from notification") |
| 247 | cur.execute("SELECT process_job(%s)", (job_id,)) |
| 248 | |
| 249 | except KeyboardInterrupt: |
| 250 | print("\nShutting down worker...") |
| 251 | cleanup() |
| 252 | break |
| 253 | except Exception as e: |
| 254 | print(f"Worker error: {e}") |
| 255 | import time |
| 256 | time.sleep(5) |
| 257 | |
| 258 | conn.close() |
| 259 | |
| 260 | |
| 261 | def init_database(): |
| 262 | """Initialize the PostBlob database""" |
| 263 | conn = psycopg2.connect(DATABASE_URL) |
| 264 | conn.autocommit = True |
| 265 | cur = conn.cursor() |
| 266 | |
| 267 | print("Initializing PostBlob database...") |
| 268 | |
| 269 | # Read and execute the SQL file |
| 270 | with open('init_postblob.sql', 'r') as f: |
| 271 | sql = f.read() |
| 272 | cur.execute(sql) |
| 273 | |
| 274 | print("Database initialized successfully!") |
| 275 | conn.close() |
| 276 | |
| 277 | |
| 278 | def run_cli(): |
| 279 | """Run interactive CLI for testing""" |
| 280 | conn = psycopg2.connect(DATABASE_URL) |
| 281 | conn.autocommit = True |
| 282 | cur = conn.cursor(cursor_factory=RealDictCursor) |
| 283 | |
| 284 | print("PostBlob Interactive CLI") |
| 285 | print("Type 'help' for commands, 'exit' to quit") |
| 286 | |
| 287 | while True: |
| 288 | try: |
| 289 | command = input("\npostblob> ").strip() |
| 290 | |
| 291 | if command == 'exit': |
| 292 | break |
| 293 | elif command == 'help': |
| 294 | print(""" |
| 295 | Available commands: |
| 296 | stats - Show database statistics |
| 297 | jobs - List recent jobs |
| 298 | blobs - List recent blobs |
| 299 | exceptions - Show recent exceptions |
| 300 | execute <code> - Execute Python code in PostgreSQL |
| 301 | sql <query> - Execute raw SQL |
| 302 | exit - Exit CLI |
| 303 | """) |
| 304 | elif command == 'stats': |
| 305 | cur.execute("SELECT handle_request('GET', '/stats', '{}', '{}') as result") |
| 306 | result = json.loads(cur.fetchone()['result']) |
| 307 | print(json.dumps(result['body'], indent=2)) |
| 308 | |
| 309 | elif command == 'jobs': |
| 310 | cur.execute("SELECT * FROM jobs ORDER BY created_at DESC LIMIT 5") |
| 311 | for job in cur.fetchall(): |
| 312 | print(f"Job {job['id']}: {job['type']} - {job['status']}") |
| 313 | |
| 314 | elif command == 'blobs': |
| 315 | cur.execute("SELECT id, filename, size, created_at FROM blobs ORDER BY created_at DESC LIMIT 5") |
| 316 | for blob in cur.fetchall(): |
| 317 | print(f"Blob {blob['id']}: {blob['filename']} ({blob['size']} bytes)") |
| 318 | |
| 319 | elif command == 'exceptions': |
| 320 | cur.execute("SELECT * FROM exceptions ORDER BY occurred_at DESC LIMIT 5") |
| 321 | for exc in cur.fetchall(): |
| 322 | print(f"Exception: {exc['error_type']} - {exc['message']}") |
| 323 | |
| 324 | elif command.startswith('execute '): |
| 325 | code = command[8:] |
| 326 | cur.execute("SELECT safe_execute(%s) as result", (code,)) |
| 327 | result = json.loads(cur.fetchone()['result']) |
| 328 | print(json.dumps(result, indent=2)) |
| 329 | |
| 330 | elif command.startswith('sql '): |
| 331 | query = command[4:] |
| 332 | cur.execute(query) |
| 333 | if cur.description: |
| 334 | for row in cur.fetchall(): |
| 335 | print(dict(row)) |
| 336 | |
| 337 | else: |
| 338 | print(f"Unknown command: {command}") |
| 339 | |
| 340 | except Exception as e: |
| 341 | print(f"Error: {e}") |
| 342 | |
| 343 | conn.close() |
| 344 | print("Goodbye!") |
| 345 | |
| 346 | |
| 347 | def main(): |
| 348 | parser = argparse.ArgumentParser(description='PostBlob - PostgreSQL does everything') |
| 349 | parser.add_argument('command', choices=['init', 'server', 'worker', 'cli', 'all'], |
| 350 | help='Command to run') |
| 351 | parser.add_argument('--port', type=int, default=8000, |
| 352 | help='Server port (default: 8000)') |
| 353 | |
| 354 | args = parser.parse_args() |
| 355 | |
| 356 | if args.command == 'init': |
| 357 | init_database() |
| 358 | elif args.command == 'server': |
| 359 | run_server(args.port) |
| 360 | elif args.command == 'worker': |
| 361 | run_worker() |
| 362 | elif args.command == 'cli': |
| 363 | run_cli() |
| 364 | elif args.command == 'all': |
| 365 | # Run server and worker in same process (for development) |
| 366 | import threading |
| 367 | worker_thread = threading.Thread(target=run_worker, daemon=True) |
| 368 | worker_thread.start() |
| 369 | run_server(args.port) |
| 370 | |
| 371 | |
| 372 | if __name__ == '__main__': |
| 373 | main() |