askrobots / dbbasic-web
Public
A Unix-style Python web framework: filesystem routing, flat files, messages and cron, with CGI's simplicity at modern speed.
Dan Quellhorst
Replace multipart parser with stdlib-only implementation
eb957a2
9 months, 2 weeks ago
History
manage.py · 2.0 KB · 85 lines
| 1 | #!/usr/bin/env python3 |
| 2 | """Management CLI for dbbasic-web""" |
| 3 | import sys |
| 4 | import os |
| 5 | |
| 6 | |
| 7 | def main(): |
| 8 | if len(sys.argv) < 2: |
| 9 | print("Usage: python manage.py [command]") |
| 10 | print("\nCommands:") |
| 11 | print(" serve - Run the development server") |
| 12 | print(" worker - Run the background job worker") |
| 13 | print(" shell - Start interactive shell") |
| 14 | sys.exit(1) |
| 15 | |
| 16 | command = sys.argv[1] |
| 17 | |
| 18 | if command == "serve": |
| 19 | serve() |
| 20 | elif command == "worker": |
| 21 | worker() |
| 22 | elif command == "shell": |
| 23 | shell() |
| 24 | else: |
| 25 | print(f"Unknown command: {command}") |
| 26 | sys.exit(1) |
| 27 | |
| 28 | |
| 29 | def serve(): |
| 30 | """Run the development server""" |
| 31 | import uvicorn |
| 32 | |
| 33 | host = os.getenv("HOST", "0.0.0.0") |
| 34 | port = int(os.getenv("PORT", "8000")) |
| 35 | |
| 36 | print(f"Starting dbbasic-web on http://{host}:{port}") |
| 37 | uvicorn.run( |
| 38 | "dbbasic_web.asgi:app", |
| 39 | host=host, |
| 40 | port=port, |
| 41 | reload=True, |
| 42 | log_level="info", |
| 43 | ) |
| 44 | |
| 45 | |
| 46 | def worker(): |
| 47 | """Run background job worker""" |
| 48 | from dbbasic_web.jobs import process_jobs |
| 49 | import time |
| 50 | |
| 51 | print("Starting job worker...") |
| 52 | while True: |
| 53 | try: |
| 54 | process_jobs() |
| 55 | time.sleep(1) |
| 56 | except KeyboardInterrupt: |
| 57 | print("\nWorker stopped") |
| 58 | break |
| 59 | except Exception as e: |
| 60 | print(f"Worker error: {e}") |
| 61 | time.sleep(5) |
| 62 | |
| 63 | |
| 64 | def shell(): |
| 65 | """Start interactive shell with app context""" |
| 66 | import code |
| 67 | from dbbasic_web import settings |
| 68 | from dbbasic_web.storage import write_text, read_text |
| 69 | from dbbasic_web.jobs import enqueue |
| 70 | from dbbasic_web.bus import EventBus |
| 71 | |
| 72 | context = { |
| 73 | "settings": settings, |
| 74 | "write_text": write_text, |
| 75 | "read_text": read_text, |
| 76 | "enqueue": enqueue, |
| 77 | "EventBus": EventBus, |
| 78 | } |
| 79 | |
| 80 | banner = "dbbasic-web interactive shell\nAvailable: settings, write_text, read_text, enqueue, EventBus" |
| 81 | code.interact(banner=banner, local=context) |
| 82 | |
| 83 | |
| 84 | if __name__ == "__main__": |
| 85 | main() |