Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
101 lines
3.0 KiB
Python
101 lines
3.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Entry point of the Mini CRM.
|
|
|
|
Run it with ``python3 server.py``. Configuration comes from the environment:
|
|
|
|
* ``PORT`` -- TCP port, default 8000 (always bound to 127.0.0.1)
|
|
* ``CRM_DB`` -- SQLite file, default ``crm.db``
|
|
|
|
The implementation lives in the sibling modules ``db``, ``models``,
|
|
``validation``, ``responses``, ``router`` and ``handlers``. This module wires
|
|
them together and re-exports their public names, so ``import server`` keeps
|
|
working for the tests and for scripts built on top of it.
|
|
"""
|
|
|
|
import os
|
|
from http.server import ThreadingHTTPServer
|
|
|
|
from db import SCHEMA, Store
|
|
from errors import MethodNotAllowedError, NotFoundError, ValidationError
|
|
from handlers import CRMRequestHandler
|
|
from models import DEFAULT_STAGE, RESOURCES, STAGES, now_iso
|
|
from responses import CONTENT_TYPES, FileResponse, Response
|
|
from router import CRMApp
|
|
from validation import (
|
|
EMAIL_RE,
|
|
MAX_NOTE_LEN,
|
|
MAX_TEXT_LEN,
|
|
clean_amount,
|
|
clean_email,
|
|
clean_reference,
|
|
clean_stage,
|
|
clean_text,
|
|
parse_id,
|
|
require_object,
|
|
)
|
|
|
|
HOST = "127.0.0.1"
|
|
DEFAULT_PORT = 8000
|
|
DEFAULT_DB = "crm.db"
|
|
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
|
STATIC_DIR = os.path.join(BASE_DIR, "static")
|
|
|
|
__all__ = [
|
|
"CONTENT_TYPES", "CRMApp", "CRMRequestHandler", "CRMServer", "DEFAULT_DB",
|
|
"DEFAULT_PORT", "DEFAULT_STAGE", "EMAIL_RE", "FileResponse", "HOST",
|
|
"MAX_NOTE_LEN", "MAX_TEXT_LEN", "MethodNotAllowedError", "NotFoundError",
|
|
"RESOURCES", "Response", "SCHEMA", "STAGES", "STATIC_DIR", "Store",
|
|
"ValidationError", "clean_amount", "clean_email", "clean_reference",
|
|
"clean_stage", "clean_text", "config_from_env", "create_server", "main",
|
|
"now_iso", "parse_id", "require_object",
|
|
]
|
|
|
|
|
|
class CRMServer(ThreadingHTTPServer):
|
|
"""Binds the CRM app to a loopback TCP port."""
|
|
|
|
allow_reuse_address = True
|
|
daemon_threads = True
|
|
|
|
def __init__(self, host, port, db_path, static_dir=STATIC_DIR):
|
|
self.app = CRMApp(Store(db_path), static_dir)
|
|
super().__init__((host, port), CRMRequestHandler)
|
|
|
|
def shutdown(self):
|
|
super().shutdown()
|
|
self.app.store.close()
|
|
|
|
|
|
def create_server(port=None, db_path=None, host=HOST):
|
|
return CRMServer(host, int(port), db_path)
|
|
|
|
|
|
def config_from_env(environ=None):
|
|
"""Read PORT and CRM_DB, with the documented defaults."""
|
|
environ = os.environ if environ is None else environ
|
|
raw_port = environ.get("PORT", str(DEFAULT_PORT))
|
|
try:
|
|
port = int(raw_port)
|
|
except ValueError:
|
|
raise SystemExit("PORT must be an integer, got %r" % raw_port)
|
|
return port, environ.get("CRM_DB", DEFAULT_DB)
|
|
|
|
|
|
def main(argv=None):
|
|
port, db_path = config_from_env()
|
|
server = create_server(port, db_path)
|
|
print(
|
|
"CRM listening on http://%s:%d (db: %s)"
|
|
% (server.server_address[0], server.server_address[1], db_path)
|
|
)
|
|
try:
|
|
server.serve_forever()
|
|
except KeyboardInterrupt:
|
|
print("\nshutting down")
|
|
finally:
|
|
server.shutdown()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|