Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (63 loc) · 1.77 KB
/
Copy pathmain.py
File metadata and controls
77 lines (63 loc) · 1.77 KB
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
import logging
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.trustedhost import TrustedHostMiddleware
from fastapi.responses import JSONResponse
from config import get_settings
from database import Base, engine
from routers import posts_router
# Configure logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)
logger = logging.getLogger(__name__)
# Initialize settings
settings = get_settings()
# Create database tables
Base.metadata.create_all(bind=engine)
# Initialize FastAPI app
app = FastAPI(
title=settings.API_TITLE,
description=settings.API_DESCRIPTION,
version=settings.API_VERSION,
docs_url="/api/docs",
openapi_url="/api/openapi.json",
redoc_url="/api/redoc",
)
# Add middleware
app.add_middleware(
TrustedHostMiddleware,
allowed_hosts=["*"], # Update this for production
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Update this for production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Include routers
app.include_router(posts_router)
# Health check endpoint
@app.get("/api/health", tags=["health"])
def health_check() -> dict:
"""Health check endpoint."""
return {"status": "healthy", "version": settings.API_VERSION}
@app.get("/", tags=["root"])
def root() -> dict:
"""Root endpoint."""
return {
"message": f"Welcome to {settings.API_TITLE} API",
"version": settings.API_VERSION,
"docs_url": "/api/docs",
}
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"main:app",
host="0.0.0.0",
port=8000,
reload=settings.DEBUG,
log_level=settings.LOG_LEVEL.lower(),
)