-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapi.py
39 lines (31 loc) · 1.05 KB
/
api.py
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
# api/api.py
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routes import router
from wdb.logger import AsyncLogger
class API:
def __init__(self):
self.app = FastAPI()
self.logger = AsyncLogger()
# CORS middleware
self.app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Allow all origins for simplicity; adjust as needed
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routes
self.app.include_router(router)
async def log_request(self, request):
await self.logger.log('INFO', f"Request: {request.method} {request.url}")
async def log_response(self, response):
await self.logger.log('INFO', f"Response: {response.status_code}")
return response
def run(self, host="0.0.0.0", port=8000):
import uvicorn
uvicorn.run(self.app, host=host, port=port)
# Example usage
if __name__ == "__main__":
api = API()
api.run()