Skip to content

Commit

Permalink
Removed angle brackets from queue list, added version numnber to inde…
Browse files Browse the repository at this point in the history
…x page, moved to 0.2.4, support for multiple queue names
  • Loading branch information
IanCal committed Jun 11, 2012
1 parent 8b20d1e commit 4387b9c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 25 deletions.
2 changes: 1 addition & 1 deletion sinan.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{project_name, semq}.
{project_vsn, "0.2.3"}.
{project_vsn, "0.2.4"}.

{build_dir, "_build"}.

Expand Down
2 changes: 1 addition & 1 deletion src/semq.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
%% application.
{application, semq,
[{description, "A simple message queue implementation accessible via POST and GET with an extremely small API"},
{vsn, "0.2.3"},
{vsn, "0.2.4"},
{modules, [semq_app,
semq_sup]},
{registered,[semq_sup]},
Expand Down
3 changes: 1 addition & 2 deletions src/semq_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
%%% @copyright 2012 Ian Calvert
%%%----------------------------------------------------------------
-module(semq_app).

-behaviour(application).

%% Application callbacks
Expand Down Expand Up @@ -33,7 +32,7 @@ start(_StartType, _StartArgs) ->
]}
],

cowboy:start_listener(web_frontend, 10000,
cowboy:start_listener(web_frontend, 100,
cowboy_tcp_transport, [{port, get_port()}],
cowboy_http_protocol, [{dispatch, Dispatch}]
),
Expand Down
6 changes: 5 additions & 1 deletion src/semq_indexpage.erl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ format_queues({ok, []}) ->

format_queues({ok, Queues}) ->
[<<"The following queues are currently running:<br>">>, lists:map(fun semq_indexpage:format_queue/1, Queues)].

get_version() ->
{ok, Vsn} = application:get_key(semq, vsn),
list_to_binary(Vsn).


handle(Req, State) ->
Expand All @@ -41,7 +45,7 @@ handle(Req, State) ->
<head>
</head>
<body>
Hooray! SEMQ is running correctly!<br>">>,
Hooray! SEMQ version ">>, get_version() ,<<" is running correctly!<br>">>,
format_queues(frontend:get_all_queue_names()),
<<"
</body>
Expand Down
10 changes: 9 additions & 1 deletion src/semq_queuelist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@
init({_Any, http}, Req, []) ->
{ok, Req, undefined}.

intersperse(_, []) -> [];
intersperse(_, [X]) -> [X];
intersperse(Sep, [X|Xs]) -> [X|[Sep|intersperse(Sep, Xs)]].

format_queues([]) ->
<<"[]">>;

format_queues(Queues) ->
[<<"[\"">>, intersperse(<<"\",\"">>, Queues), <<"\"]">>].

handle(Req, State) ->
{ok, Queues} = frontend:get_all_queue_names(),
{ok, Req2} = cowboy_http_req:reply(200, [], io_lib:format(<<"~p~n">>, [Queues]), Req),
{ok, Req2} = cowboy_http_req:reply(200, [], format_queues(Queues), Req),
{ok, Req2, State}.

terminate(_Req, _State) ->
Expand Down
43 changes: 24 additions & 19 deletions src/semq_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ init({_Any, http}, Req, []) ->
{ok, Req, undefined}.


extract_queue_names(PathFragments) ->
extract_queue_names(PathFragments, []).

extract_queue_name([<<"queue">> | [Queue | _]]) ->
{ok, Queue};
extract_queue_names([<<"queue">> | [Queue | Remainder]], Queues) ->
extract_queue_names(Remainder, [Queue | Queues]);

extract_queue_name([_Key | [_Value | Remainder]]) ->
extract_queue_name(Remainder);
extract_queue_names([_Key | [_Value | Remainder]], Queues) ->
extract_queue_names(Remainder, Queues);

extract_queue_name(_) ->
{error, "No queue name in URL"}.
extract_queue_names(_, []) ->
{error, "No queue name in URL"};

extract_queue_names(_, Queues) ->
{ok, Queues}.

queue_get_head(Queue, Req) ->
case frontend:getrequest(Queue) of
Expand All @@ -52,19 +57,19 @@ queue_get_head(Queue, Req) ->
end.


queue_delete(Queue) ->
frontend:deleterequest(Queue).
queue_delete(Queues) ->
lists:foreach(fun frontend:deleterequest/1, Queues).

queue_post(Queue, Req) ->
queue_post(Queues, Req) ->
{ok, Body, _Req} = cowboy_http_req:body(Req),
{Type, Req2} = cowboy_http_req:header('Content-Type', Req),
post_message(Queue, Body, Type, Req2).
post_message(Queues, Body, Type, Req2).

post_message(Queue, Body, undefined, Req) ->
post_message(Queue, Body, <<"text/plain">>, Req);
post_message(Queues, Body, undefined, Req) ->
post_message(Queues, Body, <<"text/plain">>, Req);

post_message(Queue, Body, Type, Req) ->
frontend:postrequest(Queue, {Type, Body}),
post_message(Queues, Body, Type, Req) ->
lists:foreach(fun (Queue) -> frontend:postrequest(Queue, {Type, Body}) end, Queues),
{ok, {<<"text/plain">>, <<"Posted">>}, Req}.

jsonp_wrapper(Status, undefined, MimeType, Body) ->
Expand All @@ -85,7 +90,7 @@ headerswithtype(Mimetype) ->

handle_method('GET', Req) ->
{Path, Req} = cowboy_http_req:path(Req),
{ok, Queue} = extract_queue_name(Path),
{ok, [Queue]} = extract_queue_names(Path),
case queue_get_head(Queue, Req) of
{ok, {Type, Message}, Req2} ->
reply(200, Type, Message, Req2);
Expand All @@ -95,17 +100,17 @@ handle_method('GET', Req) ->

handle_method('POST', Req) ->
{Path, Req} = cowboy_http_req:path(Req),
{ok, Queue} = extract_queue_name(Path),
{ok, {Type, Message}, Req2} = queue_post(Queue, Req),
{ok, Queues} = extract_queue_names(Path),
{ok, {Type, Message}, Req2} = queue_post(Queues, Req),
cowboy_http_req:reply(200, headerswithtype(Type), Message, Req2);

handle_method('HEAD', Req) ->
cowboy_http_req:reply(200, headerswithtype(<<"text/plain">>), <<"">>, Req);

handle_method('DELETE', Req) ->
{Path, Req} = cowboy_http_req:path(Req),
{ok, Queue} = extract_queue_name(Path),
queue_delete(Queue),
{ok, Queues} = extract_queue_names(Path),
queue_delete(Queues),
cowboy_http_req:reply(200, headerswithtype(<<"text/plain">>), <<"deleted">>, Req).

handle(Req, State) ->
Expand Down

0 comments on commit 4387b9c

Please sign in to comment.