Revision: 3507 Author: hans URL: http://bknr.net/trac/changeset/3507
Create new frontend project: Pontificia Cohors Helvetica
A trunk/projects/pch/ A trunk/projects/pch/src/ A trunk/projects/pch/src/pch.erl
Added: trunk/projects/pch/src/pch.erl =================================================================== --- trunk/projects/pch/src/pch.erl (rev 0) +++ trunk/projects/pch/src/pch.erl 2008-07-18 16:08:27 UTC (rev 3507) @@ -0,0 +1,45 @@ +%%% from +%%% http://www.rsaccon.com/2007/09/mochiweb-erlang-based-webserver-toolkit.html + +-module(pch). +-export([start/0, loop/2, stop/0]). +-define(DEFAULTS, [{name, ?MODULE}, + {port, 9952}]). + +start() -> + DocRoot = filename:dirname(filename:dirname(code:which(?MODULE))), + code:add_patha(filename:join([DocRoot, "mochiweb-c4", "ebin"])), + Loop = fun (Req) -> ?MODULE:loop(Req, DocRoot) end, + mochiweb_http:start([{loop, Loop} | ?DEFAULTS]). + +stop() -> + mochiweb_http:stop(?MODULE). + +loop(Req, DocRoot) -> + "/" ++ Path = Req:get(path), + case Req:get(method) of + M when M =:= 'GET'; M =:= 'HEAD' -> + case Path of + "timer" -> + Response = Req:ok({"text/plain", chunked}), + timer(Response); + "static" -> + Req:ok({"text/plain", "static response"}); + "nodes" -> + Req:ok({"text/plain", + io_lib:format("~p~n", [nodes()])}); + "dump" -> + Req:ok({"text/plain", + io_lib:format("~p~n", [Req:dump()])}); + _ -> + Req:serve_file(Path, DocRoot) + end; + _ -> + Req:respond({501, [], ""}) + end. + +timer(Req) -> + Req:write_chunk(io_lib:format("The time is: ~p~n", + [calendar:local_time()])), + timer:sleep(1000), + timer(Req).