-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
77 lines (55 loc) · 1.96 KB
/
app.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
import importlib.util
import json
import os
import requests
from fastapi import FastAPI
from api.service.routing import RedirectResponse
from api.service import error_handler
from api.service.pretty_response import PrettyJSONResponse
error_handler.set_exception_handler()
with open("config.json") as f:
config = json.load(f)
os.environ['LOGFILE_PATH'] = config.get("logfile", "log.db")
def close_running_instance():
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((config.get("host"), config.get("port")))
except socket.error:
print("Port is already in use. Shutting down running instance...")
try:
requests.get(f"http://{config.get('host')}:{config.get('port')}/host/shutdown")
except requests.exceptions.ConnectionError:
print("Could not connect to running instance. Assuming it is already shut down...")
else:
print("Closed running instance.")
close_running_instance()
root_path = os.environ.get("FASTAPI_ROOT_PATH", None)
if root_path:
print(f"Using root path: {root_path}")
app = FastAPI(root_path=root_path)
app.root_path = root_path
else:
app = FastAPI()
app.root_path = None
@app.get('/')
async def index():
response = RedirectResponse(url='/ping', root_path=app.root_path)
return response
@app.get('/ping/')
async def ping():
response = {"response": "I am alive!"}
return PrettyJSONResponse(content=response)
@app.get('/urls/')
async def urls():
response = {"response": app.openapi().get("paths")}
return PrettyJSONResponse(response)
with open("api/route/routes.json") as f:
routes = json.load(f)
for route in routes:
importlib.util.spec_from_file_location(route, f"api/route/{route}.py")
module = importlib.import_module(f"api.route.{route}")
module.setup(app)
if __name__ == '__main__':
import uvicorn
uvicorn.run(app, host=config.get("host"), port=config.get("port"))