/ gellary / backend / app.py
swadhinbiswas fix model · 3/26/2024
8d37f99
59 lines · 1.3 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
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from fastapi.exceptions import RequestValidationError
from pydantic import ValidationError
from starlette.exceptions import HTTPException
from starlette.status import HTTP_422_UNPROCESSABLE_ENTITY
from typing import List
from model.usemodel import UseModel
from model.imagemodel import ImageModel
from schemas.imageschema import ImageBase,ImageOut,Update
from api.v1.handelars.handelars import router
from beanie import init_beanie, Document
from motor.motor_asyncio import AsyncIOMotorClient
from contextlib import asynccontextmanager


async def init():
  client = AsyncIOMotorClient(
        "mongodb+srv://test1:[email protected]/?retryWrites=true&w=majority&appName=testbatch"
    )
  
  await init_beanie(database=client.test, document_models=[ImageModel])
  
  
@asynccontextmanager
async def lifespan(app: FastAPI):
    await init()
    try:
        yield
    finally:
        pass



app = FastAPI(lifespan=lifespan)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


    
app.include_router(router,prefix="/api/v1")

  


  
  

if __name__ == "__main__":
  import uvicorn
  uvicorn.run(app,host="localhost",port=8000)