-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.py
70 lines (53 loc) · 1.86 KB
/
main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from starlette.middleware.cors import CORSMiddleware
from starlette.requests import Request
from routes import proxy
from routes import examples
from utils import create_logger
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
logger = create_logger("app", ".logs/access.log")
app.mount("/static", StaticFiles(directory="frontend/assets"), name="static")
templates = Jinja2Templates(directory="frontend/pages")
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def log_requests(request: Request, call_next):
if "/proxy" in request.url.path:
client_ip = request.client.host
logger.info(
f"Incoming request from {client_ip}: {request.method} {request.url}"
)
response = await call_next(request)
# logger.info(f"Response status code: {response.status_code}")
return response
else:
return await call_next(request)
app.include_router(proxy.router, prefix="/proxy")
app.include_router(examples.router, prefix="/examples")
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# Add an get endpoint simple return the evrsion of the app
@app.get("/version")
async def version():
return {"version": "0.0.4"}
if __name__ == "__main__":
import uvicorn
# uvicorn.run("main:app", host=os.getenv("HOST"), port=int(os.getenv('PORT')), workers=1, reload=True)
uvicorn.run(
"main:app", host=os.getenv("HOST"), port=int(os.getenv("PORT")), workers=1, reload=False
)