Fork me on GitHub

funconf 0.1.0 documentation

Example webapp

«  funconf with begins   ::   Contents   ::   Roadmap  »

Example webapp

bottle makes writing a webapp super simple. begins makes writing a CLI to a program super simple. Through this example you will see how funconf can make managing configuration files super simple.

Taking this example YAML configuration file webapp.conf:

web:
  host: 0.0.0.0 
  port: 8080

Applying it to this simple program webapp.py:

import bottle
import begin
import funconf

@bottle.route('/hello')
def hello():
    return "Hello World!"

config = funconf.Config(['webapp.conf',
                         '~/.webapp.conf'])

@begin.start(env_prefix="WEBAPP_")
@config.web
def main(host='127.0.0.1', port=8080, debug=False):
    print(config)
    bottle.run(host=host, port=port, debug=debug)

The configuration changes the default host address to 0.0.0.0. In this program, configuration is applied and overridden in the following order:

The port option has been included into the config file to ensure that the funconf.Config object contains the updated port value that may have been set in an environment variable or set through the CLI. Now we have a neat way of getting the global configuration state for both host and port under the config.web object.

Here is what the help printout should look like for this application:

$ python webapp.py --help
usage: webapp.py [--help] [--debug WEBAPP_DEBUG] [--host WEBAPP_HOST]
                 [--port WEBAPP_PORT]

optional arguments:
  --help                show this help message and exit
  --debug WEBAPP_DEBUG, -d WEBAPP_DEBUG
                        (default: False)
  --host WEBAPP_HOST, -h WEBAPP_HOST
                        (default: 0.0.0.0)
  --port WEBAPP_PORT, -p WEBAPP_PORT
                        (default: 8080)

The following is the output that you’d expect:

$ python webapp.py

#
# Web
#
web:
  host: 0.0.0.0
  port: 8080

Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8080/
Hit Ctrl-C to quit.

Notice how the configuration object has been updated when we specify a new value for the port:

$ python webapp.py --port 8585
#
# Web
#
web:
  host: 0.0.0.0
  port: 8585

Bottle v0.11.6 server starting up (using WSGIRefServer())...
Listening on http://0.0.0.0:8585/
Hit Ctrl-C to quit.

The above example demonstrates how the configuration object has implicitly casted the input value 8585 CLI string into an integer.

«  funconf with begins   ::   Contents   ::   Roadmap  »