|
@@ -1,53 +0,0 @@
|
|
|
--module(web_handler).
|
|
|
--export([dispatch/1]).
|
|
|
-
|
|
|
-dispatch(Req) ->
|
|
|
- Method = Req:get(method),
|
|
|
- {_UriType, Uri} = Req:get(uri),
|
|
|
- Path = string:tokens(Uri, "/"),
|
|
|
- handle(Method, Path, Req).
|
|
|
-
|
|
|
-%% handle
|
|
|
-
|
|
|
-handle('GET', ["json"], Req) ->
|
|
|
- json(Req, erl_bench:hello_json());
|
|
|
-
|
|
|
-handle('GET', ["plaintext"], Req) ->
|
|
|
- plain(Req, erl_bench:hello_plain());
|
|
|
-
|
|
|
-handle('GET', ["db"], Req) ->
|
|
|
- json(Req, erl_bench:random_json());
|
|
|
-
|
|
|
-handle('GET', ["queries"], Req) ->
|
|
|
- Queries = queries(Req),
|
|
|
- json(Req, erl_bench:randoms_json(Queries));
|
|
|
-
|
|
|
-handle('GET', ["updates"], Req) ->
|
|
|
- Queries = queries(Req),
|
|
|
- json(Req, erl_bench:update_randoms_json(Queries));
|
|
|
-
|
|
|
-handle('GET', ["fortunes"], Req) ->
|
|
|
- html(Req, erl_bench:fortunes_html());
|
|
|
-
|
|
|
-handle(_Method, _Path, Req) ->
|
|
|
- Req:respond(404, [{"Content-Type", "text/plain"}], "Not Found").
|
|
|
-
|
|
|
-%% private
|
|
|
-
|
|
|
-json(Req, Json) ->
|
|
|
- Req:ok([{"Content-Type", "application/json"}], Json).
|
|
|
-
|
|
|
-plain(Req, Text) ->
|
|
|
- Req:ok([{"Content-Type", "text/plain"}], Text).
|
|
|
-
|
|
|
-html(Req, Html) ->
|
|
|
- Req:ok([{"Content-Type", "text/html"}], Html).
|
|
|
-
|
|
|
-queries(Req) ->
|
|
|
- Params = Req:parse_qs(),
|
|
|
- Queries = (catch list_to_integer(proplists:get_value("queries", Params, "1"))),
|
|
|
- case {is_number(Queries), Queries > 500} of
|
|
|
- {true, true} -> 500;
|
|
|
- {false, _} -> 1;
|
|
|
- _ -> Queries
|
|
|
- end.
|