-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (49 loc) · 1.74 KB
/
main.py
File metadata and controls
59 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from models import init_db
from routes import router
from hdhomerun_routes import router as hdhomerun_router
import logging
import time
from fastapi import Request
logger = logging.getLogger(__name__)
def create_app():
app = FastAPI(
title="IPTV Manager with HDHomeRun Emulation",
# Disable docs to prevent OpenAPI spec generation delays
docs_url=None,
redoc_url=None
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
# Initialize database in a background task to avoid blocking startup
@app.on_event("startup")
async def startup_event():
# Perform quick startup
logger.info("Starting application...")
init_db()
logger.info("Database initialized")
@app.middleware("http")
async def log_request_time(request: Request, call_next):
start = time.time()
path = request.url.path
# Only log non-static requests to reduce noise
if not path.startswith(("/static/", "/favicon.ico")):
logger.info(f"--> START {request.method} {path}")
try:
response = await call_next(request)
return response
finally:
duration = time.time() - start
if not path.startswith(("/static/", "/favicon.ico")):
logger.info(f"<-- END {request.method} {path} duration={duration:.3f}s")
logger.info("Application initialized, routing configured")
app.include_router(router)
app.include_router(hdhomerun_router)
logger.info("Application routes configured")
return app
app = create_app()