Add java properties example

This commit is contained in:
Mike Bierlee 2022-10-09 00:02:25 +03:00
parent 34e78be898
commit 05e44b1b26
4 changed files with 56 additions and 0 deletions

View file

@ -26,6 +26,17 @@
"targetPath": "bin/examples/json", "targetPath": "bin/examples/json",
"workingDirectory": "bin/examples/json" "workingDirectory": "bin/examples/json"
}, },
{
"name": "javaPropertiesExample",
"targetType": "executable",
"description": "Example on how to read and work with Java properties.",
"targetName": "javaPropertiesExample",
"sourcePaths": ["examples/javaProperties"],
"importPaths": ["source"],
"copyFiles": ["examples/javaProperties/application.properties"],
"targetPath": "bin/examples/javaProperties",
"workingDirectory": "bin/examples/javaProperties"
},
{ {
"name": "valueSubstitutionExample", "name": "valueSubstitutionExample",
"targetType": "executable", "targetType": "executable",

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.java : parseJavaProperties, loadJavaProperties;
import std.stdio : writeln;
import std.conv : to;
void main() {
auto config = loadJavaProperties("application.properties");
auto serverConfig = config.getConfig("server");
auto databaseConfig = parseJavaProperties("
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);
}

View file

@ -0,0 +1,4 @@
application.name=Fake HTTP Server
server.host=localhost
server.port=8080
server.protocol=https

View file

@ -12,12 +12,16 @@ clean:
dub clean dub clean
run-examples: run-jsonExample \ run-examples: run-jsonExample \
run-javaPropertiesExample \
run-valueSubstitutionExample \ run-valueSubstitutionExample \
run-manipulationExample run-manipulationExample
run-jsonExample: run-jsonExample:
dub run --build=release --config=jsonExample dub run --build=release --config=jsonExample
run-javaPropertiesExample:
dub run --build=release --config=javaPropertiesExample
run-valueSubstitutionExample: run-valueSubstitutionExample:
dub run --build=release --config=valueSubstitutionExample dub run --build=release --config=valueSubstitutionExample