File size: 1,106 Bytes
51a7f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
from fastapi import FastAPI
from routes import embeddings, search, admin
from fastapi.middleware import Middleware
from fastapi.middleware.cors import CORSMiddleware
from datetime import datetime
from uvicorn.logging import DefaultFormatter
import logging
import uvicorn
from exception import generic_exception_handler

logger = logging.getLogger(__name__)
handler = logging.StreamHandler()
handler.setFormatter(DefaultFormatter())
logger.addHandler(handler)

# Create the FastAPI instance
app = FastAPI()
app.include_router(embeddings.router)
app.include_router(search.router)
app.include_router(admin.router)
app.exception_handler(generic_exception_handler)

@app.middleware("http")
async def log_requests(request, call_next):
    start_time = datetime.utcnow()
    response = await call_next(request)
    process_time = (datetime.utcnow() - start_time).total_seconds() * 1000
    print(
        f"{request.method} {request.url.path} {response.status_code} {process_time:.2f}ms"
    )
    return response


if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8080)