myapp - myapp_web.erl Revision 386633333436 (Tue Feb 17 2009 at 09:42) - Diff Link to this snippet: https://friendpaste.com/57ODiZ9yXxc8MNQUT9GDFt Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465%% @author author <author@example.com>%% @copyright YYYY author.%% @doc Web server for myapp.-module(myapp_web).-author('author <author@example.com>').-export([start/1, stop/0, loop/2]).%% External APIstart(Options) -> {DocRoot, Options1} = get_option(docroot, Options), Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end, mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]).stop() -> mochiweb_http:stop(?MODULE).loop(Req, DocRoot) -> "/" ++ Path = Req:get(path), case Req:get(method) of Method when Method =:= 'GET'; Method =:= 'HEAD' -> case Path of "captcha" -> {CodeHex, BinPng} = mycaptcha:new(), Cookie = mochiweb_cookies:cookie("cap", CodeHex, []), Req:ok({"image/png", [Cookie], [BinPng]}); _ -> Req:serve_file(Path, DocRoot) end; 'POST' -> case Path of "captcha" -> CodeHex = Req:get_cookie_value("cap"), DataIn = Req:parse_post(), CapCode = proplists:get_value("capCode", DataIn), Out = case mycaptcha:check(CodeHex, CapCode) of true -> %% a cool one Req:ok({"text/html", [], ["<h1>Cool!</h1>"]}); false -> %% redirection Req:respond({302, [{"Location", "/"}, {"Content-Type", "text/html; charset=UTF-8"}],""}) end; _ -> Req:not_found() end; _ -> Req:respond({501, [], []}) end.%% Internal APIget_option(Option, Options) -> {proplists:get_value(Option, Options), proplists:delete(Option, Options)}.