mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-14 20:34:00 +01:00
Add ini example
This commit is contained in:
parent
987dd7f31d
commit
6d692985e0
1
TODO.md
1
TODO.md
|
@ -14,4 +14,3 @@
|
|||
- Case insensitive properties and sections
|
||||
- Escape characters
|
||||
- Support multi-line values with backslash
|
||||
- Example
|
||||
|
|
11
dub.json
11
dub.json
|
@ -49,6 +49,17 @@
|
|||
"targetPath": "bin/examples/javaProperties",
|
||||
"workingDirectory": "bin/examples/javaProperties"
|
||||
},
|
||||
{
|
||||
"name": "iniExample",
|
||||
"targetType": "executable",
|
||||
"description": "Example on how to read and work with INI files.",
|
||||
"targetName": "iniExample",
|
||||
"sourcePaths": ["examples/ini"],
|
||||
"importPaths": ["source"],
|
||||
"copyFiles": ["examples/ini/config.ini"],
|
||||
"targetPath": "bin/examples/ini",
|
||||
"workingDirectory": "bin/examples/ini"
|
||||
},
|
||||
{
|
||||
"name": "valueSubstitutionExample",
|
||||
"targetType": "executable",
|
||||
|
|
37
examples/ini/app.d
Normal file
37
examples/ini/app.d
Normal file
|
@ -0,0 +1,37 @@
|
|||
/**
|
||||
* Authors:
|
||||
* Mike Bierlee, m.bierlee@lostmoment.com
|
||||
* Copyright: 2022 Mike Bierlee
|
||||
* License:
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
import mirage.ini : loadIniConfig, parseIniConfig;
|
||||
|
||||
import std.stdio : writeln;
|
||||
import std.conv : to;
|
||||
|
||||
void main() {
|
||||
auto config = loadIniConfig("config.ini");
|
||||
auto serverConfig = config.getConfig("server");
|
||||
auto databaseConfig = parseIniConfig("
|
||||
host=localhost
|
||||
port=5432
|
||||
");
|
||||
|
||||
auto applicationName = config.get("application.name");
|
||||
|
||||
auto httpHost = serverConfig.get("host");
|
||||
auto httpPort = serverConfig.get!uint("port");
|
||||
auto httpProtocol = serverConfig.get("protocol");
|
||||
|
||||
auto dbHost = databaseConfig.get("host");
|
||||
auto dbPort = databaseConfig.get!uint("port");
|
||||
|
||||
writeln("Starting " ~ applicationName ~ "...");
|
||||
writeln("Connecting to database at " ~ dbHost ~ ":" ~ dbPort.to!string ~ "...");
|
||||
writeln(
|
||||
"HTTP server now listening at " ~ httpProtocol ~ "://" ~ httpHost ~ ":" ~ httpPort
|
||||
.to!string);
|
||||
}
|
11
examples/ini/config.ini
Normal file
11
examples/ini/config.ini
Normal file
|
@ -0,0 +1,11 @@
|
|||
# This file is intentionally messy
|
||||
# to demonstrate the capabilities
|
||||
# of the format.
|
||||
|
||||
[application]
|
||||
name = "Fake HTTP Server"
|
||||
|
||||
[server]
|
||||
host = localhost
|
||||
port = 8080
|
||||
protocol: https ;DO NOT CHANGE!
|
|
@ -1,5 +1,5 @@
|
|||
# This file is intentionally messy
|
||||
# to demonstrate the full capabilities
|
||||
# to demonstrate the capabilities
|
||||
# of the format.
|
||||
|
||||
# App config
|
||||
|
|
4
makefile
4
makefile
|
@ -17,6 +17,7 @@ clean:
|
|||
run-examples: run-quickstartExample\
|
||||
run-jsonExample \
|
||||
run-javaPropertiesExample \
|
||||
run-iniExample \
|
||||
run-valueSubstitutionExample \
|
||||
run-manipulationExample
|
||||
|
||||
|
@ -29,6 +30,9 @@ run-jsonExample:
|
|||
run-javaPropertiesExample:
|
||||
dub run --build=release --config=javaPropertiesExample
|
||||
|
||||
run-iniExample:
|
||||
dub run --build=release --config=iniExample
|
||||
|
||||
run-valueSubstitutionExample:
|
||||
dub run --build=release --config=valueSubstitutionExample
|
||||
|
||||
|
|
Loading…
Reference in a new issue