PageRenderTime 541ms CodeModel.GetById 81ms app.highlight 263ms RepoModel.GetById 98ms app.codeStats 1ms

/ucengine/src/core/uce_yaws.erl

http://github.com/AF83/ucengine
Erlang | 180 lines | 132 code | 16 blank | 32 comment | 1 complexity | 595689ccca41fe9d2cfe437de354fa17 MD5 | raw file
  1%%
  2%%  U.C.Engine - Unified Collaboration Engine
  3%%  Copyright (C) 2011 af83
  4%%
  5%%  This program is free software: you can redistribute it and/or modify
  6%%  it under the terms of the GNU Affero General Public License as published by
  7%%  the Free Software Foundation, either version 3 of the License, or
  8%%  (at your option) any later version.
  9%%
 10%%  This program is distributed in the hope that it will be useful,
 11%%  but WITHOUT ANY WARRANTY; without even the implied warranty of
 12%%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13%%  GNU Affero General Public License for more details.
 14%%
 15%%  You should have received a copy of the GNU Affero General Public License
 16%%  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 17%%
 18-module(uce_yaws).
 19
 20-include("uce.hrl").
 21-include_lib("yaws/include/yaws.hrl").
 22
 23-export([child_spec/0]).
 24
 25-import(lists, [member/2]).
 26
 27child_spec() ->
 28    [{DefaultHost, _Config}|_Hosts] = config:get(hosts),
 29    {ok, _SCList, _GC, ChildSpecs} = embedded_start_conf(config:get(DefaultHost, wwwroot),
 30                        [{servername, DefaultHost},
 31                         {listen, config:get(bind_ip)},
 32                         {port, config:get(port)},
 33                         {access_log, true},
 34                         {partial_post_size, nolimit},
 35                         {opaque, DefaultHost},
 36                         {appmods, [{"/api/" ++ ?VERSION, uce_appmod}]}],
 37                        [{flags, [{auth_log, false},
 38                                  {copy_errlog, false},
 39                                  {pick_first_virthost_on_nomatch, false},
 40                                  {debug, false}
 41                                 ]},
 42                         {logdir, config:get(log_dir)},
 43                         {cache_refresh_secs, config:get(cache_refresh)}]),
 44    ChildSpecs.
 45
 46%
 47% Backported from yaws 1.89
 48% We support currently 1.88 and more
 49% Remove this if we drop the support of yaws 1.88
 50%
 51embedded_start_conf(DocRoot, SL, GL)
 52  when is_list(DocRoot), is_list(SL), is_list(GL) ->
 53    embedded_start_conf(DocRoot, SL, GL, "default").
 54embedded_start_conf(DocRoot, SL, GL, Id)
 55  when is_list(DocRoot), is_list(SL), is_list(GL) ->
 56    case application:load(yaws) of
 57        ok -> ok;
 58        {error, {already_loaded,yaws}} -> ok;
 59        _ -> exit("cannot load yaws")
 60    end,
 61    ok = application:set_env(yaws, embedded, true),
 62    ok = application:set_env(yaws, id, Id),
 63    ChildSpecs = child_specs(),
 64    GC = yaws:create_gconf(GL, Id),
 65    SCList = case SL of
 66                  [] ->
 67                      [[]];
 68                  [Cnf|_] when is_tuple(Cnf) ->
 69                      [[yaws:create_sconf(DocRoot, SL)]];
 70                  [Cnf|_] when is_list(Cnf) ->
 71                      [[yaws:create_sconf(DocRoot, SLItem)] || SLItem <- SL]
 72              end,
 73    %% In case a server is started before any configuration has been set,
 74    %% this makes it possible to get hold of the 'pending' configuration.
 75    %% (see for example the start of the yaws_session_server)
 76    ok = application:set_env(yaws, embedded_conf, [{sclist,SCList},{gc,GC}]),
 77    {ok, SCList, GC, ChildSpecs}.
 78
 79child_specs() ->
 80    YawsLog = {yaws_log, {yaws_log, start_link, []},
 81               permanent, 5000, worker, [yaws_log]},
 82
 83    YawsServArgs = [_Env = get_app_args()],
 84    YawsServ = {yaws_server, {yaws_server, start_link, YawsServArgs},
 85                permanent, 120000, worker, [yaws_server]},
 86
 87    %% and this guy will restart auxiliary procs that can fail
 88    Sup = {yaws_sup_restarts,
 89           {yaws_sup_restarts, start_link, []},
 90           transient, infinity, supervisor, [yaws_sup_restarts]},
 91
 92    [YawsLog, YawsServ, Sup].
 93
 94get_app_args() ->
 95    AS=init:get_arguments(),
 96    Debug = case application:get_env(yaws, debug) of
 97                undefined ->
 98                    member({yaws, ["debug"]}, AS);
 99                {ok, Val} ->
100                    Val
101            end,
102    Trace = case application:get_env(yaws, trace) of
103                undefined ->
104                    case {member({yaws, ["trace", "http"]}, AS),
105                          member({yaws, ["trace", "traffic"]}, AS)} of
106                        {true, _} ->
107                            {true, http};
108                        {_, true} ->
109                            {true, traffic};
110                        _ ->
111                            false
112                    end;
113                {ok, http} ->
114                    {true, http};
115                {ok, traffic} ->
116                    {true, traffic};
117                _ ->
118                    false
119            end,
120    TraceOutput = case application:get_env(yaws, traceoutput) of
121                      undefined ->
122                          member({yaws, ["traceoutput"]}, AS);
123                      {ok, Val3} ->
124                          Val3
125                  end,
126    Conf = case application:get_env(yaws, conf) of
127               undefined ->
128                   find_c(AS);
129               {ok, File} ->
130                   {file, File}
131           end,
132    RunMod = case application:get_env(yaws, runmod) of
133                 undefined ->
134                     find_runmod(AS);
135                 {ok,Mod} ->
136                     {ok,Mod}
137             end,
138    Embedded = case application:get_env(yaws, embedded) of
139                   undefined ->
140                       false;
141                   {ok, Emb} ->
142                       Emb
143               end,
144    Id = case application:get_env(yaws, id) of
145             undefined ->
146                 "default";
147             {ok, Id0} when is_atom(Id0) ->
148                 atom_to_list(Id0);
149             {ok, Id0} ->
150                 Id0
151         end,
152
153    #env{debug = Debug, trace = Trace,
154         traceoutput = TraceOutput, conf = Conf,
155         runmod = RunMod, embedded = Embedded, id = Id}.
156
157
158%%----------------------------------------------------------------------
159%%----------------------------------------------------------------------
160find_c([{conf, [File]} |_]) ->
161    {file, File};
162find_c([_|T]) ->
163    find_c(T);
164find_c([]) ->
165    false.
166
167
168%%----------------------------------------------------------------------
169%%----------------------------------------------------------------------
170find_runmod([{runmod, [Mod]} |_]) ->
171    {ok,l2a(Mod)};
172find_runmod([_|T]) ->
173    find_runmod(T);
174find_runmod([]) ->
175    false.
176
177%%----------------------------------------------------------------------
178%%----------------------------------------------------------------------
179l2a(L) when is_list(L) -> list_to_atom(L);
180l2a(A) when is_atom(A) -> A.