-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
71 lines (55 loc) · 1.98 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
71
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
import logging
from .library.helpers import (
load_errors_lookup,
load_errors_specs_lookup,
load_good_specs,
openfile,
)
from app.routers import errors
from app.core.logging import init_loggers
import os
init_loggers()
log = logging.getLogger("error-analysis")
app = FastAPI()
here = os.path.dirname(os.path.abspath(__file__))
root = os.path.dirname(here)
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
app.mount("/data", StaticFiles(directory="data"), name="data")
app.include_router(errors.router)
@app.middleware("http")
async def load_app_data(request: Request, call_next):
"""
Middleware to ensure that data is always loaded
"""
# If we don't have data loaded, load it
if not hasattr(request.app, "error_clusters"):
log.info("Loading cluster errors lookup")
request.app.error_clusters = load_errors_lookup(root)
if not hasattr(request.app, "spec_errors"):
request.app.spec_errors = load_errors_specs_lookup(root)
if not hasattr(request.app, "good_specs"):
request.app.good_specs = load_good_specs(root)
# Save the app root and app directory root (here)
app.here = here
app.root = root
return await call_next(request)
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
data = openfile("index.md")
return templates.TemplateResponse(
"index.html",
{
"request": request,
"data": data,
"error_clusters": request.app.error_clusters,
},
)
@app.get("/page/{page_name}", response_class=HTMLResponse)
async def show_page(request: Request, page_name: str):
data = openfile(page_name + ".md")
return templates.TemplateResponse("page.html", {"request": request, "data": data})