66 lines
1.5 KiB
Python
66 lines
1.5 KiB
Python
from fastapi import FastAPI, HTTPException, Depends
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from db import MongoDB, get_database
|
|
from config import get_settings
|
|
from routers import entries, users
|
|
from contextlib import asynccontextmanager
|
|
|
|
settings = get_settings()
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI):
|
|
# Startup
|
|
MongoDB.connect_db()
|
|
yield
|
|
# Shutdown
|
|
MongoDB.close_db()
|
|
|
|
app = FastAPI(
|
|
title="Grateful Journal API",
|
|
description="Backend API for Grateful Journal - private journaling app",
|
|
version="0.1.0",
|
|
lifespan=lifespan
|
|
)
|
|
|
|
# CORS middleware (MUST be before routes)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["http://localhost:8000",
|
|
"http://127.0.0.1:8000", "http://localhost:5173"],
|
|
allow_credentials=True,
|
|
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(users.router, prefix="/api/users", tags=["users"])
|
|
app.include_router(entries.router, prefix="/api/entries", tags=["entries"])
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
return {
|
|
"status": "ok",
|
|
"environment": settings.environment,
|
|
"api_version": "0.1.0"
|
|
}
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
return {
|
|
"message": "Grateful Journal API",
|
|
"version": "0.1.0",
|
|
"docs": "/docs"
|
|
}
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
uvicorn.run(
|
|
"main:app",
|
|
host="0.0.0.0",
|
|
port=settings.api_port,
|
|
reload=settings.environment == "development"
|
|
)
|