File size: 3,652 Bytes
6c9c901
 
54fe70d
 
6c9c901
 
 
 
 
 
6e2603f
6c9c901
54fe70d
 
 
 
 
 
 
 
6c9c901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e2603f
6c9c901
 
 
6e2603f
 
 
6c9c901
 
 
 
 
 
 
 
 
54fe70d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6c9c901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e2603f
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import logging
from contextlib import asynccontextmanager
import cloudinary
import sys

from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware

from .config import get_settings
from .database import get_collection, test_connection
from .routers import auth, incidents, policies

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(sys.stdout)
    ]
)
logger = logging.getLogger(__name__)


async def setup_database_indexes():
    """Set up database indexes with error handling."""
    try:
        # Test connection first
        connection_ok = await test_connection()
        if not connection_ok:
            logger.warning("Database connection failed - skipping index creation")
            return
        
        # Create indexes
        users = get_collection("users")
        incidents_collection = get_collection("incidents")
        policies_collection = get_collection("policies")
        
        await users.create_index("email", unique=True)
        await incidents_collection.create_index("created_at")
        await policies_collection.create_index("created_at")
        await policies_collection.create_index("status")
        await policies_collection.create_index("category")
        logger.info("Database indexes created successfully")
    except Exception as e:
        logger.warning(f"Database setup failed: {e}")
        logger.warning("Application will continue without database indexes")


@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    settings = get_settings()
    
    # Initialize Cloudinary
    try:
        # Use environment variables from settings
        cloudinary.config(
            cloud_name=settings.cloudinary_cloud_name,
            api_key=settings.cloudinary_api_key,
            api_secret=settings.cloudinary_api_secret,
            secure=True
        )
        logger.info("Cloudinary initialized successfully with cloud name: %s", settings.cloudinary_cloud_name)
    except Exception as e:
        logger.error(f"Cloudinary initialization failed: {e}")
    
    await setup_database_indexes()
    yield
    # Shutdown (if needed in future)


settings = get_settings()

app = FastAPI(title=settings.app_name, lifespan=lifespan)

allowed_origins = settings.allowed_origins
if isinstance(allowed_origins, str):
    allowed_origins = [origin.strip() for origin in allowed_origins.split(",") if origin.strip()]

# Ensure we have the allowed origins for development and production
if not allowed_origins:
    allowed_origins = ["*"]  # Fallback to allow all if not configured

app.add_middleware(
    CORSMiddleware,
    allow_origins=allowed_origins,
    allow_credentials=True,
    allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
    allow_headers=["*"],
    expose_headers=["*"],
)

# Debug: Log the allowed origins in startup
logger.info(f"CORS allowed origins: {allowed_origins}")


@app.get("/health")
async def health_check():
    connection_ok = await test_connection()
    if connection_ok:
        return {"status": "ok", "database": "connected"}
    else:
        return {"status": "degraded", "database": "disconnected"}


@app.get("/")
async def root():
    return {"message": "Marine Guard API", "status": "running"}


@app.options("/{path:path}")
async def options_handler(path: str):
    """Handle CORS preflight requests."""
    return {"message": "OK"}


app.include_router(auth.router, prefix="/api")
app.include_router(incidents.router, prefix="/api")
app.include_router(policies.router, prefix="/api")