summaryrefslogtreecommitdiff
path: root/src/catlfish_web.erl
blob: 9869b2198f2ce3d23af81da8d6f9a96042d4bea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
%%% Copyright (c) 2014, NORDUnet A/S.
%%% See LICENSE for licensing information.

-module(catlfish_web).
-export([start/2, loop/2]).

start(Options, Module) ->
    lager:debug("Starting catlfish web server: ~p", [Module]),
    Loop = fun (Req) ->
                   ?MODULE:loop(Req, Module)
           end,
    mochiweb_http:start([{name, Module}, {loop, Loop} | Options]).

loop(Req, Module) ->
    "/" ++ Path = Req:get(path),
    try
        Starttime = os:timestamp(),
        case Req:get(method) of
            'GET' ->
                Query = Req:parse_qs(),
                lager:debug("GET ~p ~p", [Path, Query]),
                Result = Module:request(get, Path, Query),
                lager:debug("GET finished: ~p us", [timer:now_diff(os:timestamp(), Starttime)]),
                case Result of
                    none ->
                        Req:respond({404, [{"Content-Type", "text/plain"}], "Page not found"});
                    _ ->
                        Req:respond(Result)
                end;
            'POST' ->
                Body = Req:recv_body(),
                lager:debug("POST ~p ~p", [Path, Body]),
                Result = Module:request(post, Path, Body),
                lager:debug("POST finished: ~p us", [timer:now_diff(os:timestamp(), Starttime)]),
                case Result of
                    none ->
                        Req:respond({404, [{"Content-Type", "text/plain"}], "Page not found"});
                    _ ->
                        Req:respond(Result)
                end;
            _ ->
                Req:respond({501, [], []})
        end
    catch
        Type:What ->
            [CrashFunction | Stack] = erlang:get_stacktrace(),
            lager:error("Crash in ~p for path ~p: ~p ~p~n~p~n~p~n", [Module, Path, Type, What, CrashFunction, Stack]),
            Req:respond({500, [{"Content-Type", "text/plain"}],
                         "Internal Server Error\n"})
    end.