-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
38 lines (29 loc) · 1.04 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
from contextlib import asynccontextmanager
from datetime import datetime
import json
from fastapi import FastAPI, Response, status as HTTPStatusCode
import db
import admin.router, interaction.router
LAST_RESTART_TIME = datetime.utcnow()
# lifecycle manager to handle startup/shutdown tasks
@asynccontextmanager
async def lifespan(app: FastAPI):
# startup - create db tables if not exist
db.do_startup_actions()
# yield to let application run
yield
# perform shutdown tasks, if any
pass
app = FastAPI(lifespan=lifespan)
app.include_router(admin.router.router)
app.include_router(interaction.router.router)
@app.get('/')
def root():
now = datetime.utcnow()
content = {
'current_time_utc' : now.isoformat(),
'last_restart_time_utc' : LAST_RESTART_TIME.isoformat(),
'uptime_seconds' : (now - LAST_RESTART_TIME).total_seconds() // 1,
'uptime_days' : (now - LAST_RESTART_TIME).total_seconds() / 86400,
}
return Response(content=json.dumps(content), status_code=HTTPStatusCode.HTTP_200_OK)