-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
57 lines (44 loc) · 1.76 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
from fastapi import FastAPI, Depends, HTTPException, status
from fastapi.security import HTTPBasic, HTTPBasicCredentials
import secrets
import logging
import asyncio
from app.routes import jobs, test
from app.config.settings import scheduler
import os
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
security = HTTPBasic()
def verify_credentials(credentials: HTTPBasicCredentials = Depends(security)):
correct_username = os.getenv("API_DOCS_USERNAME", "admin")
correct_password = os.getenv("API_DOCS_PASSWORD", "secret")
is_correct_username = secrets.compare_digest(
credentials.username.encode("utf8"), correct_username.encode("utf8"))
is_correct_password = secrets.compare_digest(
credentials.password.encode("utf8"), correct_password.encode("utf8"))
if not (is_correct_username and is_correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect username or password",
headers={"WWW-Authenticate": "Basic"},
)
return credentials
app = FastAPI(title="Scheduler Service API", version="1.0.0",
description="Scheduler Service API",
openapi_url="/openapi.json",
docs_url="/docs",
redoc_url="/redoc",
dependencies=[Depends(verify_credentials)])
app.include_router(jobs.router, tags=["jobs"])
app.include_router(test.router, tags=["test"])
@app.on_event("startup")
async def startup_event():
scheduler.start()
logger.info("Scheduler started")
@app.on_event("shutdown")
async def shutdown_event():
scheduler.shutdown()
logger.info("Scheduler shutdown")
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8008)