codewithRiz commited on
Commit
21e8ee7
Β·
1 Parent(s): 5f084a6

fix filter label

Browse files
Files changed (2) hide show
  1. api/main.py +23 -13
  2. api/view_image.py +1 -1
api/main.py CHANGED
@@ -4,14 +4,20 @@ from fastapi.middleware.cors import CORSMiddleware
4
  from fastapi.staticfiles import StaticFiles
5
  import logging
6
  from pathlib import Path
 
 
 
 
 
7
 
8
  # ----------------- Import Routers -----------------
9
  from .detection import router as detection_router
10
  from .auth import router as auth_router
11
- from .camera import router as camera_router # Camera router
12
- from .config import UPLOAD_DIR # Folder where images are saved
13
  from .analytics import router as analytics_router
14
  from .view_image import router as view_images_router
 
15
  # ---------------- Logging Setup -----------------
16
  logger = logging.getLogger("ServerLogger")
17
  logging.basicConfig(
@@ -22,38 +28,43 @@ logging.basicConfig(
22
  # Ensure UPLOAD_DIR exists
23
  Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
24
 
 
 
 
 
 
 
 
 
25
  # ---------------- Create App -----------------
26
  def create_app() -> FastAPI:
27
  app = FastAPI(title="Wildlife Detection & Camera API Server")
28
 
29
- # ---- CORS Configuration ----
30
  app.add_middleware(
31
  CORSMiddleware,
32
- allow_origins=[
33
- 'https://www.daleandcompany.com','http://127.0.0.1:8080'
34
- ],
35
  allow_credentials=True,
36
  allow_methods=["*"],
37
  allow_headers=["*"],
38
  )
39
 
40
- # ---- Mount static folder for uploaded images ----
41
  app.mount("/user_data", StaticFiles(directory=UPLOAD_DIR), name="user_data")
42
  logger.info(f"Static folder mounted at /user_data -> {UPLOAD_DIR}")
43
 
44
- # ---- Include Routers ----
45
  app.include_router(auth_router, prefix="/auth", tags=["Auth"])
46
  app.include_router(camera_router, prefix="/api", tags=["Camera"])
47
  app.include_router(detection_router, prefix="/api/detection", tags=["Detection"])
48
- app.include_router(analytics_router,prefix="/api", tags=["Analytics"])
49
  app.include_router(view_images_router, prefix="/api", tags=["Images"])
50
 
51
- # ---- Health Check / Startup Event ----
52
  @app.on_event("startup")
53
  async def startup_check():
54
  logger.info("Server started successfully. All routers are active.")
55
 
56
- # ---- Root Endpoint ----
57
  @app.get("/")
58
  def root():
59
  return {
@@ -63,10 +74,9 @@ def create_app() -> FastAPI:
63
  "camera": "/api/camera/...",
64
  "detection": "/api/detection/...",
65
  "analytics": "/api/analytics/..."
66
-
67
  }
68
  }
69
 
70
  return app
71
 
72
- app = create_app()
 
4
  from fastapi.staticfiles import StaticFiles
5
  import logging
6
  from pathlib import Path
7
+ from dotenv import load_dotenv
8
+ import os
9
+
10
+ # Load .env
11
+ load_dotenv()
12
 
13
  # ----------------- Import Routers -----------------
14
  from .detection import router as detection_router
15
  from .auth import router as auth_router
16
+ from .camera import router as camera_router
17
+ from .config import UPLOAD_DIR
18
  from .analytics import router as analytics_router
19
  from .view_image import router as view_images_router
20
+
21
  # ---------------- Logging Setup -----------------
22
  logger = logging.getLogger("ServerLogger")
23
  logging.basicConfig(
 
28
  # Ensure UPLOAD_DIR exists
29
  Path(UPLOAD_DIR).mkdir(parents=True, exist_ok=True)
30
 
31
+ # ---------------- CORS Config -----------------
32
+ raw_origins = os.getenv("ALLOWED_ORIGINS", "")
33
+
34
+ # Clean + split safely
35
+ allowed_origins = [origin.strip() for origin in raw_origins.split(",") if origin.strip()]
36
+
37
+ print("CORS Origins:", allowed_origins)
38
+
39
  # ---------------- Create App -----------------
40
  def create_app() -> FastAPI:
41
  app = FastAPI(title="Wildlife Detection & Camera API Server")
42
 
 
43
  app.add_middleware(
44
  CORSMiddleware,
45
+ allow_origins=allowed_origins,
 
 
46
  allow_credentials=True,
47
  allow_methods=["*"],
48
  allow_headers=["*"],
49
  )
50
 
51
+ # ---- Static Files ----
52
  app.mount("/user_data", StaticFiles(directory=UPLOAD_DIR), name="user_data")
53
  logger.info(f"Static folder mounted at /user_data -> {UPLOAD_DIR}")
54
 
55
+ # ---- Routers ----
56
  app.include_router(auth_router, prefix="/auth", tags=["Auth"])
57
  app.include_router(camera_router, prefix="/api", tags=["Camera"])
58
  app.include_router(detection_router, prefix="/api/detection", tags=["Detection"])
59
+ app.include_router(analytics_router, prefix="/api", tags=["Analytics"])
60
  app.include_router(view_images_router, prefix="/api", tags=["Images"])
61
 
62
+ # ---- Startup ----
63
  @app.on_event("startup")
64
  async def startup_check():
65
  logger.info("Server started successfully. All routers are active.")
66
 
67
+ # ---- Root ----
68
  @app.get("/")
69
  def root():
70
  return {
 
74
  "camera": "/api/camera/...",
75
  "detection": "/api/detection/...",
76
  "analytics": "/api/analytics/..."
 
77
  }
78
  }
79
 
80
  return app
81
 
82
+ app = create_app()
api/view_image.py CHANGED
@@ -47,7 +47,7 @@ def view_images(
47
  existing_filenames = {item.path.split("/")[-1] for item in raw_files}
48
 
49
  # ── validate filter label ─────────────────────────────────────
50
- valid_filters = {"buck", "doe", "mule", "whitetail"}
51
  filter_lower = filter_label.lower() if filter_label else None
52
  if filter_lower and filter_lower not in valid_filters:
53
  raise HTTPException(
 
47
  existing_filenames = {item.path.split("/")[-1] for item in raw_files}
48
 
49
  # ── validate filter label ─────────────────────────────────────
50
+ valid_filters = { "Doe", "Mule Bucks", "White Tail Bucks"}
51
  filter_lower = filter_label.lower() if filter_label else None
52
  if filter_lower and filter_lower not in valid_filters:
53
  raise HTTPException(