Spaces:
Sleeping
Sleeping
from fastapi import FastAPI, Request | |
from fastapi.staticfiles import StaticFiles | |
from fastapi.templating import Jinja2Templates | |
from .routes import invoices | |
from app.db.database import init_db | |
import os | |
from fastapi.middleware.cors import CORSMiddleware | |
from dotenv import load_dotenv | |
# Load environment variables | |
load_dotenv() | |
# Get the absolute path to the app directory | |
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
app = FastAPI( | |
title="Invoice Generator", | |
description="API for generating invoices", | |
version="1.0.0" | |
) | |
# Configure CORS | |
app.add_middleware( | |
CORSMiddleware, | |
allow_origins=["*"], # In production, replace with your frontend domain | |
allow_credentials=True, | |
allow_methods=["*"], | |
allow_headers=["*"], | |
) | |
# Mount static files with absolute path | |
app.mount("/static", StaticFiles(directory=os.path.join(BASE_DIR, "app/static")), name="static") | |
# Templates with absolute path | |
templates = Jinja2Templates(directory=os.path.join(BASE_DIR, "app/templates")) | |
# Include routers | |
app.include_router(invoices.router) | |
async def startup_event(): | |
await init_db() | |
# Root endpoint to serve the HTML page | |
async def root(request: Request): | |
return templates.TemplateResponse("index.html", {"request": request}) | |
# Health check endpoint | |
async def health_check(): | |
return {"status": "healthy"} |