PageRenderTime 118ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/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. -include("uce.hrl").
  20. -include_lib("yaws/include/yaws.hrl").
  21. -export([child_spec/0]).
  22. -import(lists, [member/2]).
  23. child_spec() ->
  24. [{DefaultHost, _Config}|_Hosts] = config:get(hosts),
  25. {ok, _SCList, _GC, ChildSpecs} = embedded_start_conf(config:get(DefaultHost, wwwroot),
  26. [{servername, DefaultHost},
  27. {listen, config:get(bind_ip)},
  28. {port, config:get(port)},
  29. {access_log, true},
  30. {partial_post_size, nolimit},
  31. {opaque, DefaultHost},
  32. {appmods, [{"/api/" ++ ?VERSION, uce_appmod}]}],
  33. [{flags, [{auth_log, false},
  34. {copy_errlog, false},
  35. {pick_first_virthost_on_nomatch, false},
  36. {debug, false}
  37. ]},
  38. {logdir, config:get(log_dir)},
  39. {cache_refresh_secs, config:get(cache_refresh)}]),
  40. ChildSpecs.
  41. %
  42. % Backported from yaws 1.89
  43. % We support currently 1.88 and more
  44. % Remove this if we drop the support of yaws 1.88
  45. %
  46. embedded_start_conf(DocRoot, SL, GL)
  47. when is_list(DocRoot), is_list(SL), is_list(GL) ->
  48. embedded_start_conf(DocRoot, SL, GL, "default").
  49. embedded_start_conf(DocRoot, SL, GL, Id)
  50. when is_list(DocRoot), is_list(SL), is_list(GL) ->
  51. case application:load(yaws) of
  52. ok -> ok;
  53. {error, {already_loaded,yaws}} -> ok;
  54. _ -> exit("cannot load yaws")
  55. end,
  56. ok = application:set_env(yaws, embedded, true),
  57. ok = application:set_env(yaws, id, Id),
  58. ChildSpecs = child_specs(),
  59. GC = yaws:create_gconf(GL, Id),
  60. SCList = case SL of
  61. [] ->
  62. [[]];
  63. [Cnf|_] when is_tuple(Cnf) ->
  64. [[yaws:create_sconf(DocRoot, SL)]];
  65. [Cnf|_] when is_list(Cnf) ->
  66. [[yaws:create_sconf(DocRoot, SLItem)] || SLItem <- SL]
  67. end,
  68. %% In case a server is started before any configuration has been set,
  69. %% this makes it possible to get hold of the 'pending' configuration.
  70. %% (see for example the start of the yaws_session_server)
  71. ok = application:set_env(yaws, embedded_conf, [{sclist,SCList},{gc,GC}]),
  72. {ok, SCList, GC, ChildSpecs}.
  73. child_specs() ->
  74. YawsLog = {yaws_log, {yaws_log, start_link, []},
  75. permanent, 5000, worker, [yaws_log]},
  76. YawsServArgs = [_Env = get_app_args()],
  77. YawsServ = {yaws_server, {yaws_server, start_link, YawsServArgs},
  78. permanent, 120000, worker, [yaws_server]},
  79. %% and this guy will restart auxiliary procs that can fail
  80. Sup = {yaws_sup_restarts,
  81. {yaws_sup_restarts, start_link, []},
  82. transient, infinity, supervisor, [yaws_sup_restarts]},
  83. [YawsLog, YawsServ, Sup].
  84. get_app_args() ->
  85. AS=init:get_arguments(),
  86. Debug = case application:get_env(yaws, debug) of
  87. undefined ->
  88. member({yaws, ["debug"]}, AS);
  89. {ok, Val} ->
  90. Val
  91. end,
  92. Trace = case application:get_env(yaws, trace) of
  93. undefined ->
  94. case {member({yaws, ["trace", "http"]}, AS),
  95. member({yaws, ["trace", "traffic"]}, AS)} of
  96. {true, _} ->
  97. {true, http};
  98. {_, true} ->
  99. {true, traffic};
  100. _ ->
  101. false
  102. end;
  103. {ok, http} ->
  104. {true, http};
  105. {ok, traffic} ->
  106. {true, traffic};
  107. _ ->
  108. false
  109. end,
  110. TraceOutput = case application:get_env(yaws, traceoutput) of
  111. undefined ->
  112. member({yaws, ["traceoutput"]}, AS);
  113. {ok, Val3} ->
  114. Val3
  115. end,
  116. Conf = case application:get_env(yaws, conf) of
  117. undefined ->
  118. find_c(AS);
  119. {ok, File} ->
  120. {file, File}
  121. end,
  122. RunMod = case application:get_env(yaws, runmod) of
  123. undefined ->
  124. find_runmod(AS);
  125. {ok,Mod} ->
  126. {ok,Mod}
  127. end,
  128. Embedded = case application:get_env(yaws, embedded) of
  129. undefined ->
  130. false;
  131. {ok, Emb} ->
  132. Emb
  133. end,
  134. Id = case application:get_env(yaws, id) of
  135. undefined ->
  136. "default";
  137. {ok, Id0} when is_atom(Id0) ->
  138. atom_to_list(Id0);
  139. {ok, Id0} ->
  140. Id0
  141. end,
  142. #env{debug = Debug, trace = Trace,
  143. traceoutput = TraceOutput, conf = Conf,
  144. runmod = RunMod, embedded = Embedded, id = Id}.
  145. %%----------------------------------------------------------------------
  146. %%----------------------------------------------------------------------
  147. find_c([{conf, [File]} |_]) ->
  148. {file, File};
  149. find_c([_|T]) ->
  150. find_c(T);
  151. find_c([]) ->
  152. false.
  153. %%----------------------------------------------------------------------
  154. %%----------------------------------------------------------------------
  155. find_runmod([{runmod, [Mod]} |_]) ->
  156. {ok,l2a(Mod)};
  157. find_runmod([_|T]) ->
  158. find_runmod(T);
  159. find_runmod([]) ->
  160. false.
  161. %%----------------------------------------------------------------------
  162. %%----------------------------------------------------------------------
  163. l2a(L) when is_list(L) -> list_to_atom(L);
  164. l2a(A) when is_atom(A) -> A.