-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
102 lines (77 loc) · 2.19 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import secrets
import string
from argon2 import PasswordHasher
from dotenv import load_dotenv
from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from backend.config import Config, Session
from backend.database import Users, init_db
from backend.utils import UnicornException
# env
load_dotenv()
# config
conf = Session.config = Config()
app = FastAPI()
# CORS
origins = [
"*",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# plugins
from backend.plugins import auth, menu, orders, products, subcategories, users
app.include_router(auth.router)
app.include_router(menu.router)
app.include_router(orders.router)
app.include_router(products.router)
app.include_router(subcategories.router)
app.include_router(users.router)
# db
init_db(app)
# error
@app.exception_handler(UnicornException)
async def unicorn_exception_handler(_: Request, exc: UnicornException):
return JSONResponse(
status_code=exc.status,
content={
"error": True,
"message": exc.message
}
)
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(_: Request, exc: RequestValidationError):
detail = exc.errors()
message = "Error in:\n"
message += "\n".join([
f"{' -> '.join(x['loc'])}:\n {x['msg']}"
for x in detail
])
return JSONResponse(
status_code=422,
content={
"error": True,
"message": message,
"detail": detail
}
)
# creation admin user if not exist
@app.on_event("startup")
async def startup_event():
if not await Users.filter(role="admin").exists():
alphabet = string.ascii_letters + string.digits
password = "".join(secrets.choice(alphabet) for _ in range(8))
ph = PasswordHasher()
await Users(
username="admin",
password=ph.hash(password),
role="admin"
).save()
print("Username: admin")
print("Password:", password)