You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by be...@apache.org on 2021/04/12 20:03:00 UTC
[couchdb] branch 3.x updated: Support uri_file write for chttpd
(#3428)
This is an automated email from the ASF dual-hosted git repository.
bessbd pushed a commit to branch 3.x
in repository https://gitbox.apache.org/repos/asf/couchdb.git
The following commit(s) were added to refs/heads/3.x by this push:
new ed21b64 Support uri_file write for chttpd (#3428)
ed21b64 is described below
commit ed21b643e4ec9f1ed02022855696fd2d5f4cbe0e
Author: Rami Alia <ra...@gmail.com>
AuthorDate: Mon Apr 12 17:02:46 2021 -0300
Support uri_file write for chttpd (#3428)
Co-authored-by: Rami Alia <ra...@hb-studios.com>
---
src/chttpd/src/chttpd_sup.erl | 70 ++++++++++++++++++++++++++++++++++++++++++-
src/couch/src/couch_sup.erl | 51 -------------------------------
2 files changed, 69 insertions(+), 52 deletions(-)
diff --git a/src/chttpd/src/chttpd_sup.erl b/src/chttpd/src/chttpd_sup.erl
index d4bdb11..5bcd4bd 100644
--- a/src/chttpd/src/chttpd_sup.erl
+++ b/src/chttpd/src/chttpd_sup.erl
@@ -26,7 +26,16 @@
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 100, Type, [I]}).
start_link(Args) ->
- supervisor:start_link({local,?MODULE}, ?MODULE, Args).
+ case supervisor:start_link({local, ?MODULE}, ?MODULE, Args) of
+ {ok, _} = Resp ->
+ notify_started(),
+ notify_uris(),
+ write_uris(),
+ Resp;
+ Else ->
+ notify_error(Else),
+ Else
+ end.
init([]) ->
Children = [
@@ -95,3 +104,62 @@ append_if_set({Key, Value}, Opts) ->
"The value for `~s` should be string convertable "
"to integer which is >= 0 (got `~p`)", [Key, Value]),
Opts.
+
+notify_started() ->
+ couch_log:info("Apache CouchDB has started. Time to relax.~n", []).
+
+notify_error(Error) ->
+ couch_log:error("Error starting Apache CouchDB:~n~n ~p~n~n", [Error]).
+
+notify_uris() ->
+ lists:foreach(fun(Uri) ->
+ couch_log:info("Apache CouchDB has started on ~s", [Uri])
+ end, get_uris()).
+
+write_uris() ->
+ case config:get("couchdb", "uri_file", undefined) of
+ undefined ->
+ ok;
+ UriFile ->
+ Lines = [io_lib:format("~s~n", [Uri]) || Uri <- get_uris()],
+ write_file(UriFile, Lines)
+ end.
+
+get_uris() ->
+ Ip = config:get("chttpd", "bind_address"),
+ lists:flatmap(fun(Uri) ->
+ case get_uri(Uri, Ip) of
+ undefined -> [];
+ Else -> [Else]
+ end
+ end, [chttpd, couch_httpd, https]).
+
+get_uri(Name, Ip) ->
+ case get_port(Name) of
+ undefined ->
+ undefined;
+ Port ->
+ io_lib:format("~s://~s:~w/", [get_scheme(Name), Ip, Port])
+ end.
+
+get_scheme(chttpd) -> "http";
+get_scheme(couch_httpd) -> "http";
+get_scheme(https) -> "https".
+
+get_port(Name) ->
+ try
+ mochiweb_socket_server:get(Name, port)
+ catch
+ exit:{noproc, _} ->
+ undefined
+ end.
+
+write_file(FileName, Contents) ->
+ case file:write_file(FileName, Contents) of
+ ok ->
+ ok;
+ {error, Reason} ->
+ Args = [FileName, file:format_error(Reason)],
+ couch_log:error("Failed ot write ~s :: ~s", Args),
+ throw({error, Reason})
+ end.
diff --git a/src/couch/src/couch_sup.erl b/src/couch/src/couch_sup.erl
index 6e7ef98..b936c1e 100644
--- a/src/couch/src/couch_sup.erl
+++ b/src/couch/src/couch_sup.erl
@@ -36,8 +36,6 @@ start_link() ->
case supervisor:start_link({local, ?MODULE}, ?MODULE, []) of
{ok, _} = Resp ->
notify_started(),
- notify_uris(),
- write_uris(),
Resp;
Else ->
notify_error(Else),
@@ -131,12 +129,6 @@ notify_error(Error) ->
couch_log:error("Error starting Apache CouchDB:~n~n ~p~n~n", [Error]).
-notify_uris() ->
- lists:foreach(fun(Uri) ->
- couch_log:info("Apache CouchDB has started on ~s", [Uri])
- end, get_uris()).
-
-
write_pidfile() ->
case init:get_argument(pidfile) of
{ok, [PidFile]} ->
@@ -145,49 +137,6 @@ write_pidfile() ->
ok
end.
-
-write_uris() ->
- case config:get("couchdb", "uri_file", undefined) of
- undefined ->
- ok;
- UriFile ->
- Lines = [io_lib:format("~s~n", [Uri]) || Uri <- get_uris()],
- write_file(UriFile, Lines)
- end.
-
-
-get_uris() ->
- Ip = config:get("chttpd", "bind_address"),
- lists:flatmap(fun(Uri) ->
- case get_uri(Uri, Ip) of
- undefined -> [];
- Else -> [Else]
- end
- end, [couch_httpd, https]).
-
-
-get_uri(Name, Ip) ->
- case get_port(Name) of
- undefined ->
- undefined;
- Port ->
- io_lib:format("~s://~s:~w/", [get_scheme(Name), Ip, Port])
- end.
-
-
-get_scheme(couch_httpd) -> "http";
-get_scheme(https) -> "https".
-
-
-get_port(Name) ->
- try
- mochiweb_socket_server:get(Name, port)
- catch
- exit:{noproc, _} ->
- undefined
- end.
-
-
write_file(FileName, Contents) ->
case file:write_file(FileName, Contents) of
ok ->