145 lines
3.8 KiB
Python
145 lines
3.8 KiB
Python
"""
|
|
Simplified FastAPI application entry point
|
|
For quick start and testing
|
|
"""
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.responses import JSONResponse
|
|
import structlog
|
|
from datetime import datetime
|
|
|
|
from app.config import get_settings
|
|
from app.handlers.webhook import router as webhook_router
|
|
from app.handlers.health import router as health_router
|
|
from app.handlers.logs import router as logs_router
|
|
from app.handlers.admin import router as admin_router
|
|
|
|
# Configure logging
|
|
structlog.configure(
|
|
processors=[
|
|
structlog.stdlib.filter_by_level,
|
|
structlog.stdlib.add_logger_name,
|
|
structlog.stdlib.add_log_level,
|
|
structlog.stdlib.PositionalArgumentsFormatter(),
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.processors.StackInfoRenderer(),
|
|
structlog.processors.format_exc_info,
|
|
structlog.processors.UnicodeDecoder(),
|
|
structlog.processors.JSONRenderer()
|
|
],
|
|
context_class=dict,
|
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
|
wrapper_class=structlog.stdlib.BoundLogger,
|
|
cache_logger_on_first_use=True,
|
|
)
|
|
|
|
logger = structlog.get_logger()
|
|
|
|
# Create FastAPI application
|
|
app = FastAPI(
|
|
title="Gitea Webhook Ambassador",
|
|
description="High-performance webhook service from Gitea to Jenkins",
|
|
version="1.0.0"
|
|
)
|
|
|
|
# Add CORS middleware
|
|
app.add_middleware(
|
|
CORSMiddleware,
|
|
allow_origins=["*"],
|
|
allow_credentials=True,
|
|
allow_methods=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
|
|
# Include routers
|
|
app.include_router(webhook_router)
|
|
app.include_router(health_router)
|
|
app.include_router(logs_router)
|
|
app.include_router(admin_router)
|
|
|
|
@app.get("/")
|
|
async def root():
|
|
"""Root path"""
|
|
return {
|
|
"name": "Gitea Webhook Ambassador",
|
|
"version": "1.0.0",
|
|
"description": "High-performance webhook service from Gitea to Jenkins",
|
|
"endpoints": {
|
|
"webhook": "/webhook/gitea",
|
|
"health": "/health",
|
|
"metrics": "/metrics",
|
|
"logs": "/api/logs",
|
|
"admin": "/api/admin"
|
|
}
|
|
}
|
|
|
|
@app.middleware("http")
|
|
async def log_requests(request: Request, call_next):
|
|
"""Request logging middleware"""
|
|
start_time = datetime.utcnow()
|
|
|
|
# Log request
|
|
logger.info(
|
|
"Request started",
|
|
method=request.method,
|
|
url=str(request.url),
|
|
client_ip=request.client.host if request.client else None
|
|
)
|
|
|
|
# Process request
|
|
response = await call_next(request)
|
|
|
|
# Calculate processing time
|
|
process_time = (datetime.utcnow() - start_time).total_seconds()
|
|
|
|
# Log response
|
|
logger.info(
|
|
"Request completed",
|
|
method=request.method,
|
|
url=str(request.url),
|
|
status_code=response.status_code,
|
|
process_time=process_time
|
|
)
|
|
|
|
# Add processing time to response header
|
|
response.headers["X-Process-Time"] = str(process_time)
|
|
|
|
return response
|
|
|
|
@app.exception_handler(Exception)
|
|
async def global_exception_handler(request: Request, exc: Exception):
|
|
"""Global exception handler"""
|
|
logger.error(
|
|
"Unhandled exception",
|
|
method=request.method,
|
|
url=str(request.url),
|
|
error=str(exc),
|
|
exc_info=True
|
|
)
|
|
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={
|
|
"error": "Internal server error",
|
|
"message": "An unexpected error occurred"
|
|
}
|
|
)
|
|
|
|
if __name__ == "__main__":
|
|
import uvicorn
|
|
settings = get_settings()
|
|
|
|
logger.info(
|
|
"Starting Gitea Webhook Ambassador",
|
|
host=settings.server.host,
|
|
port=settings.server.port,
|
|
version=settings.version
|
|
)
|
|
|
|
uvicorn.run(
|
|
"app.main_simple:app",
|
|
host=settings.server.host,
|
|
port=settings.server.port,
|
|
reload=settings.server.reload
|
|
) |