shoptracker / app /main.py
saann's picture
ShopTracker V1 - FastAPI + PostgreSQL + Frontend
fd0007e
Raw
History Blame Contribute Delete
2.34 kB
# 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)
@app.get("/api/status")
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):
@app.get("/")
def serve_frontend():
return FileResponse(os.path.join(FRONTEND_DIR, "index.html"))
@app.get("/style.css")
def serve_css():
return FileResponse(os.path.join(FRONTEND_DIR, "style.css"), media_type="text/css")
@app.get("/app.js")
def serve_js():
return FileResponse(os.path.join(FRONTEND_DIR, "app.js"), media_type="application/javascript")
@app.get("/manifest.json")
def serve_manifest():
return FileResponse(os.path.join(FRONTEND_DIR, "manifest.json"), media_type="application/json")
@app.get("/sw.js")
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")