Spaces:
Sleeping
Sleeping
File size: 3,669 Bytes
2f1409f 3a19693 76089f2 2f1409f 3a19693 76089f2 3a19693 3c4a809 76089f2 3a19693 76089f2 2f1409f 76089f2 3a19693 3c4a809 3a19693 76089f2 3a19693 2f1409f 248dd6d 2f1409f 3c4a809 248dd6d 2f1409f 3a19693 | 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 | import os
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from fastapi.responses import FileResponse
from database import engine, Base, SessionLocal
from auth import get_current_user, hash_password
from routers import leads, imports, scraping, export, analytics
from routers.auth_router import router as auth_router
from routers.admin import router as admin_router
from routers.projects import router as projects_router
# Create all DB tables
Base.metadata.create_all(bind=engine)
app = FastAPI(
title="Market Research & Lead Generation API",
description="Automate market research, data collection, and lead generation",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Public routes (no auth required)
app.include_router(auth_router)
# Protected routes (require login)
_auth = [Depends(get_current_user)]
app.include_router(projects_router, dependencies=_auth)
app.include_router(leads.router, dependencies=_auth)
app.include_router(imports.router, dependencies=_auth)
app.include_router(scraping.router, dependencies=_auth)
app.include_router(analytics.router, dependencies=_auth)
app.include_router(admin_router) # admin routes have their own dependency
# Export uses token query param (for direct browser download links)
app.include_router(export.router)
@app.get("/health")
def health():
return {"status": "ok"}
# ββ Serve React frontend (production build) ββββββββββββββββββββββββββββββββββ
STATIC_DIR = os.path.join(os.path.dirname(__file__), "static")
if os.path.isdir(STATIC_DIR):
app.mount("/assets", StaticFiles(directory=os.path.join(STATIC_DIR, "assets")), name="assets")
@app.get("/")
def serve_index():
return FileResponse(os.path.join(STATIC_DIR, "index.html"))
# Catch-all: serve index.html for frontend routes (React Router)
# Excludes API paths so POST/PUT/DELETE requests are not intercepted
@app.get("/{full_path:path}")
def serve_spa(full_path: str):
if full_path.startswith(("auth/", "leads", "import", "scrape", "export", "analytics", "admin", "health", "projects")):
from fastapi import HTTPException
raise HTTPException(status_code=404)
file_path = os.path.join(STATIC_DIR, full_path)
if os.path.isfile(file_path):
return FileResponse(file_path)
return FileResponse(os.path.join(STATIC_DIR, "index.html"))
else:
@app.get("/")
def root():
return {"name": "Market Research & Lead Generation API", "version": "1.0.0", "docs": "/docs"}
# ββ Create default admin on first startup βββββββββββββββββββββββββββββββββ
def _create_default_admin():
from models import User
db = SessionLocal()
try:
if db.query(User).count() == 0:
admin = User(
username="admin",
full_name="Administrator",
hashed_password=hash_password("admin123"),
is_admin=True,
is_active=True,
)
db.add(admin)
db.commit()
print("=" * 50)
print(" Default admin account created:")
print(" Username : admin")
print(" Password : admin123")
print(" β Change this password in the Admin panel!")
print("=" * 50)
finally:
db.close()
_create_default_admin()
|