Spaces:
Runtime error
Runtime error
| # main.py | |
| # Entry point of the entire application | |
| from dotenv import load_dotenv | |
| load_dotenv() # Load .env file for local development | |
| from fastapi import FastAPI | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import FileResponse | |
| import os | |
| from app.database import engine, Base | |
| from app.routers import categories, purchases, sales, dashboard | |
| # Create all database tables on startup | |
| Base.metadata.create_all(bind=engine) | |
| app = FastAPI( | |
| title="ShopTracker API", | |
| description="Simple shop management for small retail shops in India", | |
| version="1.0.0" | |
| ) | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"] | |
| ) | |
| # All 4 routers now active | |
| app.include_router(categories.router) | |
| app.include_router(purchases.router) | |
| app.include_router(sales.router) | |
| app.include_router(dashboard.router) | |
| def get_status(): | |
| return { | |
| "message": "ShopTracker API is running!", | |
| "version": "1.0.0", | |
| "status": "online" | |
| } | |
| # Try frontend inside backend first (Railway) | |
| # Fall back to frontend outside backend (local development) | |
| FRONTEND_DIR = os.path.join(os.path.dirname(__file__), "..", "frontend") | |
| FRONTEND_DIR = os.path.abspath(FRONTEND_DIR) | |
| if not os.path.exists(FRONTEND_DIR): | |
| FRONTEND_DIR = os.path.join(os.path.dirname(__file__), "..", "..", "frontend") | |
| FRONTEND_DIR = os.path.abspath(FRONTEND_DIR) | |
| if os.path.exists(FRONTEND_DIR): | |
| def serve_frontend(): | |
| return FileResponse(os.path.join(FRONTEND_DIR, "index.html")) | |
| def serve_css(): | |
| return FileResponse(os.path.join(FRONTEND_DIR, "style.css"), media_type="text/css") | |
| def serve_js(): | |
| return FileResponse(os.path.join(FRONTEND_DIR, "app.js"), media_type="application/javascript") | |
| def serve_manifest(): | |
| return FileResponse(os.path.join(FRONTEND_DIR, "manifest.json"), media_type="application/json") | |
| def serve_sw(): | |
| return FileResponse(os.path.join(FRONTEND_DIR, "sw.js"), media_type="application/javascript") | |
| app.mount("/static", StaticFiles(directory=FRONTEND_DIR), name="static") | |