-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
44 lines (36 loc) · 1.35 KB
/
Copy pathmain.cpp
File metadata and controls
44 lines (36 loc) · 1.35 KB
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
// 06_https — TLS with a self-signed development certificate.
//
// Generate a cert first (helper script is copied next to the binary):
// ./gen_cert.sh # writes server.crt + server.key
// ./example_06_https
//
// Try it:
// curl -k https://localhost:8443/
//
// Certificate paths can also come from the environment:
// SOCKETIFY_CERT_FILE=server.crt SOCKETIFY_KEY_FILE=server.key ./example_06_https
#include <socketify/socketify.h>
#include <cstdio>
using namespace socketify;
int main() {
ServerOptions opts;
// Prefer env vars (SOCKETIFY_CERT_FILE / SOCKETIFY_KEY_FILE), fall back
// to files in the working directory.
if (auto env = TlsOptions::from_env()) {
opts.tls = *env;
} else {
opts.tls = TlsOptions{.cert_file = "server.crt", .key_file = "server.key"};
}
Server server(opts);
server.Get("/", [](Request& req, Response& res) {
res.json({{"secure", true}, {"client", std::string(req.remote_ip())}});
});
if (!server.Run("0.0.0.0", 8443)) {
std::fprintf(stderr, "failed to start: %s\n", server.last_error().c_str());
std::fprintf(stderr, "hint: run ./gen_cert.sh first to create server.crt/server.key\n");
return 1;
}
std::printf("HTTPS on https://localhost:8443 (curl -k to accept the self-signed cert)\n");
server.Wait();
return 0;
}