Skip to content

Instantly share code, notes, and snippets.

@arobson
Created June 17, 2011 19:07
Embed
What would you like to do?
Snippet of the 'Dynamic' HTTP server that accomodates RESTful requests and static content using Misultin.
handle_request(Req) ->
#req{uri={_,Uri}} = Req:raw(),
Ext=filename:extension(Uri),
handle_request(Req, Uri, Ext).
handle_request(Req, _Uri, []) ->
Segments = Req:resource([urldecode]),
Method = get_atom(Req:get(method)),
[Resource|Arguments]=Segments,
ResourceModule = get_atom(Resource),
ResourceModule:Method(Arguments,Req); %%this is dynamically invoking another module/function
handle_request(Req, Uri, _Ext) ->
handle_static_request(Req, Uri).
handle_static_request(Req, "/favicon.ico") ->
Req:respond(204, "stop. asking.");
handle_static_request(Req, Uri) ->
File = list:append("/git/empire/web/content", Uri),
Req:file(File).
get_atom(X) ->
case (is_atom(X)) of
true ->
list_to_atom(string:to_lower(atom_to_list(X)));
false ->
list_to_atom(string:to_lower(X))
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment