New branched document revision support in the back-end database. Replicator almost...
[couchdbimport.git] / CouchProjects / CouchDb / couch_server_sup.erl
blob404d6879b684b6bfcdccc68f01608e1e838becd9
1 %% CouchDb
2 %% Copyright (C) 2006 Damien Katz
3 %%
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.
8 %%
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.
13 %%
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
26 -export([init/1]).
28 start_link(IniFilename) ->
29 case whereis(couch_server_sup) of
30 undefined ->
31 start_server(IniFilename);
32 _Else ->
33 {error, already_started}
34 end.
36 start_server(IniFilename) ->
37 {ok, IniBin} =
38 case IniFilename of
39 "" ->
40 % no ini file specified, check the command line args
41 case init:get_argument(couch) of
42 {ok, Args} ->
43 case lists:keysearch("ini", 1, Args) of
44 {value, {"ini", Filename}} ->
45 file:read_file(Filename);
46 _Else ->
47 get_default_ini()
48 end;
49 _Else ->
50 get_default_ini()
51 end;
52 IniFilename ->
53 % if IniFilename is specified, it is used
54 file:read_file(IniFilename)
55 end,
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}),
78 case StartResult of
79 {ok,_} ->
80 % only output when startup was successful
81 io:format("~s~n", [ConsoleStartupCompleteMsg]);
82 _ -> ok
83 end,
84 StartResult.
86 stop() ->
87 catch exit(whereis(couch_server_sup), normal),
88 couch_log:stop().
90 get_default_ini() ->
91 case file:read_file(?DEFAULT_INI) of
92 {ok, Ini} ->
93 {ok, Ini};
94 {error, enoent} ->
95 {ok, <<>>} % default ini missing, just return a blank
96 end.
98 init({DbServerRoot, FabricServer, HttpConfigFile, UtilDriverDir}) ->
99 {ok, {{one_for_one, 10, 3600},
100 [{couch_server,
101 {couch_server, sup_start_link, [DbServerRoot]},
102 permanent,
103 brutal_kill,
104 worker,
105 [couch_server]},
106 {couch_util,
107 {couch_util, start_link, [UtilDriverDir]},
108 permanent,
109 brutal_kill,
110 worker,
111 [couch_util]},
112 {couch_fabric,
113 {couch_fabric, start_link, [FabricServer]},
114 permanent,
115 brutal_kill,
116 worker,
117 [couch_fabric]},
118 {httpd,
119 {httpd, start_link, [HttpConfigFile]},
120 permanent,
121 1000,
122 supervisor,
123 [httpd]}
124 ]}}.