]> review.fuel-infra Code Review - packages/trusty/rabbitmq-server.git/blob
8976265311b4ba09393800ac242236c13c98eea8
[packages/trusty/rabbitmq-server.git] /
1 %% @author {{author}}
2 %% @copyright {{year}} {{author}}
3
4 %% @doc Web server for {{appid}}.
5
6 -module({{appid}}_web).
7 -author("{{author}}").
8
9 -export([start/1, stop/0, loop/2]).
10
11 %% External API
12
13 start(Options) ->
14     {DocRoot, Options1} = get_option(docroot, Options),
15     Loop = fun (Req) ->
16                    ?MODULE:loop(Req, DocRoot)
17            end,
18     mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
19
20 stop() ->
21     mochiweb_http:stop(?MODULE).
22
23 loop(Req, DocRoot) ->
24     "/" ++ Path = Req:get(path),
25     try
26         case Req:get(method) of
27             Method when Method =:= 'GET'; Method =:= 'HEAD' ->
28                 case Path of
29                     _ ->
30                         Req:serve_file(Path, DocRoot)
31                 end;
32             'POST' ->
33                 case Path of
34                     _ ->
35                         Req:not_found()
36                 end;
37             _ ->
38                 Req:respond({501, [], []})
39         end
40     catch
41         Type:What ->
42             Report = ["web request failed",
43                       {path, Path},
44                       {type, Type}, {what, What},
45                       {trace, erlang:get_stacktrace()}],
46             error_logger:error_report(Report),
47             %% NOTE: mustache templates need \\ because they are not awesome.
48             Req:respond({500, [{"Content-Type", "text/plain"}],
49                          "request failed, sorry\\n"})
50     end.
51
52 %% Internal API
53
54 get_option(Option, Options) ->
55     {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
56
57 %%
58 %% Tests
59 %%
60 -ifdef(TEST).
61 -include_lib("eunit/include/eunit.hrl").
62
63 you_should_write_a_test() ->
64     ?assertEqual(
65        "No, but I will!",
66        "Have you written any tests?"),
67     ok.
68
69 -endif.