-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(http): add http handler without creating http service (#615)
- Loading branch information
Wu Tao
authored
Sep 9, 2020
1 parent
4425703
commit 6d4f9c4
Showing
16 changed files
with
181 additions
and
227 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.