From 05e44b1b26f4f09f02a0297f71bffc13ec9e04a5 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 9 Oct 2022 00:02:25 +0300 Subject: [PATCH] Add java properties example --- dub.json | 11 ++++++ examples/javaProperties/app.d | 37 +++++++++++++++++++ .../javaProperties/application.properties | 4 ++ makefile | 4 ++ 4 files changed, 56 insertions(+) create mode 100644 examples/javaProperties/app.d create mode 100644 examples/javaProperties/application.properties diff --git a/dub.json b/dub.json index d3ebfb9..560190b 100644 --- a/dub.json +++ b/dub.json @@ -26,6 +26,17 @@ "targetPath": "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", "targetType": "executable", diff --git a/examples/javaProperties/app.d b/examples/javaProperties/app.d new file mode 100644 index 0000000..d03b19c --- /dev/null +++ b/examples/javaProperties/app.d @@ -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); +} diff --git a/examples/javaProperties/application.properties b/examples/javaProperties/application.properties new file mode 100644 index 0000000..ab38307 --- /dev/null +++ b/examples/javaProperties/application.properties @@ -0,0 +1,4 @@ +application.name=Fake HTTP Server +server.host=localhost +server.port=8080 +server.protocol=https \ No newline at end of file diff --git a/makefile b/makefile index 3f95952..86a39e6 100644 --- a/makefile +++ b/makefile @@ -12,12 +12,16 @@ clean: dub clean run-examples: run-jsonExample \ + run-javaPropertiesExample \ run-valueSubstitutionExample \ run-manipulationExample run-jsonExample: dub run --build=release --config=jsonExample +run-javaPropertiesExample: + dub run --build=release --config=javaPropertiesExample + run-valueSubstitutionExample: dub run --build=release --config=valueSubstitutionExample