feat: add main entry point wiring API server and poller
Made-with: Cursor
This commit is contained in:
@@ -1,3 +1,52 @@
|
||||
import asyncio
|
||||
import logging
|
||||
|
||||
import uvicorn
|
||||
|
||||
from ntr_fetcher.api import create_app
|
||||
from ntr_fetcher.config import Settings
|
||||
from ntr_fetcher.db import Database
|
||||
from ntr_fetcher.poller import Poller
|
||||
from ntr_fetcher.soundcloud import SoundCloudClient
|
||||
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||||
)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def run() -> None:
|
||||
"""Entry point for ntr-fetcher CLI."""
|
||||
pass
|
||||
settings = Settings()
|
||||
|
||||
db = Database(settings.db_path)
|
||||
db.initialize()
|
||||
logger.info("Database initialized at %s", settings.db_path)
|
||||
|
||||
sc = SoundCloudClient()
|
||||
poller = Poller(
|
||||
db=db,
|
||||
soundcloud=sc,
|
||||
soundcloud_user=settings.soundcloud_user,
|
||||
show_day=settings.show_day,
|
||||
show_hour=settings.show_hour,
|
||||
poll_interval=settings.poll_interval_seconds,
|
||||
)
|
||||
|
||||
app = create_app(db=db, poller=poller, admin_token=settings.admin_token)
|
||||
|
||||
@app.on_event("startup")
|
||||
async def start_poller():
|
||||
logger.info("Starting poller (interval=%ds)", settings.poll_interval_seconds)
|
||||
asyncio.create_task(poller.run_supervised())
|
||||
|
||||
@app.on_event("shutdown")
|
||||
async def shutdown():
|
||||
logger.info("Shutting down")
|
||||
await sc.close()
|
||||
|
||||
uvicorn.run(app, host=settings.host, port=settings.port)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
run()
|
||||
|
||||
Reference in New Issue
Block a user