-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
95 lines (71 loc) · 3.02 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
85
86
87
88
89
90
91
92
93
94
95
import sys
from loguru import logger
from fastapi import FastAPI
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from starlette.middleware.cors import CORSMiddleware
from certify.core.error.http_error import http_error_handler
from certify.core.error.validation_error import http422_error_handler
from certify.routes import router
from certify.core.logging import intercept_logging
from certify.core.config import ALLOWED_HOSTS, DEBUG, SECRET_KEY
from certify.core.events import create_start_app_handler, create_stop_app_handler
from certify.core.middleware.jwt import OAuthMiddleware
print(
"""
██████╗███████╗██████╗ ████████╗██╗███████╗██╗ ██╗
██╔════╝██╔════╝██╔══██╗╚══██╔══╝██║██╔════╝╚██╗ ██╔╝
██║ █████╗ ██████╔╝ ██║ ██║█████╗ ╚████╔╝
██║ ██╔══╝ ██╔══██╗ ██║ ██║██╔══╝ ╚██╔╝
╚██████╗███████╗██║ ██║ ██║ ██║██║ ██║
╚═════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝
"""
)
def catch_exceptions():
sys.excepthook = lambda _type, message, stack: logger.opt(
exception=(_type, message, stack)
).error("Uncaught Exception")
def create_application():
catch_exceptions()
intercept_logging()
app = FastAPI(
title="Certify",
description="Certificate Verfification System API for IITM-POD",
version="1.0.0",
debug=DEBUG
)
logger.info(f"Starting Certify CVS server")
logger.info(f"Island API endpoint - /api/")
logger.info("Adding middlewares")
logger.debug("Adding OAuthMiddleware")
app.add_middleware(OAuthMiddleware)
logger.debug("Adding CORSMiddleware")
app.add_middleware(
CORSMiddleware,
allow_origins=ALLOWED_HOSTS or ["*"],
allow_credentials=True,
allow_methods=["GET", "POST"],
allow_headers=["*"], #TODO: Dangerous practise
)
logger.info("Adding startup and shutdown events")
app.add_event_handler(
"startup", create_start_app_handler(app))
app.add_event_handler(
"shutdown", create_stop_app_handler(app))
logger.info("Adding exception handlers")
app.add_exception_handler(
HTTPException,
http_error_handler
)
app.add_exception_handler(
RequestValidationError,
http422_error_handler
)
logger.info("Adding routers")
app.include_router(router)
logger.info("Application setup complete")
return app
app = create_application()
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", log_level="debug")