-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
61 lines (50 loc) · 1.5 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
from fastapi.middleware.cors import CORSMiddleware
from src.utils.arguments import arg_parser
from src.utils.scheduler import schedule
from src.api.v1.api import api_router
from src.utils.openapi import openapi
from src.utils.metrics import metrics
from src.utils.log import logger
from src.utils import settings
from fastapi import FastAPI
import uvicorn
import sys
args = arg_parser()
prom_addr, rule_path = args.get("prom.addr"), args.get("rule.path")
host, port = args.get("web.listen_address").split(":")
if not all([settings.check_rules_directory(rule_path),
settings.check_fs_permissions(rule_path),
settings.establish_prom_connection(prom_addr),
settings.check_reload_api_status(prom_addr)]):
sys.exit()
def custom_openapi_wrapper():
return openapi(app)
app = FastAPI(swagger_ui_parameters={"defaultModelsExpandDepth": -1})
app.openapi = custom_openapi_wrapper
metrics(app)
origins = ["*"]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router)
def main():
config = uvicorn.Config(
app,
host=host,
port=int(port),
server_header=False,
date_header=False,
log_config=None)
server = uvicorn.Server(config)
try:
logger.info(f"Server listening on {host}:{port}")
server.run()
except BaseException as e:
logger.error(e)
if __name__ == "__main__":
schedule()
main()