Manual Route Registration

Additionnal URL routes can be registered by creating a new RoutingTable which references classes and their methods. Note that the appropriate classes should be imported beforehand. The referenced resource class can be either an instance, or the actual class. In that case, it will be instantiated by the ServiceLocator, and should therefore be registered manually in the configure_service_locator context method.

from jivago.wsgi.methods import GET, POST
from jivago.wsgi.routing.tree_routing_table import TreeRoutingTable

my_routing_table = TreeRoutingTable()

my_routing_table.register_route(GET, "/hello", MyResourceClass, MyResourceClass.get_hello)
my_routing_table.register_route(POST, "/hello", MyResourceClass, MyResourceClass.get_hello)

This new RoutingTable can then be used to configure the Router object, which is used to serve all requests. The recommended way of configuring your application is by inheriting from the ProductionJivagoContext class, and then overriding the create_router method.

from jivago.config.production_jivago_context import ProductionJivagoContext
from jivago.jivago_application import JivagoApplication
from jivago.lang.annotations import Override
from jivago.lang.registry import Registry
from jivago.wsgi.routing.router import Router


class MyApplicationContext(ProductionJivagoContext):

    @Override
    def create_router(self) -> Router:
        router = super().create_router()
        router.add_routing_table(my_routing_table)
        return router


app = JivagoApplication(my_package,context=MyApplicationContext)

Serving static files

While it is not generally recommended to serve static files from a WSGI application for performance reasons, Jivago supports static file serving. The StaticFileRoutingTable dynamically defines routes for serving files.

from jivago.config.production_jivago_context import ProductionJivagoContext
from jivago.lang.annotations import Override
from jivago.wsgi.routing.router import Router
from jivago.wsgi.routing.serving.static_file_routing_table import StaticFileRoutingTable


class MyApplicationContext(ProductionJivagoContext):

    @Override
    def create_router(self) -> Router:
        router = super().create_router()
        router.add_routing_table(StaticFileRoutingTable("/var/www"))
        router.add_routing_table(StaticFileRoutingTable("/var/www", allowed_extensions=['.html', '.xml']))

        return router

The StaticFileRoutingTable can also be used with a allowed_extensions parameter to explicitly allow or disallow specific file types.

Defining path prefixes

When registering a new routing table, using the path_prefix parameter maps the new routing table to part of the path hierarchy. For instance, static files can be served from /static/my_file.html.

from jivago.config.production_jivago_context import ProductionJivagoContext
from jivago.lang.annotations import Override
from jivago.wsgi.routing.router import Router
from jivago.wsgi.routing.serving.static_file_routing_table import StaticFileRoutingTable


class MyApplicationContext(ProductionJivagoContext):

    @Override
    def create_router(self) -> Router:
        router = super().create_router()

        router.add_routing_table(StaticFileRoutingTable("/var/www"), "/static")

        return router