-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlibexample.cpp
105 lines (86 loc) · 3.41 KB
/
libexample.cpp
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <cpprom/cpprom.hpp>
#include "filecache.hpp"
#include "router.hpp"
#include "tcp.hpp"
#ifdef TLS_SUPPORT_ENABLED
#include "ssl.hpp"
#endif
using namespace std::literals;
static std::string getMimeType(std::string fileExt)
{
static std::unordered_map<std::string, std::string> mimeTypes {
{ "jpg", "image/jpeg" },
{ "html", "text/html" },
};
const auto it = mimeTypes.find(fileExt);
if (it == mimeTypes.end()) {
return "text/plain";
}
return it->second;
}
int main()
{
slog::init(slog::Severity::Debug);
IoQueue io;
FileCache fileCache(io);
Router router;
router.route(Method::Get, "/",
[](const Request&, const Router::RouteParams&) -> Response { return "Hello!"s; });
router.route(Method::Get, "/number/:num",
[](const Request&, const Router::RouteParams& params) -> Response {
return "Number: "s + std::string(params.at("num"));
});
router.route("/headers", [](const Request& req, const Router::RouteParams&) -> Response {
std::string s;
s.reserve(1024);
for (const auto& [name, value] : req.headers.getEntries()) {
s.append("'" + std::string(name) + "' = '" + std::string(value) + "'\n");
}
return s;
});
router.route("/users/:uid", [](const Request&, const Router::RouteParams& params) -> Response {
return "User #'" + std::string(params.at("uid")) + "'";
});
router.route(
"/users/:uid/name", [](const Request&, const Router::RouteParams& params) -> Response {
return "User name for #'" + std::string(params.at("uid")) + "'";
});
router.route("/users/:uid/friends/:fid",
[](const Request&, const Router::RouteParams& params) -> Response {
return "Friend #'" + std::string(params.at("fid")) + "' for user '"
+ std::string(params.at("uid")) + "'";
});
router.route("/users/:uid/files/:path*",
[](const Request&, const Router::RouteParams& params) -> Response {
return "File '" + std::string(params.at("path")) + "' for user '"
+ std::string(params.at("uid")) + "'";
});
router.route("/file/:path*",
[&fileCache](const Request&, const Router::RouteParams& params) -> Response {
const auto path = params.at("path");
const auto f = fileCache.get(std::string(path));
if (!f) {
return Response(StatusCode::NotFound, "Not Found");
}
const auto extDelim = path.find_last_of('.');
const auto ext = path.substr(std::min(extDelim + 1, path.size()));
return Response(f->contents.value(), getMimeType(std::string(ext)));
});
router.route("/metrics",
[&io](const Request&, const Router::RouteParams&, std::shared_ptr<Responder> responder) {
io.async<Response>(
[]() {
return Response(
cpprom::Registry::getDefault().serialize(), "text/plain; version=0.0.4");
},
[responder = std::move(responder)](
std::error_code ec, Response&& response) mutable {
assert(!ec);
responder->respond(std::move(response));
});
});
Server<TcpConnectionFactory> server(io, TcpConnectionFactory {}, router);
server.start();
io.run();
return 0;
}