-
Notifications
You must be signed in to change notification settings - Fork 48
/
carsharing.py
53 lines (41 loc) · 1.32 KB
/
carsharing.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
import uvicorn
from fastapi import FastAPI
from fastapi import Request
from fastapi.middleware.cors import CORSMiddleware
from sqlmodel import SQLModel
from starlette.responses import JSONResponse
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
from db import engine
from routers import cars, web, auth
from routers.cars import BadTripException
app = FastAPI(title="Car Sharing")
app.include_router(web.router)
app.include_router(cars.router)
app.include_router(auth.router)
origins = [
"http://localhost:8000",
"http://localhost:8080",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.on_event("startup")
def on_startup():
SQLModel.metadata.create_all(engine)
@app.exception_handler(BadTripException)
async def unicorn_exception_handler(request: Request, exc: BadTripException):
return JSONResponse(
status_code=HTTP_422_UNPROCESSABLE_ENTITY,
content={"message": "Bad Trip"},
)
# @app.middleware("http")
# async def add_cars_cookie(request: Request, call_next):
# response = await call_next(request)
# response.set_cookie(key="cars_cookie", value="you_visited_the_carsharing_app")
# return response
if __name__ == "__main__":
uvicorn.run("carsharing:app", reload=True)