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

HttpsRedirectMicroApp class capable of redirecting http traffic to https.

MicroApp

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

APIยถ

class duck.app.microapp.HttpsRedirectMicroApp(location_root_url: str, *args, **kwargs)ยถ

Bases: duck.app.microapp.MicroApp

HttpsRedirectMicroApp class capable of redirecting http traffic to https.

Initialization

Initialize the MicroApp class.

Parameters:
  • add โ€“ Micro application address, defaults to localhost.

  • port โ€“ Micro application port. Defaults to 8080.

  • parent_app โ€“ The root Duck application instance.

  • domain โ€“ Micro application domain. Defaults to None.

  • uses_ipv6 โ€“ Whether to use IPV6. Defaults to False.

  • enable_https โ€“ Whether to enable https. Defaults to False.

  • no_logs โ€“ Whether to log anything to console. Defaults to True.

  • workers โ€“ Number of workers to use. None will disable 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.

async async_view(request: duck.http.request.HttpRequest, request_processor: duck.http.core.processor.AsyncRequestProcessor) โ†’ duck.http.response.HttpResponseยถ

Returns an http redirect response.

view(request: duck.http.request.HttpRequest, request_processor: duck.http.core.processor.RequestProcessor) โ†’ duck.http.response.HttpResponseยถ

Returns an http redirect response.

class duck.app.microapp.MicroApp(addr: str = 'localhost', port: int = 8080, parent_app: App = None, domain: str = None, uses_ipv6: bool = False, enable_https: bool = False, no_logs: bool = True, workers: Optional[int] = None, force_worker_processes: bool = False)ยถ

Duck micro app 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

Initialize the MicroApp class.

Parameters:
  • add โ€“ Micro application address, defaults to localhost.

  • port โ€“ Micro application port. Defaults to 8080.

  • parent_app โ€“ The root Duck application instance.

  • domain โ€“ Micro application domain. Defaults to None.

  • uses_ipv6 โ€“ Whether to use IPV6. Defaults to False.

  • enable_https โ€“ Whether to enable https. Defaults to False.

  • no_logs โ€“ Whether to log anything to console. Defaults to True.

  • workers โ€“ Number of workers to use. None will disable 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.

async _async_view(request: duck.http.request.HttpRequest, processor: duck.http.core.processor.AsyncRequestProcessor) โ†’ duck.http.response.HttpResponseยถ

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ยถ

Internal entry method to response generation.

Parameters:
  • request โ€“ The http request object.

  • processor โ€“ Default request processor which may be used to process request.

property absolute_uri: strยถ

Returns application server absolute URL.

abstractmethod async async_view(request: duck.http.request.HttpRequest, processor: duck.http.core.processor.AsyncRequestProcessor) โ†’ duck.http.response.HttpResponseยถ

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.

build_absolute_uri(path: str) โ†’ strยถ

Builds and returns absolute URL from provided path.

on_app_start()ยถ

Called on successfull app start.

run(run_forever: bool = True)ยถ

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.

property server_up: boolยถ

Checks whether the micro-application server is up and running.

Returns:

True if up else False

Return type:

bool

start_server()ยถ

Starts the Duck Server in a new thread.

stop()ยถ

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ยถ

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.