-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
84 lines (71 loc) · 2.44 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
72
73
74
75
76
77
78
79
80
81
82
83
84
"""Main module for FastAPI service"""
import markdown
from fastapi import FastAPI, Request, APIRouter
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from secure import secure
from starlette.exceptions import HTTPException as StarletteHTTPException
from starlette.middleware.cors import CORSMiddleware
from starlette.responses import Response
from src.routes import extract
from src.util import Utils
SWAGGER_URL = f"{Utils.get_environment_prefix()}/swagger-ui"
allowed_origins = ["https://*.nb.no*", "http://*.nb.no*"]
app = FastAPI(
title="METEOR",
description="API documentation for METEOR",
docs_url=SWAGGER_URL,
openapi_url=f"{Utils.get_environment_prefix()}/openapi.json"
)
app.mount(
f"{Utils.get_environment_prefix()}/static",
StaticFiles(directory="static"),
name="static"
)
app.add_middleware(CORSMiddleware, allow_origins=allowed_origins, allow_methods=["GET", "POST"])
secure_headers = secure.Secure()
router = APIRouter(prefix=Utils.get_environment_prefix())
router.include_router(extract.router)
app.include_router(router=router)
templates = Jinja2Templates(directory="templates")
@app.get(f"{Utils.get_environment_prefix()}/doc", tags=["Documentation"])
def get_documentation(request: Request) -> Response:
doc_text = "\n"
with open("DOC.md", encoding="UTF-8") as infile:
for line in infile:
doc_text += line
return templates.TemplateResponse(
"doc.html",
{
"request": request,
"doc": markdown.markdown(doc_text),
"root_path": Utils.get_environment_prefix()
}
)
@app.get(
f"{Utils.get_environment_prefix()}/",
tags=["Templates"],
response_class=HTMLResponse, status_code=200
)
async def get_front_page_html(request: Request) -> Response:
return templates.TemplateResponse(
"index.html",
{
"request": request,
"root_path": Utils.get_environment_prefix()
}
)
@app.exception_handler(StarletteHTTPException)
def display_error_message_in_template(request: Request, exc: StarletteHTTPException) -> Response:
return templates.TemplateResponse(
"index.html",
{
"request": request,
"results": {
'error': str(exc.detail)
},
"root_path": Utils.get_environment_prefix()
},
status_code=exc.status_code
)