44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
"""FastAPI admin application."""
|
|
from __future__ import annotations
|
|
|
|
import threading
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.responses import FileResponse
|
|
from fastapi.staticfiles import StaticFiles
|
|
|
|
from admin.models import init_db
|
|
from admin.routes import router
|
|
|
|
app = FastAPI(title="Seed Admin", docs_url=None, redoc_url=None)
|
|
|
|
app.include_router(router)
|
|
|
|
_STATIC = Path(__file__).parent / "static"
|
|
app.mount("/static", StaticFiles(directory=str(_STATIC)), name="static")
|
|
|
|
|
|
@app.on_event("startup")
|
|
def on_startup() -> None:
|
|
init_db()
|
|
_start_huey_consumer()
|
|
|
|
|
|
def _start_huey_consumer() -> None:
|
|
from huey.consumer import Consumer
|
|
from crawler.tasks import huey
|
|
|
|
class _ThreadConsumer(Consumer):
|
|
def _set_signal_handlers(self) -> None:
|
|
pass # signal.signal() only works on the main thread
|
|
|
|
consumer = _ThreadConsumer(huey, workers=1, periodic=False)
|
|
t = threading.Thread(target=consumer.run, daemon=True, name="huey-consumer")
|
|
t.start()
|
|
|
|
|
|
@app.get("/")
|
|
def dashboard() -> FileResponse:
|
|
return FileResponse(str(_STATIC / "dashboard.html"))
|