Snippet of the 'Dynamic' HTTP server that accomodates RESTful requests and static content using Misultin.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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