Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

refactor(http): add http handler without creating http service #615

Merged

Conversation

neverchanje
Copy link
Contributor

@neverchanje neverchanje commented Sep 7, 2020

What problem do I solve?

To register an HTTP handler, we previously needed to define an http_service subclass before writing the exact handler.

class recent_start_time_http_service : public http_service
{
public:
    recent_start_time_http_service()
    {
        register_handler("",
                         std::bind(&recent_start_time_http_service::get_recent_start_time_handler,
                                   this,
                                   std::placeholders::_1,
                                   std::placeholders::_2),
                         "ip:port/recentStartTime");
    }

    std::string path() const override { return "recentStartTime"; }

    void get_recent_start_time_handler(const http_request &req, http_response &resp);
};

As we can see, http_service is too heavy to implement a lightweight handler.

How do I solve this problem

We can simplify this short type handler by using register_http_call, a new API of the library.

    register_http_call("recentStartTime")
        .with_callback([](const http_request &req, http_response &resp) {
            get_recent_start_time_handler(req, resp);
        })
        .with_help("Gets the server start time.");

Remember that we use register_http_service to register an http_sevice. I did to http_call likewise. register_http_call is designed to be a convenience utility that could boost our future development of new HTTP API.

Known issue

In this PR I removed version_http_service. This http call will be registered during a pegasus_service_app is created. So I will commit another PR for this.

@neverchanje neverchanje added the component/http HTTP/RESTful support label Sep 7, 2020
auto call_ptr = dsn::make_unique<http_call>();
call_ptr->path = std::move(full_path);
http_call &call = *call_ptr;
// register this call
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the comment doesn't make sense. Maybe you can remove it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. Any other problems?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no

@neverchanje neverchanje merged commit 6d4f9c4 into XiaoMi:master Sep 9, 2020
@neverchanje neverchanje deleted the http-refactoring-server-decouple branch September 9, 2020 08:28
@neverchanje neverchanje added 2.1.0 and removed component/http HTTP/RESTful support labels Mar 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants