2017-02-13 20:20:35 +01:00
|
|
|
/**
|
|
|
|
* Poodinis Dependency Injection Framework
|
2024-02-17 13:08:59 +01:00
|
|
|
* Copyright 2014-2024 Mike Bierlee
|
2017-02-13 20:20:35 +01:00
|
|
|
* 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 poodinis;
|
|
|
|
|
|
|
|
import std.stdio;
|
|
|
|
import std.string;
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class IntValueInjector : ValueInjector!int {
|
|
|
|
int get(string key) {
|
|
|
|
switch (key) {
|
2021-05-01 21:16:44 +02:00
|
|
|
case "http.port":
|
|
|
|
return 8080;
|
|
|
|
case "http.keep_alive":
|
|
|
|
return 60;
|
|
|
|
default:
|
|
|
|
throw new ValueNotAvailableException(key);
|
2017-02-13 20:20:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class StringValueInjector : ValueInjector!string {
|
|
|
|
string get(string key) {
|
|
|
|
switch (key) {
|
2021-05-01 21:16:44 +02:00
|
|
|
case "http.hostname":
|
|
|
|
return "acme.org";
|
|
|
|
default:
|
|
|
|
throw new ValueNotAvailableException(key);
|
2017-02-13 20:20:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
class HttpServer {
|
2017-02-13 20:20:35 +01:00
|
|
|
|
|
|
|
@Value("http.port")
|
|
|
|
private int port = 80;
|
|
|
|
|
|
|
|
@Value("http.hostname")
|
|
|
|
private string hostName = "localhost";
|
|
|
|
|
|
|
|
@Value("http.max_connections")
|
|
|
|
private int maxConnections = 1000; // Default assignment is kept because max_connections is not available within the injector
|
|
|
|
|
|
|
|
@MandatoryValue("http.keep_alive")
|
|
|
|
private int keepAliveTime; // A ResolveException is thrown when the value is not available, default assignments are not used.
|
|
|
|
|
2023-05-17 23:03:02 +02:00
|
|
|
void serve() {
|
2021-05-01 21:16:44 +02:00
|
|
|
writeln(format("Serving pages for %s:%s with max connection count of %s",
|
|
|
|
hostName, port, maxConnections));
|
2017-02-13 20:20:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 23:24:18 +01:00
|
|
|
void main() {
|
2017-02-13 20:20:35 +01:00
|
|
|
auto dependencies = new shared DependencyContainer();
|
|
|
|
dependencies.register!(ValueInjector!int, IntValueInjector);
|
|
|
|
dependencies.register!(ValueInjector!string, StringValueInjector);
|
|
|
|
dependencies.register!HttpServer;
|
|
|
|
|
|
|
|
auto server = dependencies.resolve!HttpServer;
|
|
|
|
server.serve(); // Prints "Serving pages for acme.org:8080 with max connection count of 1000"
|
|
|
|
}
|