Vibora Docs
Search…
Configuration
Configuration handling in Vibora is simple thanks to components.
In your init script (usually called run.py) you can load environment variables, config files or whatever and register a config class as a new component and that's all.
This method is a little bit harder for beginners when compared to the Django approach but it's way more flexible and allows you to build whatever suits you better.
Here goes a practical example:
1) Create a file called config.py
1
import aioredis
2
3
4
class Config:
5
def __init__(self, config: dict):
6
self.port = config['port']
7
self.host = config['host']
8
self.redis_host = config['redis']['host']
Copied!
2) Create a file called api.py
1
from vibora import Vibora
2
from vibora.blueprints import Blueprint
3
from vibora.hooks import Events
4
from aioredis import ConnectionsPool
5
from config import Config
6
7
api = Blueprint()
8
9
10
@api.route('/')
11
async def home(pool: ConnectionsPool):
12
await pool.set('my_key', 'any_value')
13
value = await pool.get('my_key')
14
return Response(value.encode())
15
16
17
@api.handle(Events.BEFORE_SERVER_START)
18
async def initialize_db(app: Vibora, config: Config):
19
20
# Creating a pool of connection to Redis.
21
pool = await aioredis.create_pool(config.redis_host)
22
23
# In this case we are registering the pool as a new component
24
# but if you find yourself using too many components
25
# feel free to wrap them all inside a single component
26
# so you don't need to repeat yourself in every route.
27
app.components.add(pool)
Copied!
3) Now create a file called config.json
1
{
2
"host": "0.0.0.0",
3
"port": 8000,
4
"redis_host": "127.0.0.1"
5
}
Copied!
4) Now create a file called run.py
1
import json
2
from vibora import Vibora
3
from api import api
4
from config import Config
5
6
7
if __name__ == "__main__":
8
# Creating a new app
9
app = Vibora()
10
11
# Registering our API
12
app.add_blueprint(api, prefixes={'v1': '/v1'})
13
14
# Opening the configuration file.
15
with open('config.json') as f:
16
17
# Parsing the JSON configs.
18
config = Config(json.load(f))
19
20
# Registering the config as a component so you can use it
21
# later on (as we do in the "before_server_start" hook)
22
app.components.add(config)
23
24
# Running the server.
25
app.run(host=config.host, port=config.port)
Copied!
The previous example loads your configuration from JSON files, but other approaches, such as environment variables, can be used.
Notice that we register the config instance as a component because databases drivers, for example, often need to be instantiated after the server is forked so you'll need the config after the "run script".
Also, our config class in this example is a mere wrapper for our JSON config but in a real app, you could be using the config class as a components wrapper. You'll just need to add references to many important components so you don't need to repeat yourself by importing many different components in every route.
Last modified 3yr ago
Copy link