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
cli.py · 3.7 KB · 140 lines
| 1 | """CLI entry point for dbbasic-web command""" |
| 2 | import sys |
| 3 | import os |
| 4 | |
| 5 | |
| 6 | def main(): |
| 7 | """Main CLI entry point (installed as 'dbbasic-web' command)""" |
| 8 | if len(sys.argv) < 2: |
| 9 | print("Usage: dbbasic-web [command]") |
| 10 | print("\nCommands:") |
| 11 | print(" init <name> - Create a new dbbasic-web project") |
| 12 | print(" serve - Run the development server") |
| 13 | print(" worker - Run the background job worker") |
| 14 | print(" shell - Start interactive shell") |
| 15 | sys.exit(1) |
| 16 | |
| 17 | command = sys.argv[1] |
| 18 | |
| 19 | if command == "init": |
| 20 | init_project() |
| 21 | elif command == "serve": |
| 22 | serve() |
| 23 | elif command == "worker": |
| 24 | worker() |
| 25 | elif command == "shell": |
| 26 | shell() |
| 27 | else: |
| 28 | print(f"Unknown command: {command}") |
| 29 | sys.exit(1) |
| 30 | |
| 31 | |
| 32 | def init_project(): |
| 33 | """Create a new dbbasic-web project""" |
| 34 | if len(sys.argv) < 3: |
| 35 | print("Usage: dbbasic-web init <project_name>") |
| 36 | sys.exit(1) |
| 37 | |
| 38 | project_name = sys.argv[2] |
| 39 | print(f"Creating new dbbasic-web project: {project_name}") |
| 40 | |
| 41 | # TODO: Scaffold project structure |
| 42 | print("Project scaffolding not yet implemented") |
| 43 | print(f"Manually create: {project_name}/") |
| 44 | print(" - {project_name}_web/") |
| 45 | print(" - api/") |
| 46 | print(" - templates/") |
| 47 | print(" - public/") |
| 48 | print(" - manage.py") |
| 49 | print(" - pyproject.toml") |
| 50 | |
| 51 | |
| 52 | def serve(): |
| 53 | """Run the development server""" |
| 54 | import uvicorn |
| 55 | |
| 56 | host = os.getenv("HOST", "0.0.0.0") |
| 57 | port = int(os.getenv("PORT", "8000")) |
| 58 | |
| 59 | # Try to import from current project |
| 60 | try: |
| 61 | # Look for app in current directory |
| 62 | sys.path.insert(0, os.getcwd()) |
| 63 | print(f"Starting dbbasic-web on http://{host}:{port}") |
| 64 | |
| 65 | # Try common module names |
| 66 | for module_name in ["app.asgi:app", "dbbasic_web.asgi:app"]: |
| 67 | try: |
| 68 | uvicorn.run( |
| 69 | module_name, |
| 70 | host=host, |
| 71 | port=port, |
| 72 | reload=True, |
| 73 | log_level="info", |
| 74 | ) |
| 75 | return |
| 76 | except (ImportError, ModuleNotFoundError): |
| 77 | continue |
| 78 | |
| 79 | print("Error: Could not find ASGI application") |
| 80 | print("Expected: app.asgi:app or dbbasic_web.asgi:app") |
| 81 | sys.exit(1) |
| 82 | except Exception as e: |
| 83 | print(f"Error starting server: {e}") |
| 84 | sys.exit(1) |
| 85 | |
| 86 | |
| 87 | def worker(): |
| 88 | """Run background job worker""" |
| 89 | import time |
| 90 | |
| 91 | sys.path.insert(0, os.getcwd()) |
| 92 | |
| 93 | try: |
| 94 | from dbbasic_web.jobs import process_jobs |
| 95 | except ImportError: |
| 96 | print("Error: Could not import jobs module") |
| 97 | sys.exit(1) |
| 98 | |
| 99 | print("Starting job worker...") |
| 100 | while True: |
| 101 | try: |
| 102 | process_jobs() |
| 103 | time.sleep(1) |
| 104 | except KeyboardInterrupt: |
| 105 | print("\nWorker stopped") |
| 106 | break |
| 107 | except Exception as e: |
| 108 | print(f"Worker error: {e}") |
| 109 | time.sleep(5) |
| 110 | |
| 111 | |
| 112 | def shell(): |
| 113 | """Start interactive shell with app context""" |
| 114 | import code |
| 115 | |
| 116 | sys.path.insert(0, os.getcwd()) |
| 117 | |
| 118 | try: |
| 119 | from dbbasic_web import settings |
| 120 | from dbbasic_web.storage import write_text, read_text |
| 121 | from dbbasic_web.jobs import enqueue |
| 122 | from dbbasic_web.bus import EventBus |
| 123 | |
| 124 | context = { |
| 125 | "settings": settings, |
| 126 | "write_text": write_text, |
| 127 | "read_text": read_text, |
| 128 | "enqueue": enqueue, |
| 129 | "EventBus": EventBus, |
| 130 | } |
| 131 | |
| 132 | banner = "dbbasic-web interactive shell\nAvailable: settings, write_text, read_text, enqueue, EventBus" |
| 133 | code.interact(banner=banner, local=context) |
| 134 | except ImportError as e: |
| 135 | print(f"Error importing modules: {e}") |
| 136 | sys.exit(1) |
| 137 | |
| 138 | |
| 139 | if __name__ == "__main__": |
| 140 | main() |