Neoteroi / BlackSheep

Fast ASGI web framework for Python
https://www.neoteroi.dev/blacksheep/
MIT License
1.8k stars 75 forks source link

Alternative settings format #386

Closed Kokoserver closed 1 year ago

Kokoserver commented 1 year ago

I have been playing around with blacksheep i just want to ask if there is another way to specify the settings aside using setings.yaml. I'm used to using pydantic BaseSetting which allow me to have all my settings in one place and also can cach it even importantly load all the settings on app start. i try looking into the docs i could not find much information abbout it

RobertoPrevato commented 1 year ago

BlackSheep is not bound to any solution to handle application settings. The settings.yaml file of the project templates is just an example, you can use pydantic BaseSettings in blacksheep, too. By the way, it is the same with input models for request handlers: blacksheep supports pydantic out of the box, while still being abstracted from it (it also supports dataclasses and regular classes).

The new project templates I am working on for blacksheep v2, use another library to build the configuration root, supporting layers of sources (like env variables, configuration files, settings stored in a user's folder, secrets stored in AWS or Azure, etc.); but it is anyway possible to use pydantic and instantiate a subclass of BaseSettings from the configuration root, to support multiple sources and validation at the same time. I will try to prepare an example for this.

I am still working on the new project templates, and I published just yesterday to pypi the blacksheep-cli (something might still not work). https://pypi.org/project/blacksheep-cli/0.0.2/

You can already try the new templates for v2 (although it is still alpha):

pip install blacksheep-cli

And then bootstrap a project this way:

blacksheep create --template mvc

the new templates support different types of configuration files to build the configuration root (JSON, TOML, YAML, INI), and you can add BaseSettings on top of that, to validate those settings.

blacksheep create --template mvc
✨ Project name: mvc-test
📜 Use OpenAPI Documentation? Yes
🔧 App settings format TOML
──────────────────────────────────────────────────────────────────────
🏗️  Project created in mvc-test
──────────────────────────────────────────────────────────────────────
-- What's next:
        cd mvc-test
        pip install -r requirements.txt
        python dev.py
RobertoPrevato commented 1 year ago

I read more about the settings management feature in pydantic, I confirm that it can be used in blacksheep (as with any other kind of application). I will consider including it as an option in the new project templates.

RobertoPrevato commented 1 year ago

@Kokoserver Thanks to your question, I looked more into this, and added support for Pydantic settings management in the new project templates.

You can try it this way:

pip install -U blacksheep-cli

blacksheep create --template mvc
✨ Project name: example
📜 Use OpenAPI Documentation? Yes
🔧 App settings library Pydantic

This way the project uses BaseSettings to handle project settings.

RobertoPrevato commented 1 year ago

Marked as completed because now both the API and the MVC project templates support and use pydantic to handle settings (either to collect app settings from sources + validation, or only validation).