Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pouches / Bundles

Quokka bundles logic in so called Pouches. These contain everything that a library or application need to function. This includes database logic, templates, form (or other backend) logic and the entirety of styling.

The pouches allow you to extend the base framework with whatever resources it supports.

To keep the Pouches and your libraries and applications updatable without a big hassle as soon as Quokka gets a new function, the Pouch trait shall only get new callbacks added to it which come with default function bodies so that you can simply update and use the new functions when you are ready. Or just completely skip it if you don't need it.

To let Quokka build your Bundle it also has to implement the quokka::config::TryFromModule trait. For an example check out the Configuration chapter.

Available callbacks

The following hooks are available to extend the functionallity of Quokka and other bundles.

CallbackDescription
get_routerCreate your application's router. It may contain all your required layers, states and routes
configure_routerAllows you to modify the global router which conatins all the routers from other bundles too. Only use it if necessary.
configure_stateConfigures the S State. The global state that Quokka uses. Here you interact with every other state, register your resources etc. Combined with a ProvideStateRef<YourState> you have easy access to every substate. The first-party, Quokka provided substates even come with traits that allow you to directly act on a S: ProvideStateRef<SubState>.
run_setup_jobThis allows you to initialize and run some job. This function can be used to either run some initialization (like database migrations) or spawn some daemon into the tokio runtime.

Examples

The examples will provide you with some more details about the more complex callbacks.

Note: For further examples you can checkout the examples directory of the Quokko repository.

configure_state

This is most likely the most basic but still mostly used implementation of this function. Extracting a substate and register some resource on it.

impl<S> quokka::bundle::Pouch<S> for MyBundle
    where S: quokka::state::ProvideStateRef<ThirdPartyState> {

    fn configure_state(&self, state: &mut S) -> quokka::state::Result<()> {
        // The ProvideStateRef trait implements a method giving you a mutable reference to the original substate allowing you to modify it
        let third_party: &mut ThirdPartyState = state.provide();
        third_party.register_a_thing("Thing");

        Ok(())
    }
}