-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcarsharing.py
More file actions
56 lines (41 loc) · 1.33 KB
/
carsharing.py
File metadata and controls
56 lines (41 loc) · 1.33 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
from contextlib import asynccontextmanager
import uvicorn
from fastapi import FastAPI, Request, status
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from sqlmodel import SQLModel
from db import engine
from routers import cars, web, auth
from routers.cars import BadTripException
@asynccontextmanager
async def lifespan(app: FastAPI):
SQLModel.metadata.create_all(engine)
yield
app = FastAPI(title="Car Sharing", lifespan=lifespan)
app.include_router(cars.router)
app.include_router(web.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.exception_handler(BadTripException)
async def unicorn_exception_handler(request: Request, exc: BadTripException):
return JSONResponse(
status_code=status.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)