from __future__ import annotations

from http.server import ThreadingHTTPServer, BaseHTTPRequestHandler

from .config import get_config
from .web import WebApp


class Handler(BaseHTTPRequestHandler):
    app: WebApp

    def do_GET(self) -> None:
        self.app.dispatch(self)

    def do_POST(self) -> None:
        self.app.dispatch(self)

    def log_message(self, fmt: str, *args) -> None:
        print("%s - %s" % (self.address_string(), fmt % args))


def main() -> None:
    config = get_config()
    Handler.app = WebApp(config)
    server = ThreadingHTTPServer((config.host, config.port), Handler)
    print(f"Sticknife registration listening on http://{config.host}:{config.port}")
    server.serve_forever()

