- endpointy /workdays/add, is-workday, next, previous, between - /holidays, /countries (filtr eu), /markets, /health, /version - data svatku z knihovny holidays 0.109, vsech 27 zemi EU vcetne regionu - vychozi zeme CZ a cestina pres DEFAULT_COUNTRY a DEFAULT_LANGUAGE - vlastni vikend, extra_holidays a extra_workdays v requestu - testy logiky, API a testy proti realnym kalendarum - CLAUDE.md s povinnym overenim vysledku proti realite - dokumentace ve slozce documentation - .gitignore: vyjimka pro app/lib
122 lines
4.1 KiB
Python
122 lines
4.1 KiB
Python
"""Service entry point.
|
|
|
|
The application runs behind the AppFactory reverse proxy under
|
|
/apps/<app-id>. The prefix is stripped before the request reaches the
|
|
container, so only the OpenAPI document has to know about it. That is what
|
|
root_path does.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
from datetime import date
|
|
|
|
from fastapi import FastAPI, Request
|
|
from fastapi.responses import JSONResponse
|
|
|
|
from .config import get_settings
|
|
from .errors import ServiceError
|
|
from .logging_setup import setup_logging
|
|
from .routers import health as health_router
|
|
from .routers import holidays as holidays_router
|
|
from .routers import workdays as workdays_router
|
|
from .services.calendars import holidays_in_year, library_version, resolve_calendar
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
DESCRIPTION = """
|
|
Sluzba pocita s pracovnimi dny, vikendy a statnimi svatky. Pokryva vsechny
|
|
zeme EU vcetne regionu a dalsi zeme a financni kalendare. Data svatku dodava
|
|
knihovna python-holidays.
|
|
|
|
## Vychozi chovani
|
|
|
|
- zeme je CZ (promenna `DEFAULT_COUNTRY`), nazvy svatku cesky
|
|
(`DEFAULT_LANGUAGE`)
|
|
- pocitaji se jen statni svatky, kategorie `public`
|
|
- vikend je vikend dane zeme, v EU vzdy sobota a nedele
|
|
- data se zadavaji jako `YYYY-MM-DD` nebo `D.M.YYYY`, vraci se vzdy `YYYY-MM-DD`
|
|
|
|
## Pravidla vypoctu
|
|
|
|
- **add** vychozi den se nepocita. Ctvrtek 26.11.2026 plus 3 pracovni dny
|
|
v CZ je utery 1.12.2026. Zaporne `days` posouva zpet, `days=0` vrati vychozi
|
|
den beze zmeny.
|
|
- **next** a **previous** vraci nejblizsi pracovni den po nebo pred datem,
|
|
s `include_start=true` vrati vychozi den, kdyz je pracovni.
|
|
- **between** pocita oba krajni dny vcetne. Obracene poradi dat vrati zaporne
|
|
pocty.
|
|
- `extra_holidays` prida dny volna, `extra_workdays` udela z libovolneho dne
|
|
pracovni den a ma prednost.
|
|
- Kazdy kalendar pokryva jen urcity rozsah let (v GET /countries). Datum nebo
|
|
vysledek mimo nej vraci chybu `out_of_range`, sluzba nikdy nepocita
|
|
s chybejicimi svatky.
|
|
|
|
## Regiony
|
|
|
|
Nektere svatky plati jen v regionu, napriklad Bozi Telo v Bavorsku
|
|
(`country=DE®ion=BY`) nebo 26. prosinec v Alsasku a Mosele
|
|
(`country=FR®ion=57`). Bez regionu se pocitaji jen celostatni svatky.
|
|
|
|
## Financni kalendare
|
|
|
|
Parametr `market` nahradi zemi financnim kalendarem, napriklad `XECB` pro
|
|
platebni system TARGET2.
|
|
"""
|
|
|
|
# Cached settings are read once here so that a broken configuration stops the
|
|
# service at start instead of at the first request.
|
|
settings = get_settings()
|
|
setup_logging(settings.log_level)
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|
default = resolve_calendar("country", settings.default_country, None, None, settings.default_language)
|
|
holidays_in_year(default.spec, date.today().year)
|
|
logger.info(
|
|
"Service started",
|
|
extra={
|
|
"default_country": default.spec.code,
|
|
"language": default.spec.language,
|
|
"holidays_library": library_version(),
|
|
"root_path": settings.root_path,
|
|
},
|
|
)
|
|
yield
|
|
logger.info("Service stopped")
|
|
|
|
|
|
app = FastAPI(
|
|
title=settings.app_name,
|
|
version=settings.app_version,
|
|
description=DESCRIPTION,
|
|
root_path=settings.root_path,
|
|
lifespan=lifespan,
|
|
)
|
|
|
|
|
|
@app.exception_handler(ServiceError)
|
|
async def service_error_handler(request: Request, exc: ServiceError) -> JSONResponse:
|
|
logger.info(
|
|
"Request rejected",
|
|
extra={"error_code": exc.error_code, "path": request.url.path, "status_code": exc.status_code},
|
|
)
|
|
return JSONResponse(status_code=exc.status_code, content=exc.to_dict())
|
|
|
|
|
|
@app.exception_handler(Exception)
|
|
async def unhandled_error_handler(request: Request, exc: Exception) -> JSONResponse:
|
|
logger.exception("Unhandled error", extra={"path": request.url.path})
|
|
return JSONResponse(
|
|
status_code=500,
|
|
content={"error_code": "internal_error", "message": "Doslo k neocekavane chybe sluzby."},
|
|
)
|
|
|
|
|
|
app.include_router(health_router.router)
|
|
app.include_router(workdays_router.router)
|
|
app.include_router(holidays_router.router)
|