PageRenderTime 7ms CodeModel.GetById 0ms RepoModel.GetById 0ms 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
Possible License(s): MIT
  1. %% @author author <author@example.com>
  2. %% @copyright YYYY author.
  3. %% @doc Web server for skel.
  4. -module(skel_web).
  5. -author('author <author@example.com>').
  6. -export([start/1, stop/0, loop/2]).
  7. %% External API
  8. start(Options) ->
  9. {DocRoot, Options1} = get_option(docroot, Options),
  10. Loop = fun (Req) ->
  11. ?MODULE:loop(Req, DocRoot)
  12. end,
  13. mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).
  14. stop() ->
  15. mochiweb_http:stop(?MODULE).
  16. loop(Req, DocRoot) ->
  17. "/" ++ Path = Req:get(path),
  18. case Req:get(method) of
  19. Method when Method =:= 'GET'; Method =:= 'HEAD' ->
  20. case Path of
  21. _ ->
  22. Req:serve_file(Path, DocRoot)
  23. end;
  24. 'POST' ->
  25. case Path of
  26. _ ->
  27. Req:not_found()
  28. end;
  29. _ ->
  30. Req:respond({501, [], []})
  31. end.
  32. %% Internal API
  33. get_option(Option, Options) ->
  34. {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.
  35. %%
  36. %% Tests
  37. %%
  38. -include_lib("eunit/include/eunit.hrl").
  39. -ifdef(TEST).
  40. -endif.