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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion include/dsn/dist/replication/meta_service_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ class meta_service_app : public service_app
private:
friend class ::dsn::replication::test::test_checker;
std::unique_ptr<dsn::replication::meta_service> _service;
version_http_service *_version_http_service;
hycdong marked this conversation as resolved.
Show resolved Hide resolved
};
} // namespace service
} // namespace dsn
4 changes: 0 additions & 4 deletions include/dsn/dist/replication/replication_service_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ class replication_service_app : public ::dsn::service_app
private:
friend class ::dsn::replication::test::test_checker;
replica_stub_ptr _stub;
version_http_service *_version_http_service;

static const char *replica_service_app_info(int argc, char **argv);
static void replica_service_app_info_free(const char *response);
};
} // namespace replication
} // namespace dsn
30 changes: 26 additions & 4 deletions include/dsn/http/http_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

#pragma once

#include <dsn/c/api_common.h>
#include <dsn/tool-api/rpc_message.h>
#include <dsn/cpp/serverlet.h>
#include <dsn/utility/errors.h>
#include <dsn/utility/flags.h>

Expand All @@ -20,6 +17,7 @@ enum http_method
HTTP_METHOD_POST = 2,
};

class message_ex;
struct http_request
{
static error_with<http_request> parse(dsn::message_ex *m);
Expand Down Expand Up @@ -59,6 +57,17 @@ struct http_call
std::string path;
std::string help;
http_callback callback;

http_call &with_callback(http_callback cb)
{
callback = std::move(cb);
return *this;
}
http_call &with_help(std::string hp)
{
help = std::move(hp);
return *this;
}
};

// A suite of HTTP handlers coupled using the same prefix of the service.
Expand All @@ -74,11 +83,24 @@ class http_service
void register_handler(std::string path, http_callback cb, std::string help);
};

// Example:
//
// ```
// register_http_call("/meta/app")
// .with_callback(std::bind(&meta_http_service::get_app_handler,
// this,
// std::placeholders::_1,
// std::placeholders::_2))
// .with_help("Gets the app information")
// .add_argument("app_name", HTTP_ARG_STRING);
// ```
extern http_call &register_http_call(std::string full_path);

// Starts serving HTTP requests.
// The internal HTTP server will reuse the rDSN server port.
extern void start_http_server();

// NOTE: the memory of `svc` will be transfered to the underlying registry.
// NOTE: the memory of `svc` will be transferred to the underlying registry.
// TODO(wutao): pass `svc` as a std::unique_ptr.
extern void register_http_service(http_service *svc);

Expand Down
77 changes: 77 additions & 0 deletions src/http/builtin_http_calls.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#include <dsn/utility/output_utils.h>
#include <dsn/utility/time_utils.h>

#include "builtin_http_calls.h"
#include "http_call_registry.h"
#include "pprof_http_service.h"

namespace dsn {

/*extern*/ void get_help_handler(const http_request &req, http_response &resp)
{
utils::table_printer tp;
std::ostringstream oss;
auto calls = http_call_registry::instance().list_all_calls();
for (const auto &call : calls) {
tp.add_row_name_and_data(std::string("/") + call->path, call->help);
}
tp.output(oss, utils::table_printer::output_format::kJsonCompact);
resp.body = oss.str();
resp.status_code = http_status_code::ok;
}

/*extern*/ void get_recent_start_time_handler(const http_request &req, http_response &resp)
{
char start_time[100];
dsn::utils::time_ms_to_date_time(dsn::utils::process_start_millis(), start_time, 100);
std::ostringstream out;
dsn::utils::table_printer tp;
tp.add_row_name_and_data("RecentStartTime", start_time);
tp.output(out, dsn::utils::table_printer::output_format::kJsonCompact);

resp.body = out.str();
resp.status_code = http_status_code::ok;
}

/*extern*/ void register_builtin_http_calls()
{
#ifdef DSN_ENABLE_GPERF
static pprof_http_service pprof_svc;
#endif

register_http_call("")
.with_callback(
[](const http_request &req, http_response &resp) { get_help_handler(req, resp); })
.with_help("Lists all supported calls");

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.");

register_http_call("perfCounter")
.with_callback([](const http_request &req, http_response &resp) {
get_perf_counter_handler(req, resp);
})
.with_help("Gets the value of a perf counter");
}

} // namespace dsn
35 changes: 35 additions & 0 deletions src/http/builtin_http_calls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

