2 %% Copyright (C) 2006 Damien Katz
4 %% This program is free software; you can redistribute it and/or
5 %% modify it under the terms of the GNU General Public License
6 %% as published by the Free Software Foundation; either version 2
7 %% of the License, or (at your option) any later version.
9 %% This program is distributed in the hope that it will be useful,
10 %% but WITHOUT ANY WARRANTY; without even the implied warranty of
11 %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 %% GNU General Public License for more details.
14 %% You should have received a copy of the GNU General Public License
15 %% along with this program; if not, write to the Free Software
16 %% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 -module(couch_server_sup
).
19 -behaviour(supervisor
).
21 -define(DEFAULT_INI
, "couch.ini").
23 -export([start_link
/1,stop
/0]).
25 %% supervisor callbacks
28 start_link(IniFilename
) ->
29 case whereis(couch_server_sup
) of
31 start_server(IniFilename
);
33 {error
, already_started
}
36 start_server(IniFilename
) ->
40 % no ini file specified, check the command line args
41 case init:get_argument(couch
) of
43 case lists:keysearch("ini", 1, Args
) of
44 {value
, {"ini", Filename
}} ->
45 file:read_file(Filename
);
53 % if IniFilename is specified, it is used
54 file:read_file(IniFilename
)
56 {ok
, Ini
} = couch_util:parse_ini(binary_to_list(IniBin
)),
57 ConsoleStartupMsg
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupMsg", "CouchDb server starting")),
58 ConsoleStartupCompleteMsg
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "ConsoleStartupCompleteMsg", "CouchDb server started")),
59 LogLevel
= list_to_atom(couch_util:ini_get_value(Ini
, "Couch", "LogLevel", "error")),
60 DbRootDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "DbRootDir", ".")),
61 HttpConfigFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "HttpConfigFile", "couch_httpd.conf")),
62 FabricServer
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "FabricServer", "FabricServer")),
63 LogFile
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "LogFile", "couchdb.log")),
64 UtilDriverDir
= couch_util:abs_pathname(couch_util:ini_get_value(Ini
, "Couch", "UtilDriverDir", ".")),
66 io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel
]),
68 io:format("~s~n", [ConsoleStartupMsg
]),
70 % start the couch logger
71 ok
= couch_log:start(LogFile
, LogLevel
),
73 couch_log:debug("Startup Info:~n\tDbRootDir=~p~n\tHttpConfigFile=~p~n\tFabricServer=~p~n\tLogFile=~p~n\tUtilDriverDir=~p",
74 [DbRootDir
, HttpConfigFile
, FabricServer
, LogFile
, UtilDriverDir
]),
76 StartResult
= supervisor:start_link({local
, couch_server_sup
}, couch_server_sup
, {DbRootDir
, FabricServer
, HttpConfigFile
, UtilDriverDir
}),
80 % only output when startup was successful
81 io:format("~s~n", [ConsoleStartupCompleteMsg
]);
87 catch exit(whereis(couch_server_sup
), normal
),
91 case file:read_file(?DEFAULT_INI
) of
95 {ok
, <<>>} % default ini missing, just return a blank
98 init({DbServerRoot
, FabricServer
, HttpConfigFile
, UtilDriverDir
}) ->
99 {ok
, {{one_for_one
, 10, 3600},
101 {couch_server
, sup_start_link
, [DbServerRoot
]},
107 {couch_util
, start_link
, [UtilDriverDir
]},
113 {couch_fabric
, start_link
, [FabricServer
]},
119 {httpd
, start_link
, [HttpConfigFile
]},