Config Classes
Since app configs are used as the CLIs entrypoint into an app and a replacement of environment files, config classes are used alongside them to configure and influence the runtime of your app.
The core concept
Config classes define and / or overwrite the settings of your app. One example for that
is the SESSION_COOKIE_SECURE variable that caused the warning inside the log when we were
setting up our first app. Those settings are set up using
config classes, then parsed when your Fluid instance gets initialized and can be accessed via the
fluid.config dict later on.
Registering your first config
You've got multiple options on how to register a config class. All of them rely on the
register_config decorator. To keep things simple, we will use our main.py
file from getting started and expand it with a minimal example of registering a config:
from webfluid import Fluid
from webfluid.core.config import register_config
from fastapi.responses import HTMLResponse
@register_config(10)
class MyConfig:
SESSION_COOKIE_SECURE = True
fluid = Fluid(__name__)
@fluid.get("/", response_class=HTMLResponse)
async def home():
return await fluid.render(
"index.html",
title="Hello World!",
name="my friend"
)
@fluid.get("/health")
async def health():
return {"status": "ok"}
if __name__ == "__main__":
fluid.mix()
What just happened
There are two major details we need to take a look at:
- We've registered the config before the app will get initialized. This is important because the decorator function must be executed before the app gets initialized. Otherwise, your config will never be parsed.
- There is a number passed as a parameter to the decorator. This is the priority of the config. If you have multiple configs registered, the one with the highest priority will overwrite the others. Default (if noting is passed) is 1. Possible values are 1 to 10. So 10 is the highest possible priority.
The second way to register your config
Now you may not want to ensure manually that the config registration decorator is executed
before initializing your app. Thankfully, there's some magic happening behind the scenes.
Because if you just create a config.py file inside the fluid directory, you
can outsource the registration of your to the initialization process.
Important config values
When configuring your app, there are some important settings you need to know:
-
APP_CONFIG— A dictionary that stores the FastAPI initialization parameters as keyword arguments. Default (if not defined in a config class) is { "title": "WebFluid Application", "version": "1.0.0" }. -
APP_FRONTEND— The Frontend configuration dictionary of your main app. More about that in the "Frontend" chapter. -
SESSION_COOKIE_*— There are three SessionMiddleware configuration values: NAME, SAMESITE and SECURE. Default values are "session", "lax" and False. -
PROXY_FIX— If set to true your app will be cast into a ProxyHeadersMiddleware and thefluid.asgi_appproperty will return a ProxyHeadersMiddleware instance. -
RATELIMIT_*— Three values for setting up ratelimiting: ENABLED, STORAGE_URI and DEFAULT. WebFluid uses slowapi for ratelimiting. Default values are True, f"{os.getenv('REDIS_URI', 'redis://localhost:6379')}/1" and ["500/day", "100/hour"]. - Further configuration values are used by the framework extensions to load their settings. More about them and their defaults in the next chapter.
Continue reading
From here you can continue straight with Fluid Extensions.