#pragma once

#include <dsn/cpp/serverlet.h>
#include <dsn/http/http_server.h>
#include <dsn/utility/errors.h>

namespace dsn {

// Register basic services for the HTTP server.
extern void register_builtin_http_calls();

extern void get_perf_counter_handler(const http_request &req, http_response &resp);

extern void get_help_handler(const http_request &req, http_response &resp);

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

} // namespace dsn
26 changes: 17 additions & 9 deletions src/http/http_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

#include <dsn/http/http_server.h>
#include <dsn/tool_api.h>
#include <dsn/utility/time_utils.h>
#include <boost/algorithm/string.hpp>
#include <fmt/ostream.h>

#include "http_message_parser.h"
#include "root_http_service.h"
#include "pprof_http_service.h"
#include "perf_counter_http_service.h"
#include "builtin_http_calls.h"
#include "uri_decoder.h"
#include "http_call_registry.h"
#include "http_server_impl.h"
Expand Down Expand Up @@ -38,6 +38,20 @@ DSN_DEFINE_bool("http", enable_http_server, true, "whether to enable the embedde
}
}

/*extern*/ http_call &register_http_call(std::string full_path)
{
auto call_ptr = dsn::make_unique<http_call>();
call_ptr->path = std::move(full_path);
http_call &call = *call_ptr;
http_call_registry::instance().add(std::move(call_ptr));
return call;
}

/*extern*/ void deregister_http_call(const std::string &full_path)
{
http_call_registry::instance().remove(full_path);
}

void http_service::register_handler(std::string path, http_callback cb, std::string help)
{
if (!FLAGS_enable_http_server) {
Expand All @@ -64,13 +78,7 @@ http_server::http_server() : serverlet<http_server>("http_server")
tools::register_message_header_parser<http_message_parser>(NET_HDR_HTTP, {"GET ", "POST"});

// add builtin services
register_http_service(new root_http_service());

#ifdef DSN_ENABLE_GPERF
register_http_service(new pprof_http_service());
hycdong marked this conversation as resolved.
Show resolved Hide resolved
#endif // DSN_ENABLE_GPERF

register_http_service(new perf_counter_http_service());
register_builtin_http_calls();
}

void http_server::serve(message_ex *msg)
Expand Down
5 changes: 2 additions & 3 deletions src/http/perf_counter_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
// can be found in the LICENSE file in the root directory of this source tree.

#include <dsn/utility/output_utils.h>
#include "perf_counter_http_service.h"
#include "builtin_http_calls.h"

namespace dsn {

void perf_counter_http_service::get_perf_counter_handler(const http_request &req,
http_response &resp)
void get_perf_counter_handler(const http_request &req, http_response &resp)
{
std::string perf_counter_name;
for (const auto &p : req.query_args) {
Expand Down
28 changes: 0 additions & 28 deletions src/http/perf_counter_http_service.h

This file was deleted.

2 changes: 2 additions & 0 deletions src/http/pprof_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include "pprof_http_service.h"

#include <dsn/dist/fmt_logging.h>
#include <dsn/c/api_layer1.h>
#include <dsn/utility/process_utils.h>
#include <dsn/utility/string_conv.h>
#include <dsn/utility/defer.h>
#include <dsn/utility/timer.h>
Expand Down
43 changes: 0 additions & 43 deletions src/http/root_http_service.h

This file was deleted.

38 changes: 0 additions & 38 deletions src/http/server_info_http_services.cpp

This file was deleted.

Loading