-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathmain.py
More file actions
55 lines (40 loc) · 1.56 KB
/
main.py
File metadata and controls
55 lines (40 loc) · 1.56 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
from contextlib import asynccontextmanager
from fastapi import FastAPI
from routers import (
inserate_ultra as inserate,
inserat,
inserate_detailed_ultra as inserate_detailed,
)
from utils.browser import OptimizedPlaywrightManager
from utils.asyncio_optimizations import EventLoopOptimizer
# Global browser manager instance for sharing across all endpoints
browser_manager = None
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Manage application lifecycle - startup and shutdown events"""
global browser_manager
# Setup uvloop for maximum performance (2-4x improvement)
uvloop_enabled = EventLoopOptimizer.setup_uvloop()
# Optimize event loop settings
EventLoopOptimizer.optimize_event_loop()
# Startup: Initialize shared browser manager with optimized settings
browser_manager = OptimizedPlaywrightManager(max_contexts=20, max_concurrent=10)
await browser_manager.start()
# Store browser manager in app state for access by routers
app.state.browser_manager = browser_manager
app.state.uvloop_enabled = uvloop_enabled
yield
# Shutdown: Clean up browser resources
if browser_manager:
await browser_manager.close()
app = FastAPI(version="1.0.0", lifespan=lifespan)
@app.get("/")
async def root():
return {
"message": "Welcome to the Kleinanzeigen API",
"endpoints": ["/inserate", "/inserat/{id}", "/inserate-detailed"],
"status": "operational",
}
app.include_router(inserate.router)
app.include_router(inserat.router)
app.include_router(inserate_detailed.router)