98 lines
2.8 KiB
Python
98 lines
2.8 KiB
Python
import logging
|
|
import sys
|
|
from app.routers import fixstuff_auto
|
|
from fastapi import FastAPI
|
|
from app.routers import main, delivery, confirm, fixstuff_customer
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
import os
|
|
from config import load_config
|
|
from sqlalchemy import create_engine, text
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
|
|
ApplicationConfig = load_config()
|
|
|
|
# Configure logging
|
|
def setup_logging():
|
|
"""Configure structured logging for the application."""
|
|
log_level = logging.DEBUG if ApplicationConfig.CURRENT_SETTINGS != 'PRODUCTION' else logging.INFO
|
|
|
|
formatter = logging.Formatter(
|
|
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
datefmt='%Y-%m-%d %H:%M:%S'
|
|
)
|
|
|
|
root_logger = logging.getLogger()
|
|
root_logger.setLevel(log_level)
|
|
root_logger.handlers.clear()
|
|
|
|
console_handler = logging.StreamHandler(sys.stdout)
|
|
console_handler.setLevel(log_level)
|
|
console_handler.setFormatter(formatter)
|
|
root_logger.addHandler(console_handler)
|
|
|
|
logging.getLogger('uvicorn.access').setLevel(logging.WARNING)
|
|
|
|
return logging.getLogger('eamco_auto_api')
|
|
|
|
logger = setup_logging()
|
|
|
|
# Database setup
|
|
engine = create_engine(ApplicationConfig.SQLALCHEMY_DATABASE_URI)
|
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
|
|
|
def check_db_connection():
|
|
"""
|
|
Test database connectivity.
|
|
"""
|
|
try:
|
|
db = SessionLocal()
|
|
db.execute(text("SELECT 1"))
|
|
db.close()
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
app.include_router(main.router)
|
|
app.include_router(delivery.router)
|
|
app.include_router(confirm.router)
|
|
app.include_router(fixstuff_auto.router)
|
|
app.include_router(fixstuff_customer.router)
|
|
|
|
# print(ApplicationConfig.origins)
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=ApplicationConfig.origins,
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
|
|
|
|
|
|
@app.get("/")
|
|
def read_root():
|
|
return {"Status": "Auto Service is online"}
|
|
|
|
@app.on_event("startup")
|
|
async def startup_event():
|
|
"""Application startup - log configuration and test DB connection."""
|
|
logger.info("🚀 eamco_auto_api STARTING")
|
|
mode = ApplicationConfig.CURRENT_SETTINGS.upper()
|
|
if mode in ['DEVELOPMENT', 'DEV']:
|
|
logger.info("🤖🤖🤖🤖🤖 Mode: Development 🤖🤖🤖🤖🤖")
|
|
elif mode in ['PRODUCTION', 'PROD']:
|
|
logger.info("💀💀💀💀💀💀💀💀💀💀 ⚠️ WARNING PRODUCTION 💀💀💀💀💀💀💀💀💀💀")
|
|
logger.info(f"DB: {ApplicationConfig.SQLALCHEMY_DATABASE_URI[:30]}...")
|
|
logger.info(f"CORS: {len(ApplicationConfig.origins)} origins configured")
|
|
|
|
# Test database connection
|
|
if check_db_connection():
|
|
logger.info("DB Connection: ✅ OK")
|
|
else:
|
|
logger.info("DB Connection: ❌ FAILED")
|