96 lines
2.8 KiB
Python
96 lines
2.8 KiB
Python
from fastapi import FastAPI
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
from app.config import get_settings
|
|
from app.database import init_db
|
|
from app.api import species, images, jobs, exports, stats, sources
|
|
|
|
settings = get_settings()
|
|
|
|
app = FastAPI(
|
|
title="PlantGuideScraper API",
|
|
description="Web scraper interface for houseplant image collection",
|
|
version="1.0.0",
|
|
)
|
|
|
|
# CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(species.router, prefix="/api/species", tags=["Species"])
|
|
app.include_router(images.router, prefix="/api/images", tags=["Images"])
|
|
app.include_router(jobs.router, prefix="/api/jobs", tags=["Jobs"])
|
|
app.include_router(exports.router, prefix="/api/exports", tags=["Exports"])
|
|
app.include_router(stats.router, prefix="/api/stats", tags=["Stats"])
|
|
app.include_router(sources.router, prefix="/api/sources", tags=["Sources"])
|
|
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""Initialize database on startup."""
|
|
init_db()
|
|
|
|
|
|
@app.get("/health")
|
|
async def health_check():
|
|
"""Health check endpoint."""
|
|
return {"status": "healthy", "service": "plant-scraper"}
|
|
|
|
|
|
@app.get("/api/debug")
|
|
async def debug_check():
|
|
"""Debug endpoint - checks database connection."""
|
|
import time
|
|
from app.database import SessionLocal
|
|
from app.models import Species, Image
|
|
|
|
results = {"status": "checking", "checks": {}}
|
|
|
|
# Check 1: Can we create a session?
|
|
try:
|
|
start = time.time()
|
|
db = SessionLocal()
|
|
results["checks"]["session_create"] = {"ok": True, "ms": int((time.time() - start) * 1000)}
|
|
except Exception as e:
|
|
results["checks"]["session_create"] = {"ok": False, "error": str(e)}
|
|
results["status"] = "error"
|
|
return results
|
|
|
|
# Check 2: Simple query - count species
|
|
try:
|
|
start = time.time()
|
|
count = db.query(Species).count()
|
|
results["checks"]["species_count"] = {"ok": True, "count": count, "ms": int((time.time() - start) * 1000)}
|
|
except Exception as e:
|
|
results["checks"]["species_count"] = {"ok": False, "error": str(e)}
|
|
results["status"] = "error"
|
|
db.close()
|
|
return results
|
|
|
|
# Check 3: Count images
|
|
try:
|
|
start = time.time()
|
|
count = db.query(Image).count()
|
|
results["checks"]["image_count"] = {"ok": True, "count": count, "ms": int((time.time() - start) * 1000)}
|
|
except Exception as e:
|
|
results["checks"]["image_count"] = {"ok": False, "error": str(e)}
|
|
results["status"] = "error"
|
|
db.close()
|
|
return results
|
|
|
|
db.close()
|
|
results["status"] = "healthy"
|
|
return results
|
|
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root endpoint."""
|
|
return {"message": "PlantGuideScraper API", "docs": "/docs"}
|