Spaces:
Sleeping
Sleeping
File size: 1,730 Bytes
73268e9 | 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 | """Entry point FastAPI CodeBuddy."""
from contextlib import asynccontextmanager
from typing import AsyncGenerator
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from slowapi import _rate_limit_exceeded_handler
from slowapi.errors import RateLimitExceeded
from api.routes import router as router_utama
from api.schemas import ResponsKesehatan
from api.teacher import router as router_guru
from utils.config import pengaturan
from utils.database import init_db
from utils.pembatas_kueri import pembatas_per_ip
@asynccontextmanager
async def lifespan(_app: FastAPI) -> AsyncGenerator[None, None]:
"""Inisialisasi DB saat startup."""
await init_db()
yield
app = FastAPI(
title=pengaturan.nama_aplikasi,
description=(
"API tutor pemrograman Python dengan OCR tulisan tangan, "
"sandbox eksekusi, dan LLM lokal via Ollama."
),
version="0.1.0",
lifespan=lifespan,
)
# Rate limiting state dan error handler
app.state.limiter = pembatas_per_ip
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
# CORS dengan daftar origin spesifik
app.add_middleware(
CORSMiddleware,
allow_origins=pengaturan.allowed_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(router_utama, prefix="/api")
app.include_router(router_guru, prefix="/api")
@app.get("/health", response_model=ResponsKesehatan, tags=["health"])
async def cek_kesehatan() -> ResponsKesehatan:
"""Probe kesehatan untuk load balancer dan demo hackathon."""
return ResponsKesehatan()
@app.get("/", tags=["root"])
async def akar() -> dict[str, str]:
return {"docs": "/docs", "health": "/health"}
|