mirror of
https://github.com/mbierlee/mirage-injector.git
synced 2024-11-21 15:34:01 +01:00
Add quickstart example
This commit is contained in:
parent
4da7977d95
commit
994a67d403
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -13,4 +13,5 @@ poodinis-mirage-config-injector-test-*
|
|||
*.o
|
||||
*.obj
|
||||
*.lst
|
||||
*.pdb
|
||||
*.pdb
|
||||
/bin/
|
4
CHANGES.md
Normal file
4
CHANGES.md
Normal file
|
@ -0,0 +1,4 @@
|
|||
# Mirage Config injector for Poodinis Changelog
|
||||
|
||||
**Version 1.0.0** (27-11-2022)
|
||||
* Initial version
|
48
README.md
48
README.md
|
@ -4,4 +4,50 @@ Version 0.0.0
|
|||
Copyright 2022 Mike Bierlee
|
||||
Licensed under the terms of the MIT license - See [LICENSE.txt](LICENSE.txt)
|
||||
|
||||
A config value injector for the [Poodinis dependency injection framework](https://github.com/mbierlee/poodinis) using [Mirage Config](https://github.com/mbierlee/mirage-config)
|
||||
A config value injector for the [Poodinis dependency injection framework](https://github.com/mbierlee/poodinis) using [Mirage Config](https://github.com/mbierlee/mirage-config)
|
||||
|
||||
## Getting started
|
||||
|
||||
### DUB Dependency
|
||||
|
||||
See the [DUB project page](https://code.dlang.org/packages/poodinis-mirage-config-injector) for instructions on how to include Mirage Config into your project.
|
||||
|
||||
### Quickstart
|
||||
|
||||
```d
|
||||
import poodinis : DependencyContainer, Value;
|
||||
import poodinis.valueinjector.mirage : loadConfig;
|
||||
|
||||
import std.stdio : writeln;
|
||||
import std.conv : to;
|
||||
|
||||
class Server
|
||||
{
|
||||
@Value("server.host")
|
||||
private string host;
|
||||
|
||||
@Value("server.port")
|
||||
private int port;
|
||||
|
||||
public void run()
|
||||
{
|
||||
writeln("Running server on " ~ host ~ ":" ~ port.to!string);
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!Server;
|
||||
container.loadConfig("config.ini");
|
||||
|
||||
auto server = container.resolve!Server;
|
||||
server.run();
|
||||
}
|
||||
```
|
||||
|
||||
Functions such as `loadConfig` are the same as available in Mirage. All individual loaders and parses are available. For more information on how to use Mirage, see https://github.com/mbierlee/mirage-config/blob/main/README.md
|
||||
|
||||
## History
|
||||
|
||||
For a full overview of changes, see [CHANGES.md](CHANGES.md)
|
||||
|
|
24
dub.json
24
dub.json
|
@ -8,5 +8,27 @@
|
|||
"dependencies": {
|
||||
"poodinis": "~>8.1.3",
|
||||
"mirage-config": "~>1.0.0"
|
||||
}
|
||||
},
|
||||
"configurations": [
|
||||
{
|
||||
"name": "library",
|
||||
"targetType": "library"
|
||||
},
|
||||
{
|
||||
"name": "unittest",
|
||||
"targetType": "executable",
|
||||
"sourcePaths": ["source"],
|
||||
"mainSourceFile": "testmain.d"
|
||||
},
|
||||
{
|
||||
"name": "quickstartExample",
|
||||
"targetType": "executable",
|
||||
"targetName": "quickstartExample",
|
||||
"sourcePaths": ["examples/quickstart"],
|
||||
"importPaths": ["source"],
|
||||
"copyFiles": ["examples/quickstart/config.ini"],
|
||||
"targetPath": "bin/examples/quickstart",
|
||||
"workingDirectory": "bin/examples/quickstart"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
|
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=quickstartExample
|
||||
```
|
38
examples/quickstart/app.d
Normal file
38
examples/quickstart/app.d
Normal file
|
@ -0,0 +1,38 @@
|
|||
/**
|
||||
* Mirage Config value injector for the Poodinis Dependency Injection Framework
|
||||
* Copyright 2022 Mike Bierlee
|
||||
* 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 examples.quickstart.app;
|
||||
|
||||
import poodinis : DependencyContainer, Value;
|
||||
import poodinis.valueinjector.mirage : loadConfig;
|
||||
|
||||
import std.stdio : writeln;
|
||||
import std.conv : to;
|
||||
|
||||
class Server
|
||||
{
|
||||
@Value("server.host")
|
||||
private string host;
|
||||
|
||||
@Value("server.port")
|
||||
private int port;
|
||||
|
||||
public void run()
|
||||
{
|
||||
writeln("Running server on " ~ host ~ ":" ~ port.to!string);
|
||||
}
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
auto container = new shared DependencyContainer();
|
||||
container.register!Server;
|
||||
container.loadConfig("config.ini");
|
||||
|
||||
auto server = container.resolve!Server;
|
||||
server.run();
|
||||
}
|
3
examples/quickstart/config.ini
Normal file
3
examples/quickstart/config.ini
Normal file
|
@ -0,0 +1,3 @@
|
|||
[server]
|
||||
host = "0.0.0.0"
|
||||
port = 8080
|
18
makefile
Normal file
18
makefile
Normal file
|
@ -0,0 +1,18 @@
|
|||
.PHONY: build
|
||||
.PHONY: test
|
||||
.PHONY: clean
|
||||
|
||||
build:
|
||||
dub build --build=release
|
||||
|
||||
build-docs:
|
||||
dub build --build=ddox
|
||||
|
||||
test:
|
||||
dub test
|
||||
|
||||
clean:
|
||||
dub clean
|
||||
|
||||
run-quickstartExample:
|
||||
dub run --build=release --config=quickstartExample
|
|
@ -1,5 +1,5 @@
|
|||
/**
|
||||
* Poodinis Dependency Injection Framework
|
||||
* Mirage Config value injector for the Poodinis Dependency Injection Framework
|
||||
* Copyright 2022 Mike Bierlee
|
||||
* This software is licensed under the terms of the MIT license.
|
||||
* The full terms of the license can be found in the LICENSE file.
|
||||
|
|
15
testmain.d
Normal file
15
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 testmain;
|
||||
|
||||
version (unittest) {
|
||||
void main() {
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue