From 94ae45a40816e2b5b2d5fafd55def2d8f100c132 Mon Sep 17 00:00:00 2001 From: Mike Bierlee Date: Sun, 25 Sep 2022 19:03:26 +0300 Subject: [PATCH] Add JSON config example --- .gitignore | 2 ++ dub.json | 23 ++++++++++++++++++++++- examples/README.md | 10 ++++++++++ examples/json/app.d | 39 +++++++++++++++++++++++++++++++++++++++ examples/json/config.json | 10 ++++++++++ makefile | 17 +++++++++++++++++ source/mirage/testmain.d | 15 +++++++++++++++ 7 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 examples/README.md create mode 100644 examples/json/app.d create mode 100644 examples/json/config.json create mode 100644 makefile create mode 100644 source/mirage/testmain.d diff --git a/.gitignore b/.gitignore index 981a70e..dd14b6e 100644 --- a/.gitignore +++ b/.gitignore @@ -8,8 +8,10 @@ mirage-config.dylib mirage-config.dll mirage-config.a mirage-config.lib +mirage-config.pdb mirage-config-test-* *.exe *.o *.obj *.lst +/bin/ \ No newline at end of file diff --git a/dub.json b/dub.json index a322081..07a70c5 100644 --- a/dub.json +++ b/dub.json @@ -4,5 +4,26 @@ "copyright": "Copyright © 2022, Mike Bierlee", "authors": ["Mike Bierlee"], "license": "MIT", - "targetType": "library" + "configurations": [ + { + "name": "library", + "targetType": "library" + }, + { + "name": "unittest", + "targetType": "executable", + "sourcePaths": ["source"] + }, + { + "name": "jsonExample", + "targetType": "executable", + "description": "Example of how to read and work with JSON configurations.", + "targetName": "jsonExample", + "sourcePaths": ["examples/json"], + "importPaths": ["source"], + "copyFiles": ["examples/json/config.json"], + "targetPath": "bin/examples/json", + "workingDirectory": "bin/examples/json" + } + ] } diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..08560b7 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,10 @@ +To run any of these examples, specify the relevant DUB configuration: +``` +dub run --build=release --config=Example +``` +Where \ corresponds to the name of the folder where the code example is in (camel-cased). + +e.g: +``` +dub run --build=release --config=jsonExample +``` diff --git a/examples/json/app.d b/examples/json/app.d new file mode 100644 index 0000000..6e56c0d --- /dev/null +++ b/examples/json/app.d @@ -0,0 +1,39 @@ +/** + * 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.json : JsonConfigFactory; + +import std.stdio : writeln; +import std.conv : to; + +void main() { + auto config = new JsonConfigFactory().loadFile("config.json"); + auto serverConfig = config.getConfig("server"); + auto databaseConfig = new JsonConfigFactory().parseConfig(" + { + \"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/json/config.json b/examples/json/config.json new file mode 100644 index 0000000..1e504f1 --- /dev/null +++ b/examples/json/config.json @@ -0,0 +1,10 @@ +{ + "application": { + "name": "Fake HTTP Server" + }, + "server": { + "host": "localhost", + "port": 8080, + "protocol": "https" + } +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..9fe74fa --- /dev/null +++ b/makefile @@ -0,0 +1,17 @@ +.PHONY: build +.PHONY: test +.PHONY: clean + +build: + dub build --build=release + +test: + dub test + +clean: + dub clean + +run-examples: run-jsonExample + +run-jsonExample: + dub run --build=release --config=jsonExample \ No newline at end of file diff --git a/source/mirage/testmain.d b/source/mirage/testmain.d new file mode 100644 index 0000000..ea920f0 --- /dev/null +++ b/source/mirage/testmain.d @@ -0,0 +1,15 @@ +/** + * 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. + */ + +module mirage.testmain; + +version (unittest) { + void main() { + } +}