#include <drogon/drogon.h>

using namespace drogon;

class GlobalMid : public HttpMiddleware<GlobalMid, false> {
public:
    void invoke(const HttpRequestPtr &req,
                MiddlewareNextCallback &&next,
                MiddlewareCallback &&done) override {

        LOG_DEBUG << "进入全局中间件：" << req->path();
        next([done](auto resp) {
            resp->addHeader("X-Global", "Yes");
            LOG_DEBUG << "离开全局中间件";
            done(resp);
        });
    }
};

int main() {
    app().registerMiddleware(std::make_shared<GlobalMid>());

    app().registerHandler("/hello", [](const HttpRequestPtr &request,
       std::function<void(const HttpResponsePtr &)> &&cb){
        auto resp = HttpResponse::newHttpResponse();
        resp->setBody("OK");
        cb(resp);
    }, {Get});

    app().addListener("0.0.0.0", 80);
    app().run();
}