python_template_server.template_server

Template FastAPI server module.

Classes

TemplateServer([package_name, api_prefix, ...])

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 routers property and validate_config method 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 server

  • api_key_header_name (str) – The API key header name

  • config_filepath (Path) – Path to the configuration file

  • config (TemplateServerConfig | None) – Optional pre-loaded configuration

static lifespan(app)[source]

Handle application lifespan events.

Return type:

AsyncGenerator[None]

property static_dir_exists: bool

Check if the static directory exists.

Return bool:

True if the static directory exists, False otherwise

abstract property routers: list[BaseRouter]

List of BaseRouter instances to include in the server.

Return list[BaseRouter]:

List of BaseRouter instances

abstractmethod validate_config(config_data)[source]

Validate configuration data against the TemplateServerConfig model.

Parameters:

config_data (dict[str, Any]) – The configuration data to validate

Return TemplateServerConfig:

The validated configuration model

Raises:

ValidationError – If the configuration data is invalid

Return type:

TemplateServerConfig

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:

TemplateServerConfig

run()[source]

Run the server using uvicorn.

Return type:

None