Initial commit: Add EAMCO Service API
- Add FastAPI service for managing oil delivery services - Implement service scheduling and management endpoints - Add customer service history tracking - Include database models for services, customers, and auto-delivery - Add authentication and authorization middleware - Configure Docker support for local, dev, and prod environments - Add comprehensive .gitignore for Python projects
This commit is contained in:
116
main.py
Normal file
116
main.py
Normal file
@@ -0,0 +1,116 @@
|
||||
import logging
|
||||
import sys
|
||||
import uuid
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
from config import load_config
|
||||
from sqlalchemy import create_engine, text
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.routers import service
|
||||
|
||||
|
||||
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_service')
|
||||
|
||||
|
||||
logger = setup_logging()
|
||||
|
||||
# Database setup with connection pooling
|
||||
engine = create_engine(
|
||||
ApplicationConfig.SQLALCHEMY_DATABASE_URI,
|
||||
pool_pre_ping=True,
|
||||
pool_size=5,
|
||||
max_overflow=10,
|
||||
pool_recycle=3600,
|
||||
)
|
||||
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()
|
||||
|
||||
|
||||
# Request ID middleware for request tracking/correlation
|
||||
class RequestIDMiddleware(BaseHTTPMiddleware):
|
||||
async def dispatch(self, request: Request, call_next):
|
||||
request_id = request.headers.get("X-Request-ID") or str(uuid.uuid4())[:8]
|
||||
request.state.request_id = request_id
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-ID"] = request_id
|
||||
return response
|
||||
|
||||
|
||||
app.add_middleware(RequestIDMiddleware)
|
||||
|
||||
app.include_router(service.router)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=ApplicationConfig.CORS_ALLOWED_ORIGINS,
|
||||
allow_credentials=True,
|
||||
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||||
allow_headers=["Authorization", "Content-Type", "Accept", "Origin", "X-Requested-With"],
|
||||
)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
def health_check():
|
||||
return {"ok": True, "service": "eamco_service"}
|
||||
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
"""Application startup - log configuration and test DB connection."""
|
||||
logger.info("eamco_service STARTING")
|
||||
mode = ApplicationConfig.CURRENT_SETTINGS.upper()
|
||||
if mode in ['DEVELOPMENT', 'DEV']:
|
||||
logger.info("Mode: Development")
|
||||
elif mode in ['PRODUCTION', 'PROD']:
|
||||
logger.info("WARNING PRODUCTION")
|
||||
# Sanitize DB URI to avoid logging credentials
|
||||
db_uri = ApplicationConfig.SQLALCHEMY_DATABASE_URI
|
||||
if '@' in db_uri:
|
||||
db_uri = db_uri.split('@')[-1]
|
||||
logger.info(f"DB: ...@{db_uri[:50]}")
|
||||
logger.info(f"CORS: {len(ApplicationConfig.CORS_ALLOWED_ORIGINS)} origins configured")
|
||||
|
||||
# Test database connection
|
||||
if check_db_connection():
|
||||
logger.info("DB Connection: OK")
|
||||
else:
|
||||
logger.info("DB Connection: FAILED")
|
||||
Reference in New Issue
Block a user