Skip to content

Commit

Permalink
fix: add /version http api (#824)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smityz authored Apr 25, 2021
1 parent c49e6fd commit ad9d61d
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/http/builtin_http_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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];
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/http/builtin_http_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 <meta_server_ipport>/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);
Expand Down
24 changes: 24 additions & 0 deletions src/http/service_version.cpp
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions src/http/service_version.h
Original file line number Diff line number Diff line change
@@ -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 <string>

namespace dsn {

struct service_version
{
std::string version;
std::string git_commit;
};

extern service_version app_version;

} // namespace dsn
11 changes: 11 additions & 0 deletions src/http/test/http_server_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
5 changes: 5 additions & 0 deletions src/meta/meta_service_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include "meta_service.h"
#include "meta_http_service.h"
#include "http/service_version.h"

namespace dsn {
namespace service {
Expand Down Expand Up @@ -94,6 +95,10 @@ meta_service_app::~meta_service_app() {}

error_code meta_service_app::start(const std::vector<std::string> &args)
{
if (args.size() >= 2) {
app_version.version = *(args.end() - 2);
app_version.git_commit = *(args.end() - 1);
}
return _service->start();
}

Expand Down
6 changes: 6 additions & 0 deletions src/replica/replication_service_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -52,6 +53,11 @@ replication_service_app::~replication_service_app(void) {}

error_code replication_service_app::start(const std::vector<std::string> &args)
{
if (args.size() >= 2) {
app_version.version = *(args.end() - 2);
app_version.git_commit = *(args.end() - 1);
}

replication_options opts;
opts.initialize();

Expand Down

0 comments on commit ad9d61d

Please sign in to comment.