Skip to content

Commit faa4fc8

Browse files
committed
Extract the response of _versions endpoint
When running with old version of Clouseau, it will return `{ok, null}` when we call `clouseau_rpc:version().`. So add `is_binary/1` to prevent `badarg` errors. Also simplify the code by extracting the response to `get_versions/0` function.
1 parent 121ac7c commit faa4fc8

File tree

1 file changed

+47
-42
lines changed

1 file changed

+47
-42
lines changed

src/chttpd/src/chttpd_node.erl

Lines changed: 47 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
-export([
1717
handle_node_req/1,
1818
get_stats/0,
19+
get_versions/0,
1920
run_queues/0,
2021
message_queues/0,
2122
db_pid_stats/0
@@ -46,48 +47,10 @@ handle_node_req(#httpd{method = 'GET', path_parts = [_, _Node, <<"_smoosh">>, <<
4647
handle_node_req(#httpd{path_parts = [_, _Node, <<"_smoosh">>, <<"status">>]} = Req) ->
4748
send_method_not_allowed(Req, "GET");
4849
% GET /_node/$node/_versions
49-
handle_node_req(#httpd{method = 'GET', path_parts = [_, _Node, <<"_versions">>]} = Req) ->
50-
IcuVer = couch_ejson_compare:get_icu_version(),
51-
UcaVer = couch_ejson_compare:get_uca_version(),
52-
ColVer = couch_ejson_compare:get_collator_version(),
53-
Hashes = crypto:supports(hashs),
54-
EngineName = couch_server:get_js_engine(),
55-
JsEngine =
56-
case EngineName of
57-
<<"spidermonkey">> ->
58-
#{
59-
name => EngineName,
60-
version => couch_server:get_spidermonkey_version()
61-
};
62-
_Other ->
63-
#{name => EngineName}
64-
end,
65-
ClouseauResponse =
66-
case clouseau_rpc:version() of
67-
{ok, Version} ->
68-
#{
69-
clouseau => #{
70-
version => Version
71-
}
72-
};
73-
_ ->
74-
#{}
75-
end,
76-
BaseResponse = #{
77-
erlang => #{
78-
version => ?l2b(?COUCHDB_ERLANG_VERSION),
79-
supported_hashes => Hashes
80-
},
81-
collation_driver => #{
82-
name => <<"libicu">>,
83-
library_version => couch_util:version_to_binary(IcuVer),
84-
collation_algorithm_version => couch_util:version_to_binary(UcaVer),
85-
collator_version => couch_util:version_to_binary(ColVer)
86-
},
87-
javascript_engine => JsEngine
88-
},
89-
Response = maps:merge(BaseResponse, ClouseauResponse),
90-
send_json(Req, 200, Response);
50+
handle_node_req(#httpd{method = 'GET', path_parts = [_, Node, <<"_versions">>]} = Req) ->
51+
Versions = call_node(Node, chttpd_node, get_versions, []),
52+
EJSON = couch_stats_httpd:to_ejson(Versions),
53+
send_json(Req, EJSON);
9154
handle_node_req(#httpd{path_parts = [_, _Node, <<"_versions">>]} = Req) ->
9255
send_method_not_allowed(Req, "GET");
9356
% GET /_node/$node/_config
@@ -339,6 +302,48 @@ get_stats() ->
339302
{distribution_events, mem3_distribution:events()}
340303
].
341304

305+
get_versions() ->
306+
IcuVer = couch_ejson_compare:get_icu_version(),
307+
UcaVer = couch_ejson_compare:get_uca_version(),
308+
ColVer = couch_ejson_compare:get_collator_version(),
309+
Hashes = crypto:supports(hashs),
310+
EngineName = couch_server:get_js_engine(),
311+
JsEngine =
312+
case EngineName of
313+
<<"spidermonkey">> ->
314+
#{
315+
name => EngineName,
316+
version => couch_server:get_spidermonkey_version()
317+
};
318+
_Other ->
319+
#{name => EngineName}
320+
end,
321+
ClouseauResponse =
322+
case clouseau_rpc:version() of
323+
{ok, Version} when is_binary(Version) ->
324+
#{
325+
clouseau => #{
326+
version => Version
327+
}
328+
};
329+
_ ->
330+
#{}
331+
end,
332+
BaseResponse = #{
333+
erlang => #{
334+
version => ?l2b(?COUCHDB_ERLANG_VERSION),
335+
supported_hashes => Hashes
336+
},
337+
collation_driver => #{
338+
name => <<"libicu">>,
339+
library_version => couch_util:version_to_binary(IcuVer),
340+
collation_algorithm_version => couch_util:version_to_binary(UcaVer),
341+
collator_version => couch_util:version_to_binary(ColVer)
342+
},
343+
javascript_engine => JsEngine
344+
},
345+
maps:merge(BaseResponse, ClouseauResponse).
346+
342347
db_pid_stats_formatted() ->
343348
{CF, CDU} = db_pid_stats(),
344349
{format_pid_stats(CF), format_pid_stats(CDU)}.

0 commit comments

Comments
 (0)