mirror of
https://github.com/mbierlee/mirage-config.git
synced 2024-11-14 20:34:00 +01:00
Add JSON config example
This commit is contained in:
parent
3c5f88b9fd
commit
94ae45a408
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -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/
|
23
dub.json
23
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"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
10
examples/README.md
Normal file
10
examples/README.md
Normal 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
39
examples/json/app.d
Normal 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
10
examples/json/config.json
Normal file
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"application": {
|
||||
"name": "Fake HTTP Server"
|
||||
},
|
||||
"server": {
|
||||
"host": "localhost",
|
||||
"port": 8080,
|
||||
"protocol": "https"
|
||||
}
|
||||
}
|
17
makefile
Normal file
17
makefile
Normal 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
15
source/mirage/testmain.d
Normal 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() {
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue