Initial commit
This commit is contained in:
commit
6b4f1e2eb4
20 files changed
+806
No files matched your search
@@ -0,0 +1,13 @@
|
||||
from enum import StrEnum
|
||||
|
||||
from service_auth import KeySource, ServiceAuth
|
||||
|
||||
|
||||
class Caller(StrEnum):
|
||||
GATEWAY = "gateway"
|
||||
SCHEDULER = "scheduler"
|
||||
|
||||
auth = ServiceAuth({
|
||||
Caller.GATEWAY: KeySource("GATEWAY_KEY", "/etc/secrets/gateway_key"),
|
||||
Caller.SCHEDULER: KeySource("SCHEDULER_KEY", "/etc/secrets/scheduler_key"),
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
from typing import ClassVar
|
||||
|
||||
|
||||
class GenericError(Exception):
|
||||
"""Base class for every GENERIC error the API can explain to a client."""
|
||||
status_code: ClassVar[int] = 502
|
||||
code: ClassVar[str] = "sm_upstream_error"
|
||||
client_visible: ClassVar[bool] = False
|
||||
|
||||
@property
|
||||
def client_message(self) -> str:
|
||||
return str(self) if self.client_visible else "The GENERIC entry could not be checked right now, please try again later"
|
||||
@property
|
||||
def headers(self) -> dict[str, str]:
|
||||
"""Extra HTTP headers the proxy should send with this error."""
|
||||
return {}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.middleware.base import BaseHTTPMiddleware
|
||||
|
||||
from metrics_server import REQUEST_COUNTER
|
||||
from generic_errors import GenericError
|
||||
from simple_logger_handler import setup_logger
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
|
||||
async def repository_error_handler(request: Request, exc: GenericError):
|
||||
log = logger.warning if exc.client_visible else logger.error
|
||||
log(f"[FastAPI] {request.method} {request.url.path} failed: {exc}")
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
headers=exc.headers,
|
||||
content={"code": exc.code, "detail": exc.client_message},
|
||||
)
|
||||
|
||||
|
||||
async def validation_error_handler(request: Request, exc: RequestValidationError):
|
||||
errors = exc.errors()
|
||||
|
||||
for err in errors:
|
||||
original = (err.get("ctx") or {}).get("error")
|
||||
if isinstance(original, GenericError):
|
||||
return JSONResponse(
|
||||
status_code=original.status_code,
|
||||
headers=exc.headers,
|
||||
content={"code": original.code, "detail": original.client_message},
|
||||
)
|
||||
|
||||
# Problems in headers, path or query are the calling service's fault, not the user's
|
||||
if any(err["loc"][0] != "body" for err in errors):
|
||||
logger.error(f"[FastAPI] Invalid request from a calling service: {[(e['loc'], e['msg']) for e in errors]}")
|
||||
return JSONResponse(status_code=400, content={"code": "bad_request", "detail": "The request could not be processed"})
|
||||
|
||||
messages = []
|
||||
for err in errors:
|
||||
field = ".".join(str(p) for p in err["loc"] if p != "body")
|
||||
msg = err["msg"].removeprefix("Value error, ")
|
||||
messages.append(f"{field}: {msg}" if field else msg)
|
||||
return JSONResponse(status_code=422, content={"code": "validation_error", "detail": "; ".join(messages)})
|
||||
|
||||
|
||||
async def prometheus_middleware(request: Request, call_next):
|
||||
logger.debug(f"[FastAPI] Incoming request: {request.method} {request.url.path}")
|
||||
status = 500
|
||||
try:
|
||||
response = await call_next(request)
|
||||
status = response.status_code
|
||||
logger.debug(f"[FastAPI] Request processed with status {status}")
|
||||
except Exception as e:
|
||||
logger.error(f"[FastAPI] Exception occurred: {e}", exc_info=True)
|
||||
raise
|
||||
finally:
|
||||
REQUEST_COUNTER.labels(request.method, request.url.path, status).inc()
|
||||
logger.debug(f"[FastAPI] Counter incremented for {request.method} {request.url.path} [{status}]")
|
||||
return response
|
||||
|
||||
|
||||
def register_handlers(app: FastAPI) -> None:
|
||||
"""Attach the error handlers and the metrics middleware to the app."""
|
||||
app.add_exception_handler(GenericError, repository_error_handler)
|
||||
app.add_exception_handler(RequestValidationError, validation_error_handler)
|
||||
app.add_middleware(BaseHTTPMiddleware, dispatch=prometheus_middleware)
|
||||
+78
@@ -0,0 +1,78 @@
|
||||
import asyncio
|
||||
import os
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
import uvicorn
|
||||
from fastapi import Depends, FastAPI
|
||||
|
||||
from auth_config import Caller, auth
|
||||
from db_module import (
|
||||
close_connection_pool,
|
||||
create_connection_pool,
|
||||
start_healthcheck_thread,
|
||||
)
|
||||
from handlers import register_handlers
|
||||
from metrics_server import metrics_api
|
||||
from routers.scheduler import scheduler_router
|
||||
from routers.user import user_router
|
||||
from secret_manager import cleanup_secret_files
|
||||
from send_notifications import NotificationClient
|
||||
from simple_logger_handler import LOG_LEVEL, setup_logger
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
logger.info("[App] Starting application...")
|
||||
app.state.notifier = NotificationClient()
|
||||
logger.info("[DB] Creating MySQL connection pool...")
|
||||
create_connection_pool("seadexPool")
|
||||
|
||||
start_healthcheck_thread()
|
||||
logger.info("[DB] MySQL healthcheck thread started.")
|
||||
|
||||
SECRET_PATHS = frozenset({
|
||||
"/etc/secrets/api_key",
|
||||
"/etc/secrets/db_username",
|
||||
"/etc/secrets/db_password"
|
||||
})
|
||||
cleanup_secret_files(SECRET_PATHS)
|
||||
|
||||
yield
|
||||
logger.info("[App] Closing MySQL connection pool...")
|
||||
close_connection_pool()
|
||||
logger.info("[App] Shutdown complete.")
|
||||
|
||||
|
||||
|
||||
api = FastAPI(
|
||||
title="Service GENERIC",
|
||||
description="GENERICGENERICGENERICGENERICGENERIC",
|
||||
version="1.0.0",
|
||||
lifespan=lifespan,
|
||||
dependencies=[Depends(auth.authenticate)],
|
||||
)
|
||||
|
||||
register_handlers(api)
|
||||
api.include_router(user_router)
|
||||
api.include_router(scheduler_router)
|
||||
|
||||
|
||||
async def start_servers():
|
||||
logger.info("[Server] Starting main API (port 5000) and metrics server (port 9000)...")
|
||||
logger.info("[Server] Build commit id=%s", os.getenv("COMMIT_ID", "unknown"))
|
||||
|
||||
auth.load_keys(api, Caller.GATEWAY, Caller.SCHEDULER,)
|
||||
cleanup_secret_files(auth.key_paths(Caller.GATEWAY, Caller.SCHEDULER,))
|
||||
|
||||
config_main = uvicorn.Config(api, host="0.0.0.0", port=5000, log_level=LOG_LEVEL.lower(),log_config=None)
|
||||
config_metrics = uvicorn.Config(metrics_api, host="0.0.0.0", port=9000, log_level=LOG_LEVEL.lower(),log_config=None)
|
||||
|
||||
server_main = uvicorn.Server(config_main)
|
||||
server_metrics = uvicorn.Server(config_metrics)
|
||||
|
||||
await asyncio.gather(server_main.serve(), server_metrics.serve())
|
||||
logger.info("[Server] Both servers have stopped.")
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(start_servers())
|
||||
@@ -0,0 +1,10 @@
|
||||
from fastapi import FastAPI, Response
|
||||
from prometheus_client import CONTENT_TYPE_LATEST, Counter, generate_latest
|
||||
|
||||
metrics_api = FastAPI(title="Metrics Server", description="Prometheus metrics endpoint")
|
||||
|
||||
REQUEST_COUNTER = Counter("http_requests_total", "Total HTTP Requests", ["method", "endpoint", "status"])
|
||||
|
||||
@metrics_api.get("/metrics")
|
||||
async def metrics():
|
||||
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)
|
||||
@@ -0,0 +1,22 @@
|
||||
from datetime import datetime
|
||||
|
||||
from pydantic import BaseModel, Field, computed_field
|
||||
|
||||
|
||||
class GenericEntry(BaseModel):
|
||||
name: str
|
||||
custom_name: str | None = None
|
||||
published: datetime | None = None
|
||||
|
||||
class Counts(BaseModel):
|
||||
active: int
|
||||
inactive: int
|
||||
|
||||
class GenericList(BaseModel):
|
||||
active: list[GenericEntry] = Field(default_factory=list)
|
||||
inactive: list[GenericEntry] = Field(default_factory=list)
|
||||
|
||||
@computed_field
|
||||
@property
|
||||
def counts(self) -> Counts:
|
||||
return Counts(active=len(self.active),inactive=len(self.inactive))
|
||||
Whitespace-only changes.
@@ -0,0 +1,16 @@
|
||||
|
||||
from fastapi import APIRouter, Depends, Request
|
||||
|
||||
from auth_config import Caller, auth
|
||||
from db_module import (
|
||||
get_db,
|
||||
)
|
||||
from simple_logger_handler import setup_logger
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
scheduler_router = APIRouter(dependencies=[Depends(auth.require(Caller.SCHEDULER))])
|
||||
|
||||
@scheduler_router.post("/update")
|
||||
def check_for_updates(request:Request, db=Depends(get_db)):
|
||||
return
|
||||
@@ -0,0 +1,42 @@
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
|
||||
from auth_config import Caller, auth
|
||||
from db_module import (
|
||||
get_db,
|
||||
)
|
||||
from models import GenericList
|
||||
from service_auth import UserId
|
||||
from simple_logger_handler import setup_logger
|
||||
|
||||
logger = setup_logger(__name__)
|
||||
|
||||
user_router = APIRouter(dependencies=[Depends(auth.require(Caller.GATEWAY))])
|
||||
|
||||
@user_router.post("/entry/add")
|
||||
def add_entry(
|
||||
user_id: UserId,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
return
|
||||
|
||||
@user_router.post("/entry/delete")
|
||||
def remove_anime(
|
||||
user_id: UserId,
|
||||
db=Depends(get_db)
|
||||
):
|
||||
return
|
||||
|
||||
@user_router.get("/entry/list", response_model=GenericList)
|
||||
def list_active_and_inactive_anime(
|
||||
user_id: UserId,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
return
|
||||
|
||||
@user_router.post("/entry/rename")
|
||||
def rename_entry(
|
||||
user_id: UserId,
|
||||
db=Depends(get_db),
|
||||
):
|
||||
return
|
||||
Reference in new issue
Block a user