_temp_view support for alternate query languages, using content-type
[couchdbimport.git] / src / CouchDb / couch_server_sup.erl
blobdd3b738c179a42f66b6e64b1ea178811845f6644
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(couchini) of
42 {ok, [Filename]} ->
43 file:read_file(Filename);
44 _Else ->
45 get_default_ini()
46 end;
47 IniFilename ->
48 % if IniFilename is specified, it is used
49 file:read_file(IniFilename)
50 end,
51 {ok, Ini} = couch_util:parse_ini(binary_to_list(IniBin)),
52 ConsoleStartupMsg = couch_util:ini_get_value(Ini, "Couch", "ConsoleStartupMsg", "CouchDb server starting"),
53 ConsoleStartupCompleteMsg = couch_util:ini_get_value(Ini, "Couch", "ConsoleStartupCompleteMsg", "CouchDb server started"),
54 LogLevel = list_to_atom(couch_util:ini_get_value(Ini, "Couch", "LogLevel", "error")),
55 DbRootDir = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "DbRootDir", ".")),
56 HttpConfigFile = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "HttpConfigFile", "couch_httpd.conf")),
57 LogFile = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "LogFile", "couchdb.log")),
58 UtilDriverDir = couch_util:abs_pathname(couch_util:ini_get_value(Ini, "Couch", "UtilDriverDir", ".")),
59 UpdateNotifierExes = [couch_util:abs_pathname(UpdateNotifierExe) || {_,UpdateNotifierExe} <- couch_util:ini_get_values_starting(Ini, "Couch", "DbUpdateNotificationProcess")],
61 UpdateNotifierExes = [couch_util:abs_pathname(QueryExe) || {{"Couch", "DbUpdateNotificationProcess"}, QueryExe} <- Ini],
63 QueryServers = [{Lang, couch_util:abs_pathname(QueryExe)} || {{"Couch Query Servers", Lang}, QueryExe} <- Ini],
65 FtSearchQueryServer0 = couch_util:ini_get_value(Ini, "Couch", "FullTextSearchQueryServer", ""),
67 FtSearchQueryServer =
68 case FtSearchQueryServer0 of
69 "" -> "";
70 _ -> couch_util:abs_pathname(FtSearchQueryServer0)
71 end,
74 io:format("couch ~s (LogLevel=~s)~n", [couch_server:get_version(), LogLevel]),
76 io:format("~s~n", [ConsoleStartupMsg]),
78 StartResult = supervisor:start_link({local, couch_server_sup}, couch_server_sup, {LogFile, LogLevel, DbRootDir, QueryServers, HttpConfigFile, UtilDriverDir, UpdateNotifierExes, FtSearchQueryServer}),
80 couch_log:debug("Startup Info:~n\tDbRootDir=~p~n" ++
81 "\tHttpConfigFile=~p~n" ++
82 "\tLogFile=~p~n" ++
83 "\tUtilDriverDir=~p~n" ++
84 "\tDbUpdateNotificationProcesses=~p~n" ++
85 "\tFullTextSearchQueryServer=~p~n" ++
86 "~s~n",
87 [DbRootDir,
88 HttpConfigFile,
89 LogFile,
90 UtilDriverDir,
91 UpdateNotifierExes,
92 FtSearchQueryServer,
93 [lists:flatten(io_lib:format("\t~s=~p~n", [Lang, QueryExe])) || {Lang, QueryExe} <- QueryServers]]),
95 case StartResult of
96 {ok,_} ->
97 % only output when startup was successful
98 io:format("~s~n", [ConsoleStartupCompleteMsg]);
99 _ -> ok
100 end,
101 StartResult.
103 stop() ->
104 catch exit(whereis(couch_server_sup), normal),
105 couch_log:stop().
107 get_default_ini() ->
108 case file:read_file(?DEFAULT_INI) of
109 {ok, Ini} ->
110 {ok, Ini};
111 {error, enoent} ->
112 {ok, <<>>} % default ini missing, just return a blank
113 end.
115 init({LogFile, LogLevel, DbServerRoot, QueryServers, HttpConfigFile, UtilDriverDir, UpdateNotifierExes, FtSearchQueryServer}) ->
116 {ok, {{one_for_one, 10, 3600},
117 [{couch_log,
118 {couch_log, start_link, [LogFile, LogLevel]},
119 permanent,
120 brutal_kill,
121 worker,
122 [couch_server]},
123 {couch_server,
124 {couch_server, sup_start_link, [DbServerRoot]},
125 permanent,
126 brutal_kill,
127 worker,
128 [couch_server]},
129 {couch_util,
130 {couch_util, start_link, [UtilDriverDir]},
131 permanent,
132 brutal_kill,
133 worker,
134 [couch_util]},
135 {couch_query_servers,
136 {couch_query_servers, start_link, [QueryServers]},
137 permanent,
138 brutal_kill,
139 worker,
140 [couch_query_servers]},
141 {httpd,
142 {httpd, start_link, [HttpConfigFile]},
143 permanent,
144 1000,
145 supervisor,
146 [httpd]},
147 {couch_db_update_event,
148 {gen_event, start_link, [{local, couch_db_update}]},
149 permanent,
150 1000,
151 supervisor,
152 dynamic}
153 ] ++
154 lists:map(fun(UpdateNotifierExe) ->
155 {UpdateNotifierExe,
156 {couch_db_update_notifier, start_link, [UpdateNotifierExe]},
157 permanent,
158 1000,
159 supervisor,
160 [couch_db_update_notifier]}
161 end, UpdateNotifierExes)
163 case FtSearchQueryServer of
164 "" ->
166 _ ->
167 [{couch_ft_query,
168 {couch_ft_query, start_link, [FtSearchQueryServer]},
169 permanent,
170 1000,
171 supervisor,
172 [httpd]}]