Spaces:
Runtime error
Runtime error
File size: 3,915 Bytes
e7049f8 8067e4a f8b25ce e7049f8 4dbe421 9af3b3c e7049f8 f8b25ce e7049f8 4dbe421 e7049f8 e41d6e0 4dbe421 e7049f8 8067e4a b06b4e5 4dbe421 b06b4e5 9e34c5f b06b4e5 4dbe421 b06b4e5 8067e4a b9dc9fe 9af3b3c e941a76 f8b25ce a6a2032 f8b25ce e7049f8 f8b25ce e7049f8 4dbe421 f8b25ce 4dbe421 f8b25ce e7049f8 8067e4a e7049f8 8067e4a e7049f8 7ef9e24 |
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
from fastapi import FastAPI, Depends, HTTPException, status, File, UploadFile, Response
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy.ext.asyncio import AsyncSession
from typing import List
from typing import Annotated
# from transparent_background import Remover
import uvicorn,os
from pymongo import MongoClient, GEOSPHERE
from bson import ObjectId
from contextlib import asynccontextmanager
from pydantic import BaseModel, Field
from datetime import timedelta, datetime
from dotenv import dotenv_values
from app.api.v1.endpoints import user, auth, ocr, ocrtemplate, config
from app.db.base import *
from app.core.auth import *
# from app.router.user import *
from app.core.database import *
from app.core.middleware import BasicAuthMiddleware
# Load environment variables from .env file
dotenv_values(".env")
# Read environment variables
host = os.getenv("HOST", "0.0.0.0")
port = os.getenv("PORT", 8080)
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
# app.mongodb_client = MongoClient(mongodb_uri)
# app.database = app.mongodb_client[db_name]
logger.info("Connected to the MongoDB database!")
try:
await init_db()
# collections = app.database.list_collection_names()
# print(f"Collections in {db_name}: {collections}")
yield
except Exception as e:
logger.error(e)
app = FastAPI(lifespan=lifespan)
# app.add_middleware(BasicAuthMiddleware, username=os.getenv("fastapiusername"), password=os.getenv("fastapipassword"))
# Allow CORS for specific origin with credentials
origins = [
os.getenv("client")
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(user.router, prefix='/api/v1/user', tags=["User"])
app.include_router(ocrtemplate.router, prefix='/api/v1/ocrtemplate', tags=["OCR Template"])
app.include_router(ocr.router, prefix='/api/v1/ocr', tags=["OCR"])
app.include_router(auth.router, tags=["Auth"])
app.include_router(config.router, prefix='/api/v1/config', tags=["Config"])
class Destination(BaseModel):
name: str
description: str
location: Dict[str, Any]
accommodations: List[Dict[str, Any]]
activities: List[Dict[str, Any]]
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
@app.get("/")
async def root():
return {"greeting": "Hello, World!", "message": "This is a service to extract the first frame of a video!"}
@app.post("/login")
async def login_for_access_token(response: Response, db: AsyncSession = Depends(get_async_db), form_data: OAuth2PasswordRequestForm = Depends()):
user = await authenticate_user(db, form_data.username, form_data.password)
if not user:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Incorrect email or password",
headers={"WWW-Authenticate": "Bearer"},
)
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
access_token = await create_access_token(
data={"sub": user.email}, expires_delta=access_token_expires
)
print("Access token:", access_token)
# response.set_cookie("access_token", access_token, httponly=True)
# return "Login Successful"
return {"access_token": access_token, "token_type": "bearer"}
@app.get("/destinations", response_model=List[Destination])
async def get_destinations():
db = app.database['destinations']
destinations = list(db.find())
return destinations
# if __name__ == "__main__":
# uvicorn.run("app.main:app", host="0.0.0.0", port=port, log_level="info") |