Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(http): refactoring on http server #482

Closed
neverchanje opened this issue Feb 19, 2020 · 0 comments
Closed

refactor(http): refactoring on http server #482

neverchanje opened this issue Feb 19, 2020 · 0 comments
Assignees

Comments

@neverchanje
Copy link
Contributor

neverchanje commented Feb 19, 2020

We have implemented many HTTP APIs so far, some of the codes repeat each time a new API is added.

  • argument validation. For example, /meta/app?name=<app_name> requires "name" to be provided, if not, it replies with "bad_request". This case is general. We can eliminate the following codes by filtering illegal requests in http_service::call before the handler.
void meta_http_service::get_app_handler(const http_request &req, http_response &resp)
{
    std::string app_name;
    bool detailed = false;
    for (const auto &p : req.query_args) {
        if (p.first == "name") {
            app_name = p.second;
        } else if (p.first == "detail") {
            detailed = true;
        } else {
            resp.status_code = http_status_code::bad_request;
            return;
        }
    }
  • Thrift-struct conversion to human-readable JSON. Currently table_printer is the general-used utility for this purpose. However table_printer is designed to support both "table" and "JSON" format, for JSON format, we should use a dedicated JSON library, like rapidjson, and nlohmann/json.

  • redirect_if_not_primary. We add primary-meta-check in every handler. We can eliminate them by prepending this call before meta_http_service::call.

void meta_http_service::get_app_envs_handler(const http_request &req, http_response &resp)
{
    if (!redirect_if_not_primary(req, resp))
        return;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants