summaryrefslogtreecommitdiff
path: root/src/catlfish_web.erl
diff options
context:
space:
mode:
authorMagnus Ahltorp <map@kth.se>2014-10-24 04:37:18 +0200
committerLinus Nordberg <linus@nordberg.se>2014-10-24 11:10:05 +0200
commit00156d1b7740c4177fc29bd10d75ea4e0e59cc6e (patch)
tree8dc3ac870fead70b59b40240d7b12734e7d0d67b /src/catlfish_web.erl
parent5e0ad5a06c608250ca1130e378932c0275c99e63 (diff)
Use mochiweb for v1 API
Conflicts: catlfish.config src/v1.erl
Diffstat (limited to 'src/catlfish_web.erl')
-rw-r--r--src/catlfish_web.erl48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/catlfish_web.erl b/src/catlfish_web.erl
new file mode 100644
index 0000000..cdc1a39
--- /dev/null
+++ b/src/catlfish_web.erl
@@ -0,0 +1,48 @@
+%%% Copyright (c) 2014, NORDUnet A/S.
+%%% See LICENSE for licensing information.
+
+-module(catlfish_web).
+-export([start/2, stop/0, loop/2]).
+
+start(Options, Module) ->
+ Loop = fun (Req) ->
+ ?MODULE:loop(Req, Module)
+ end,
+ mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options]).
+
+stop() ->
+ mochiweb_http:stop(?MODULE).
+
+loop(Req, Module) ->
+ "/" ++ Path = Req:get(path),
+ try
+ case Req:get(method) of
+ 'GET' ->
+ Query = Req:parse_qs(),
+ lager:debug("GET ~p ~p", [Path, Query]),
+ Result = Module:request(get, Path, Query),
+ 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),
+ 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 ->
+ lager:error("Crash in ~p for path ~p: ~p ~n~p~n~p~n", [Module, Path, Type, What, erlang:get_stacktrace()]),
+ Req:respond({500, [{"Content-Type", "text/plain"}],
+ "Internal Server Error\n"})
+ end.