Skip to content

SessionConfig

SessionConfig is simple set of configurations that when passed enables the built-in middleware of Ravyn. When a SessionConfig object is passed to an application instance, it will automatically start the SessionMiddleware.

Tip

More information about HTTP Sessions here.

SessionConfig and application

To use the SessionConfig in an application instance.

from ravyn import Ravyn, SessionConfig, settings

session_config = SessionConfig(
    secret_key=settings.secret_key,
)

app = Ravyn(session_config=session_config)

Another example

from ravyn import Ravyn, SessionConfig, settings

session_config = SessionConfig(
    secret_key=settings.secret_key,
    session_cookie="session",
)

app = Ravyn(session_config=session_config)

Parameters

All the parameters and defaults are available in the SessionConfig Reference.

SessionConfig and application settings

The SessionConfig can be done directly via application instantiation but also via settings.

from ravyn import RavynSettings, ImproperlyConfigured, SessionConfig


class CustomSettings(RavynSettings):
    @property
    def session_config(self) -> SessionConfig:
        """
        Initial Default configuration for the SessionConfig.
        This can be overwritten in another setting or simply override
        `session_config` or then override the `def session_config()`
        property to change the behavior of the whole session_config.
        """
        if not self.secret_key:
            raise ImproperlyConfigured("`secret` setting not configured.")
        return SessionConfig(
            secret_key=self.secret_key,
            session_cookie="session",
        )

This will make sure you keep the settings clean, separated and without a bloated Ravyn instance.

Ravyn Sessions

If you don't want to use the built-in session configuration and if you fancy a more custom way of handling the sessions with Ravyn, there is an official package Ravyn Sessions that can help you with that including the middleware.