From ad9d61dfed8cc570ea267999f292f11ad5626767 Mon Sep 17 00:00:00 2001 From: Smilencer <527646889@qq.com> Date: Sun, 25 Apr 2021 15:30:10 +0800 Subject: [PATCH] fix: add /version http api (#824) --- src/http/builtin_http_calls.cpp | 19 +++++++++++++++ src/http/builtin_http_calls.h | 8 +++++++ src/http/service_version.cpp | 24 +++++++++++++++++++ src/http/service_version.h | 32 +++++++++++++++++++++++++ src/http/test/http_server_test.cpp | 11 +++++++++ src/meta/meta_service_app.cpp | 5 ++++ src/replica/replication_service_app.cpp | 6 +++++ 7 files changed, 105 insertions(+) create mode 100644 src/http/service_version.cpp create mode 100644 src/http/service_version.h diff --git a/src/http/builtin_http_calls.cpp b/src/http/builtin_http_calls.cpp index 6a8397a0ff..d1b2620bf1 100644 --- a/src/http/builtin_http_calls.cpp +++ b/src/http/builtin_http_calls.cpp @@ -21,6 +21,7 @@ #include "builtin_http_calls.h" #include "http_call_registry.h" #include "pprof_http_service.h" +#include "service_version.h" namespace dsn { @@ -37,6 +38,19 @@ namespace dsn { resp.status_code = http_status_code::ok; } +/*extern*/ void get_version_handler(const http_request &req, http_response &resp) +{ + std::ostringstream out; + dsn::utils::table_printer tp; + + tp.add_row_name_and_data("Version", app_version.version); + tp.add_row_name_and_data("GitCommit", app_version.git_commit); + tp.output(out, dsn::utils::table_printer::output_format::kJsonCompact); + + resp.body = out.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]; @@ -61,6 +75,11 @@ namespace dsn { [](const http_request &req, http_response &resp) { get_help_handler(req, resp); }) .with_help("Lists all supported calls"); + register_http_call("version") + .with_callback( + [](const http_request &req, http_response &resp) { get_version_handler(req, resp); }) + .with_help("Gets the server version."); + register_http_call("recentStartTime") .with_callback([](const http_request &req, http_response &resp) { get_recent_start_time_handler(req, resp); diff --git a/src/http/builtin_http_calls.h b/src/http/builtin_http_calls.h index c69bb1e330..89e961f268 100644 --- a/src/http/builtin_http_calls.h +++ b/src/http/builtin_http_calls.h @@ -30,6 +30,14 @@ extern void get_perf_counter_handler(const http_request &req, http_response &res extern void get_help_handler(const http_request &req, http_response &resp); +// Get /version +// Request body: +// { +// Version: "2.1.SNAPSHOT", +// GitCommit: "88783e1ec28c326974f808d91c1531391d38acb5" +// } +extern void get_version_handler(const http_request &req, http_response &resp); + extern void get_recent_start_time_handler(const http_request &req, http_response &resp); extern void update_config(const http_request &req, http_response &resp); diff --git a/src/http/service_version.cpp b/src/http/service_version.cpp new file mode 100644 index 0000000000..224591b75d --- /dev/null +++ b/src/http/service_version.cpp @@ -0,0 +1,24 @@ +// 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 "service_version.h" + +namespace dsn { + +service_version app_version; + +} // namespace dsn diff --git a/src/http/service_version.h b/src/http/service_version.h new file mode 100644 index 0000000000..16c7b2fea1 --- /dev/null +++ b/src/http/service_version.h @@ -0,0 +1,32 @@ +// 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 + +namespace dsn { + +struct service_version +{ + std::string version; + std::string git_commit; +}; + +extern service_version app_version; + +} // namespace dsn diff --git a/src/http/test/http_server_test.cpp b/src/http/test/http_server_test.cpp index d23313d546..a76549e45a 100644 --- a/src/http/test/http_server_test.cpp +++ b/src/http/test/http_server_test.cpp @@ -59,6 +59,17 @@ TEST(http_server, parse_url) } } +TEST(bultin_http_calls_test, meta_query) +{ + http_request req; + http_response resp; + get_recent_start_time_handler(req, resp); + ASSERT_EQ(resp.status_code, http_status_code::ok); + + get_version_handler(req, resp); + ASSERT_EQ(resp.status_code, http_status_code::ok); +} + TEST(bultin_http_calls_test, get_help) { for (const auto &call : http_call_registry::instance().list_all_calls()) { diff --git a/src/meta/meta_service_app.cpp b/src/meta/meta_service_app.cpp index aaec4e5faa..d612e005d1 100644 --- a/src/meta/meta_service_app.cpp +++ b/src/meta/meta_service_app.cpp @@ -40,6 +40,7 @@ #include "meta_service.h" #include "meta_http_service.h" +#include "http/service_version.h" namespace dsn { namespace service { @@ -94,6 +95,10 @@ meta_service_app::~meta_service_app() {} error_code meta_service_app::start(const std::vector &args) { + if (args.size() >= 2) { + app_version.version = *(args.end() - 2); + app_version.git_commit = *(args.end() - 1); + } return _service->start(); } diff --git a/src/replica/replication_service_app.cpp b/src/replica/replication_service_app.cpp index 3490882f4f..bef5b6e65e 100644 --- a/src/replica/replication_service_app.cpp +++ b/src/replica/replication_service_app.cpp @@ -30,6 +30,7 @@ #include "common/replication_common.h" #include "replica_stub.h" #include "replica_http_service.h" +#include "http/service_version.h" namespace dsn { namespace replication { @@ -52,6 +53,11 @@ replication_service_app::~replication_service_app(void) {} error_code replication_service_app::start(const std::vector &args) { + if (args.size() >= 2) { + app_version.version = *(args.end() - 2); + app_version.git_commit = *(args.end() - 1); + } + replication_options opts; opts.initialize();