Configuration¶
There exist various way of providing a runtime configuration.
If you need to pass secrets, take a look at the Libsecret project. It allows one to securely store and retrieve secrets: just unlock the keyring and start your service.
Key file¶
GLib provide a very handy way of reading and parsing key files, which are widely used across freedesktop specifications.
It should be privileged if the configuration is mostly edited by humans.
[app]
public-dir=public
[database]
provider=mysql
connection=
auth=
using GLib;
using Valum;
var config = new KeyFile ();
config.parse_path ("app.conf");
var app = new Router ();
app.get ("/public/<path:path>",
Static.serve_from_path (config.get_string ("app", "public-dir")));
JSON¶
The JSON-GLib project provide a really convenient JSON parser and generator.
{
"app": {
"publicDir": "public"
},
"database": {
"provider": "mysql",
"connection": "",
"auth": ""
}
}
using Json;
using Valum;
var parser = new Parser ();
parser.parse_from_file ("config.json");
var config = parser.get_root ();
var app = new Router ();
app.get ("/public/<path:path>",
Static.serve_from_path (config.get_object ("app").get_string_member ("publicDir")));
YAML¶
There is a GLib wrapper around libyaml that makes it more convenient to use. YAML in itself can be seen as a human-readable JSON format.
app:
publicDir: public
database:
provider: mysql
connection:
auth:
using Valum;
using Yaml;
var config = new Document.from_path ("config.yml").root as Node.Mapping;
var app = new Router ();
app.get ("/public/<path:path>",
Static.serve_from_path (config.get_mapping ("app").get_scalar ("publicDir").value));
Other approaches¶
The following approaches are a bit more complex to setup but can solve more specific use cases:
- GXml or libxml2
- GSettings for a remote (via DBus) and monitorable configuration
- environment variables via glib-2.0/GLib.Environment utilities
- CLI options (see
VSGI.Server.add_main_option
andVSGI.Server.handle_local_options
)