-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
79 lines (61 loc) · 2.28 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
import secrets
from fastapi import Depends, FastAPI, HTTPException, status
from contextlib import asynccontextmanager
from fastapi.security import HTTPBasic, HTTPBasicCredentials, OAuth2PasswordBearer
from db_config.peewee_connect import db
from typing import Union
from models.peewee_models import User, Reminder, Task, Tag, Category
from api import reminder, task, user, category
from logger import logger
from fastapi.responses import JSONResponse
models = {
'User': User,
'Reminder': Reminder,
'Task': Task,
'Tag': Tag,
'Category': Category
}
@asynccontextmanager
async def lifespan(app: FastAPI):
db.connect()
for _, model_class in models.items():
# db.drop_tables(models=[model_class])
if not model_class.table_exists():
db.create_tables([model_class])
logger.info("creating " + model_class.__name__)
else:
logger.info(model_class.__name__ + " DB tables have been created")
yield
db.close()
app = FastAPI(lifespan=lifespan)
security = HTTPBasic()
@app.get("/items/")
async def read_items(q: Union[str, None] = None):
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
if q:
results.update({"q": q})
return results
@app.get("/")
async def index(credentials: HTTPBasicCredentials = Depends(security)):
curr_username_bytes = credentials.username.encode("utf8")
correct_username_bytes = b"yums"
is_correct_username = secrets.compare_digest(
curr_username_bytes, correct_username_bytes
)
curr_password_bytes = credentials.password.encode("utf8")
correct_password_bytes = b"swordfish"
is_correct_password = secrets.compare_digest(
curr_password_bytes, correct_password_bytes
)
if not (is_correct_username and is_correct_password):
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Basic"},
)
# return credentials.username
return JSONResponse(content="Welcome to Task Management System", status_code=200)
app.include_router(user.router)
app.include_router(reminder.router)
app.include_router(task.router)
app.include_router(category.router)