Add JSON config example

This commit is contained in:
Mike Bierlee 2022-09-25 19:03:26 +03:00
parent 3c5f88b9fd
commit 94ae45a408
7 changed files with 115 additions and 1 deletions

2
.gitignore vendored
View file

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

View file

@ -4,5 +4,26 @@
"copyright": "Copyright © 2022, Mike Bierlee",
"authors": ["Mike Bierlee"],
"license": "MIT",
"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"
}
]
}

10
examples/README.md Normal file
View file

@ -0,0 +1,10 @@
To run any of these examples, specify the relevant DUB configuration:
```
dub run --build=release --config=<name>Example
```
Where \<name\> corresponds to the name of the folder where the code example is in (camel-cased).
e.g:
```
dub run --build=release --config=jsonExample
```

39
examples/json/app.d Normal file
View file

@ -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);
}

10
examples/json/config.json Normal file
View file

@ -0,0 +1,10 @@
{
"application": {
"name": "Fake HTTP Server"
},
"server": {
"host": "localhost",
"port": 8080,
"protocol": "https"
}
}

17
makefile Normal file
View file

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

15
source/mirage/testmain.d Normal file
View file

@ -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() {
}
}