duck.app.microapp

Mini application of Duck app which may be used for many simple tasks.

Notes:

  • Mini applications run independently on their own individual ports.

  • An example of a mini app is Duck’s internal HttpsRedirectApp which is used to redirect HTTP traffic to a more secure HTTPS server.

Module Contents

Classes

HttpsRedirectMicroApp

Micro application class capable of redirecting HTTP traffic to HTTPS.

MicroApp

Duck micro application class to create a new lightweight sub-application/server.

API

class duck.app.microapp.HttpsRedirectMicroApp(name: Optional[str] = None, addr: str = DEFAULT_ADDR, port: int = DEFAULT_PORT, domain: Optional[str] = None, server_url: Optional[str] = None, uses_ipv6: bool = False, enable_https: bool = False, no_checks: bool = False, no_logs: bool = True, workers: Optional[int] = None, force_worker_processes: bool = False, events: Optional[Dict[str, Optional[Callable]]] = None)[source]

Bases: duck.app.microapp.MicroApp

Micro application class capable of redirecting HTTP traffic to HTTPS.

Initialization

Parameters:
  • name – Unique name to your application.

  • addr – Address the server binds to.

  • port – Port the server binds to.

  • domain – Public-facing domain. Defaults to the bind address.

  • server_url – Public-facing absolute server URL.

  • uses_ipv6 – Whether the app should bind using IPv6.

  • enable_https – Whether HTTPS is enabled for this app.

  • no_checks – Whether to skip app checks. Defaults to Fa

  • no_logs – Whether to disable app logging. Defaults to False.

  • workers – Optional number of server workers.

  • force_worker_processes

    Determines whether to use worker processes instead of the default worker threads. By default, when workers is greater than 1, the server will use worker threads. Threads avoid cross-process synchronization issues—such as component registry mismatches (e.g., issues with Lively components) that occur when state lives in separate processes.

    Set this flag to True only when process isolation is explicitly desired and you do not require shared in-memory synchronization between workers.

  • events – Events to handle e.g. {“on_start”: some_callable}. Defaults to None.

Raises:

ApplicationError – If the provided bind address is invalid.

async async_view(request: duck.http.request.HttpRequest, request_processor: duck.http.core.processor.AsyncRequestProcessor) duck.http.response.HttpResponse[source]

Returns an HTTP redirect response.

view(request: duck.http.request.HttpRequest, request_processor: duck.http.core.processor.RequestProcessor) duck.http.response.HttpResponse[source]

Returns an HTTP redirect response.

class duck.app.microapp.MicroApp(name: Optional[str] = None, addr: str = DEFAULT_ADDR, port: int = DEFAULT_PORT, domain: Optional[str] = None, server_url: Optional[str] = None, uses_ipv6: bool = False, enable_https: bool = False, no_checks: bool = False, no_logs: bool = True, workers: Optional[int] = None, force_worker_processes: bool = False, events: Optional[Dict[str, Optional[Callable]]] = None)[source]

Bases: duck.app.base.BaseApp

Duck micro application class to create a new lightweight sub-application/server.

This micro app can be used to create a new sub-application with its own server, meaning, you can create multiple micro apps in a single Duck application.

Notes:

  • MicroApp should be used when you want to create a new server with its own address and port.

  • Every request to the micro app will be handled by the view or async_view method, no request will be passed to WSGI/ASGI.

  • Everything is to be handled manually in the view/async_view method and none of all available middlewares will be applied.

Initialization

Parameters:
  • name – Unique name to your application.

  • addr – Address the server binds to.

  • port – Port the server binds to.

  • domain – Public-facing domain. Defaults to the bind address.

  • server_url – Public-facing absolute server URL.

  • uses_ipv6 – Whether the app should bind using IPv6.

  • enable_https – Whether HTTPS is enabled for this app.

  • no_checks – Whether to skip app checks. Defaults to Fa

  • no_logs – Whether to disable app logging. Defaults to False.

  • workers – Optional number of server workers.

  • force_worker_processes

    Determines whether to use worker processes instead of the default worker threads. By default, when workers is greater than 1, the server will use worker threads. Threads avoid cross-process synchronization issues—such as component registry mismatches (e.g., issues with Lively components) that occur when state lives in separate processes.

    Set this flag to True only when process isolation is explicitly desired and you do not require shared in-memory synchronization between workers.

  • events – Events to handle e.g. {“on_start”: some_callable}. Defaults to None.

Raises:

ApplicationError – If the provided bind address is invalid.

DEFAULT_ADDR

‘localhost’

DEFAULT_PORT

8000

async _async_view(request: duck.http.request.HttpRequest, processor: duck.http.core.processor.AsyncRequestProcessor) duck.http.response.HttpResponse[source]

Internal entry method to asynchronous response generation.

Parameters:
  • request – The http request object.

  • processor – Default asynchronous request processor which may be used to process request.

_view(request: duck.http.request.HttpRequest, processor: duck.http.core.processor.RequestProcessor) duck.http.response.HttpResponse[source]

Internal entry method to response generation.

Parameters:
  • request – The http request object.

  • processor – Default request processor which may be used to process request.

abstractmethod async async_view(request: duck.http.request.HttpRequest, processor: duck.http.core.processor.AsyncRequestProcessor) duck.http.response.HttpResponse[source]

Asynchronous entry method to response generation.

Parameters:
  • request – The http request object.

  • processor – Default request processor which you may use to process request.

Notes:

  • Middlewares will not be applied on microapps, you are responsible for applying and handling middlewares.

  • On microapps, you are almost responsible for everything including managing database connections before and after request if needed.

  • But, the view response will be finalized automatically, meaning necessary headers will be set and response will be compressed if necessary.

run(run_forever: bool = True)[source]

Runs the duck sub-application.

Parameters:

run_forever – Whether to run a while loop to avoid app from exiting. Server is always run in background and setting run_forever=False will make this method return immediately after starting the background thread.

start_server()[source]

Starts the server in a new thread.

stop()[source]

Stops the current running micro-application.

abstractmethod view(request: duck.http.request.HttpRequest, processor: Union[duck.http.core.processor.AsyncRequestProcessor, duck.http.core.processor.RequestProcessor]) duck.http.response.HttpResponse[source]

Entry method to response generation.

Parameters:
  • request – The http request object.

  • processor – Default request processor which you may use to process request.

Notes:

  • Middlewares will not be applied on microapps, you are responsible for applying and handling middlewares.

  • On microapps, you are almost responsible for everything including managing database connections before and after request if needed.

  • But, the view response will be finalized automatically, meaning necessary headers will be set and response will be compressed if necessary.