Add ini example

This commit is contained in:
Mike Bierlee 2022-10-13 20:09:14 +03:00
parent 987dd7f31d
commit 6d692985e0
6 changed files with 64 additions and 2 deletions

View file

@ -14,4 +14,3 @@
- Case insensitive properties and sections
- Escape characters
- Support multi-line values with backslash
- Example

View file

@ -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
View 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
View 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!

View file

@ -1,5 +1,5 @@
# This file is intentionally messy
# to demonstrate the full capabilities
# to demonstrate the capabilities
# of the format.
# App config

View file

@ -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