87 lines
3.1 KiB
Markdown
87 lines
3.1 KiB
Markdown
# Configuration
|
|
|
|
Any reasonably complex application will need configuration that is not baked into the actual code. Settings might be different for different environments or installations.
|
|
|
|
## Basics
|
|
|
|
Sanic holds the configuration in the `config` attribute of the application object. The configuration object is merely an object that can be modified either using dot-notation or like a dictionary:
|
|
|
|
```
|
|
app = Sanic('myapp')
|
|
app.config.DB_NAME = 'appdb'
|
|
app.config.DB_USER = 'appuser'
|
|
```
|
|
|
|
Since the config object actually is a dictionary, you can use its `update` method in order to set several values at once:
|
|
|
|
```
|
|
db_settings = {
|
|
'DB_HOST': 'localhost',
|
|
'DB_NAME': 'appdb',
|
|
'DB_USER': 'appuser'
|
|
}
|
|
app.config.update(db_settings)
|
|
```
|
|
|
|
In general the convention is to only have UPPERCASE configuration parameters. The methods described below for loading configuration only look for such uppercase parameters.
|
|
|
|
## Loading Configuration
|
|
|
|
There are several ways how to load configuration.
|
|
|
|
### From environment variables.
|
|
|
|
Any variables defined with the `SANIC_` prefix will be applied to the sanic config. For example, setting `SANIC_REQUEST_TIMEOUT` will be loaded by the application automatically. You can pass the `load_vars` boolean to the Sanic constructor to override that:
|
|
|
|
```python
|
|
app = Sanic(load_vars=False)
|
|
```
|
|
|
|
### From an Object
|
|
|
|
If there are a lot of configuration values and they have sensible defaults it might be helpful to put them into a module:
|
|
|
|
```
|
|
import myapp.default_settings
|
|
|
|
app = Sanic('myapp')
|
|
app.config.from_object(myapp.default_settings)
|
|
```
|
|
|
|
You could use a class or any other object as well.
|
|
|
|
### From a File
|
|
|
|
Usually you will want to load configuration from a file that is not part of the distributed application. You can load configuration from a file using `from_pyfile(/path/to/config_file)`. However, that requires the program to know the path to the config file. So instead you can specify the location of the config file in an environment variable and tell Sanic to use that to find the config file:
|
|
|
|
```
|
|
app = Sanic('myapp')
|
|
app.config.from_envvar('MYAPP_SETTINGS')
|
|
```
|
|
|
|
Then you can run your application with the `MYAPP_SETTINGS` environment variable set:
|
|
|
|
```
|
|
$ MYAPP_SETTINGS=/path/to/config_file python3 myapp.py
|
|
INFO: Goin' Fast @ http://0.0.0.0:8000
|
|
```
|
|
|
|
The config files are regular Python files which are executed in order to load them. This allows you to use arbitrary logic for constructing the right configuration. Only uppercase variables are added to the configuration. Most commonly the configuration consists of simple key value pairs:
|
|
|
|
```
|
|
# config_file
|
|
DB_HOST = 'localhost'
|
|
DB_NAME = 'appdb'
|
|
DB_USER = 'appuser'
|
|
```
|
|
|
|
## Builtin Configuration Values
|
|
|
|
Out of the box there are just a few predefined values which can be overwritten when creating the application.
|
|
|
|
| Variable | Default | Description |
|
|
| ----------------- | --------- | --------------------------------- |
|
|
| REQUEST_MAX_SIZE | 100000000 | How big a request may be (bytes) |
|
|
| REQUEST_TIMEOUT | 60 | How long a request can take (sec) |
|
|
| KEEP_ALIVE | True | Disables keep-alive when False |
|