PageRenderTime 203ms CodeModel.GetById 81ms app.highlight 4ms RepoModel.GetById 116ms app.codeStats 0ms

/priv/skel/src/skel_web.erl

http://github.com/basho/mochiweb
Erlang | 51 lines | 32 code | 11 blank | 8 comment | 0 complexity | 6b71f100e36df8f076e549170f3f2565 MD5 | raw file
 1%% @author author <author@example.com>
 2%% @copyright YYYY author.
 3
 4%% @doc Web server for skel.
 5
 6-module(skel_web).
 7-author('author <author@example.com>').
 8
 9-export([start/1, stop/0, loop/2]).
10
11%% External API
12
13start(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
20stop() ->
21    mochiweb_http:stop(?MODULE).
22
23loop(Req, DocRoot) ->
24    "/" ++ Path = Req:get(path),
25    case Req:get(method) of
26        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
27            case Path of
28                _ ->
29                    Req:serve_file(Path, DocRoot)
30            end;
31        'POST' ->
32            case Path of
33                _ ->
34                    Req:not_found()
35            end;
36        _ ->
37            Req:respond({501, [], []})
38    end.
39
40%% Internal API
41
42get_option(Option, Options) ->
43    {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
44
45
46%%
47%% Tests
48%%
49-include_lib("eunit/include/eunit.hrl").
50-ifdef(TEST).
51-endif.