python_template_server.template_server¶
Template FastAPI server module.
Classes
|
Template FastAPI server. |
- class python_template_server.template_server.TemplateServer(package_name='python-template-server', api_prefix='/api', api_key_header_name='X-API-Key', config_filepath=PosixPath('/home/runner/work/python-template-server/python-template-server/configuration/config.json'), config=None, static_dir=PosixPath('/home/runner/work/python-template-server/python-template-server/static'))[source]¶
Template FastAPI server.
This class provides a template for building FastAPI servers with common features such as request logging, security headers and rate limiting.
Ensure you implement the setup_routes and validate_config methods in subclasses.
- __init__(package_name='python-template-server', api_prefix='/api', api_key_header_name='X-API-Key', config_filepath=PosixPath('/home/runner/work/python-template-server/python-template-server/configuration/config.json'), config=None, static_dir=PosixPath('/home/runner/work/python-template-server/python-template-server/static'))[source]¶
Initialize the TemplateServer.
- Parameters:
api_prefix (
str) – The API prefix for the serverapi_key_header_name (
str) – The API key header nameconfig_filepath (
Path) – Path to the configuration fileconfig (
TemplateServerConfig|None) – Optional pre-loaded configuration
- property static_dir_exists: bool¶
Check if the static directory exists.
- Return bool:
True if the static directory exists, False otherwise
- abstractmethod validate_config(config_data)[source]¶
Validate configuration data against the TemplateServerConfig model.
- load_config(config_filepath)[source]¶
Load configuration from the specified json file.
- Parameters:
config_filepath (
Path) – Path to the configuration file- Return TemplateServerConfig:
The validated configuration model
- Raises:
SystemExit – If configuration file is missing, invalid JSON, or fails validation
- Return type:
- add_unauthenticated_route(endpoint, handler_function, response_model, methods, limited=True)[source]¶
Add an unauthenticated API route.
- add_authenticated_route(endpoint, handler_function, response_model, methods, limited=True)[source]¶
Add an authenticated API route.
- Parameters:
- Return type:
- abstractmethod setup_routes()[source]¶
Add custom API routes.
This method must be implemented by subclasses to define API endpoints using add_unauthenticated_route and add_authenticated_route.
- Return type:
- async get_health(request)[source]¶
Get server health.
- Parameters:
request (
Request) – The incoming HTTP request- Return GetHealthResponse:
Health status response
- Raises:
HTTPException – If the server token is not configured
- Return type: