mirror of
https://github.com/CrowCpp/Crow.git
synced 2024-06-07 21:10:44 +00:00
55 lines
1.2 KiB
C++
55 lines
1.2 KiB
C++
#include "crow.h"
|
|
|
|
struct RequestLogger
|
|
{
|
|
struct context
|
|
{};
|
|
|
|
void before_handle(crow::request& req, crow::response& /*res*/, context& /*ctx*/)
|
|
{
|
|
CROW_LOG_INFO << "Request to:" + req.url;
|
|
}
|
|
|
|
void after_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/)
|
|
{}
|
|
};
|
|
|
|
// Per handler middleware has to extend ILocalMiddleware
|
|
// It is called only if enabled
|
|
struct SecretContentGuard : crow::ILocalMiddleware
|
|
{
|
|
struct context
|
|
{};
|
|
|
|
void before_handle(crow::request& /*req*/, crow::response& res, context& /*ctx*/)
|
|
{
|
|
res.write("SECRET!");
|
|
res.code = 403;
|
|
res.end();
|
|
}
|
|
|
|
void after_handle(crow::request& /*req*/, crow::response& /*res*/, context& /*ctx*/)
|
|
{}
|
|
};
|
|
|
|
int main()
|
|
{
|
|
// ALL middleware (including per handler) is listed
|
|
crow::App<RequestLogger, SecretContentGuard> app;
|
|
|
|
CROW_ROUTE(app, "/")
|
|
([]() {
|
|
return "Hello, world!";
|
|
});
|
|
|
|
CROW_ROUTE(app, "/secret")
|
|
// Enable SecretContentGuard for this handler
|
|
.middlewares<decltype(app), SecretContentGuard>()([]() {
|
|
return "";
|
|
});
|
|
|
|
app.port(18080).run();
|
|
|
|
return 0;
|
|
}
|