-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
86 lines (65 loc) · 2.36 KB
/
main.py
File metadata and controls
86 lines (65 loc) · 2.36 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# main.py
from datetime import datetime
import time
import os
from contextlib import asynccontextmanager
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.sql import text
from routers import cpu, gpu, motherboard, ram, disk, oses, config, benchmark, benchmark_results
from utils.auth import authenticate
from utils.hardware_loader import run_if_enabled
from database import init_db, engine
def _allowed_origins() -> list[str]:
raw = os.getenv("ALLOWED_ORIGINS", "*").strip()
if raw == "*" or raw == "":
return ["*"]
return [o.strip() for o in raw.split(",") if o.strip()]
@asynccontextmanager
async def lifespan(app: FastAPI):
init_db()
run_if_enabled()
yield
app = FastAPI(
lifespan=lifespan,
dependencies=[Depends(authenticate)],
)
app.add_middleware(
CORSMiddleware,
allow_origins=_allowed_origins(),
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Routers
app.include_router(cpu.router, prefix="/api/cpu", tags=["CPU"])
app.include_router(gpu.router, prefix="/api/gpu", tags=["GPU"])
app.include_router(motherboard.router, prefix="/api/motherboard", tags=["Motherboard"])
app.include_router(ram.router, prefix="/api/ram", tags=["RAM"])
app.include_router(disk.router, prefix="/api/disk", tags=["Disk"])
app.include_router(oses.router, prefix="/api/oses", tags=["OS"])
app.include_router(config.router, prefix="/api/config", tags=["Config"])
app.include_router(benchmark.router, prefix="/api/benchmark", tags=["Benchmark"])
app.include_router(benchmark_results.router, prefix="/api/benchmark_results", tags=["Benchmark Results"])
healthz_app = FastAPI(openapi_url=None, docs_url=None, redoc_url=None)
@healthz_app.get("/", include_in_schema=False)
def healthz():
return {"status": "ok"}
readyz_app = FastAPI(openapi_url=None, docs_url=None, redoc_url=None)
@readyz_app.get("/", include_in_schema=False)
def readyz():
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
return {"status": "ready"}
app.mount("/healthz", healthz_app)
app.mount("/readyz", readyz_app)
@app.get("/")
def read_root():
version = datetime.now().strftime("%d%m%Y")
return {
"app": "benchmarkinator-api",
"version": version,
"db": "mysql",
"build_no": int(time.time()),
"build_name": "Manuel Cavalera",
}