-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.d
50 lines (42 loc) · 1.54 KB
/
app.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import vibe.http.server;
import vibe.stream.tls;
import vibe.http.internal.http2.http2 : http2Callback; // ALPN negotiation
import vibe.core.core : runApplication;
void handleTestImage(scope HTTPServerRequest req, scope HTTPServerResponse res)
@safe {
}
void handleRequest(scope HTTPServerRequest req, scope HTTPServerResponse res)
@safe {
if (req.path == "/") {
bool isHTTP2 = req.httpVersion == HTTPVersion.HTTP_2;
res.headers["Content-Type"] = "text/html";
res.render!("index.dt", req, isHTTP2);
} else if(req.path == "/request") {
string reply = "BEGIN dump of received request:\n---\n";
reply ~= httpMethodString(req.method) ~ " " ~ req.path ~ " " ~ getHTTPVersionString(req.httpVersion);
foreach(hkey, hvalue; req.headers.byKeyValue) {
reply ~= "\n" ~ hkey ~ ": " ~ hvalue;
}
reply ~= "\n---\nEND of dump";
res.writeBody(reply);
} else if(req.path == "/image") {
handleTestImage(req, res);
}
}
void main()
{
import vibe.core.log;
setLogLevel(LogLevel.trace);
/* ========== HTTPS (h2) support ========== */
HTTPServerSettings tlsSettings;
tlsSettings.bindAddresses = ["0.0.0.0"];
tlsSettings.port = 46785;
/// setup TLS context by using cert and key in example rootdir
tlsSettings.tlsContext = createTLSContext(TLSContextKind.server);
tlsSettings.tlsContext.useCertificateChainFile("server.crt");
tlsSettings.tlsContext.usePrivateKeyFile("server.key");
// set alpn callback to support HTTP/2 protocol negotiation
tlsSettings.tlsContext.alpnCallback(http2Callback);
listenHTTP!handleRequest(tlsSettings);
runApplication();
}