-
Notifications
You must be signed in to change notification settings - Fork 16
/
router.cpp
41 lines (32 loc) · 1.22 KB
/
router.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
#include "expresscpp/console.hpp"
#include "expresscpp/expresscpp.hpp"
using namespace expresscpp;
int main() {
ExpressCpp app;
app.Get("/a", [](auto /*req*/, auto res, auto /*next*/) { res->Send("get_a"); });
app.Get("/b", [](auto /*req*/, auto res, auto /*next*/) { res->Send("get_b"); });
auto router = app.GetRouter();
router->Use([](auto req, auto res, auto next) {
if (req->getHeader("Authorization") == "secret_token") {
return next();
}
res->SetStatus(401);
res->Send("Access denied");
});
router->Get("/a", [](auto /*req*/, auto res, auto /*next*/) { res->Send("get_api_v0_users_a"); });
router->Get("/b", [](auto /*req*/, auto res, auto /*next*/) { res->Send("get_api_v0_users_b"); });
app.Use("/api/v0/users", router);
constexpr uint16_t port = 8081;
app.Listen(port,
[&](auto ec) {
if (ec) {
std::cerr << "ERROR: " << ec.message() << std::endl;
std::cerr << "exiting..." << std::endl;
exit(1);
}
std::cout << fmt::format(R"(you can try now: "curl http://localhost:{}/api/v0/users")", port)
<< std::endl;
})
.Run();
return 0;
}