Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (56 loc) · 1.77 KB
/
Copy pathmain.py
File metadata and controls
72 lines (56 loc) · 1.77 KB
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
import os
os.environ["TESTING"] = "0" # normal run
from contextlib import asynccontextmanager
from logging import getLogger
import uvicorn
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from app.controllers_instances import *
from app.database.database import Base, engine
from app.middleware.error_middleware import error_handler
from app.models.errors.app_error import AppError
from app.routes import (
accounting_route,
auth_route,
customer_route,
orders_route,
products_route,
returns_route,
sales_route,
user_route,
dashboard_route,
)
logger = getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
async with engine.begin() as conn:
logger.info("Creating database tables...")
await conn.run_sync(Base.metadata.create_all)
yield
await engine.dispose()
app = FastAPI(title="EZShop", lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# register routers
app.include_router(auth_route.router)
app.include_router(user_route.router)
app.include_router(customer_route.router)
app.include_router(products_route.router)
app.include_router(sales_route.router)
app.include_router(orders_route.router)
app.include_router(accounting_route.router)
app.include_router(returns_route.router)
app.include_router(dashboard_route.router)
app.add_exception_handler(AppError, error_handler)
app.add_exception_handler(Exception, error_handler)
# simple root
@app.get("/")
def read_root():
return {"message": "FastAPI MVC Demo"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)