[bknr-cvs] hans changed trunk/projects/pch/src/pch
BKNR Commits
bknr at bknr.net
Tue Jul 22 05:40:56 UTC 2008
Revision: 3541
Author: hans
URL: http://bknr.net/trac/changeset/3541
snapshot intermediate state
U trunk/projects/pch/src/pch.erl
A trunk/projects/pch/src/pch_cache_mgr.erl
A trunk/projects/pch/src/pch_front.erl
Modified: trunk/projects/pch/src/pch.erl
===================================================================
--- trunk/projects/pch/src/pch.erl 2008-07-21 15:40:49 UTC (rev 3540)
+++ trunk/projects/pch/src/pch.erl 2008-07-22 05:40:55 UTC (rev 3541)
@@ -3,39 +3,15 @@
-module(pch).
-export([start/0, loop/2, stop/0]).
--define(DEFAULTS, [{name, ?MODULE},
- {port, 9888}]).
+-define(HTTP_PORT, 9888).
+-define(MANAGEMENT_PORT, 9889).
start() ->
Cache = ets:new(table, [set, public]),
- Loop = fun (Req) -> ?MODULE:loop(Req, Cache) end,
- mochiweb_http:start([{loop, Loop} | ?DEFAULTS]).
+ pch_cache_mgr:start(?MANAGEMENT_PORT, Cache),
+ pch_front:start(?HTTP_PORT, Cache).
stop() ->
- mochiweb_http:stop(?MODULE).
+ pch_front:stop(),
+ pch_cache_mgr:stop().
-fetch_from_backend(Path) ->
- {ok, {{_Version, 200, _ReasonPhrase}, Headers, Body}} =
- http:request("http://test.createrainforest.org" ++ Path),
- {ok, Headers, Body}.
-
-backend_request(Req, Cache) ->
- Path = Req:get(path),
- case ets:lookup(Cache, Path) of
- [{Path, Headers, Body}] ->
- io:format("cache HIT for ~p~n", [Path]),
- Req:respond({200, Headers, Body});
- _ ->
- io:format("cache MISS for ~p~n", [Path]),
- {ok, Headers, Body} = fetch_from_backend(Path),
- ets:insert(Cache, {Path, Headers, Body}),
- Req:respond({200, Headers, Body})
- end.
-
-loop(Req, Cache) ->
- case Req:get(method) of
- M when M =:= 'GET' ->
- backend_request(Req, Cache);
- _ ->
- Req:respond({501, [], ""})
- end.
Added: trunk/projects/pch/src/pch_cache_mgr.erl
===================================================================
--- trunk/projects/pch/src/pch_cache_mgr.erl (rev 0)
+++ trunk/projects/pch/src/pch_cache_mgr.erl 2008-07-22 05:40:55 UTC (rev 3541)
@@ -0,0 +1,19 @@
+-module(pch_cache_mgr).
+-export([start/1, loop/2, stop/0]).
+
+start(Port, Cache) ->
+ mochiweb_http:start([{loop, fun (Req) ->
+ ?MODULE:loop(Req, Cache) end},
+ {port, Port},
+ {name, ?MODULE}]).
+
+stop() ->
+ mochiweb_http:stop(?MODULE).
+
+loop(Req, Cache) ->
+ case Req:get(method) of
+ M when M =:= 'GET' ->
+ backend_request(Req, Cache);
+ _ ->
+ Req:respond({501, [], ""})
+ end.
Added: trunk/projects/pch/src/pch_front.erl
===================================================================
--- trunk/projects/pch/src/pch_front.erl (rev 0)
+++ trunk/projects/pch/src/pch_front.erl 2008-07-22 05:40:55 UTC (rev 3541)
@@ -0,0 +1,37 @@
+-module(pch_front).
+-export([start/0, loop/2, stop/0]).
+
+start(Port, Cache) ->
+ mochiweb_http:start([{loop, fun (Req) ->
+ ?MODULE:loop(Req, Cache) end},
+ {port, Port},
+ {name, ?MODULE}]).
+
+stop() ->
+ mochiweb_http:stop(?MODULE),
+
+fetch_from_backend(Path) ->
+ {ok, {{_Version, 200, _ReasonPhrase}, Headers, Body}} =
+ http:request("http://test.createrainforest.org" ++ Path),
+ {ok, Headers, Body}.
+
+backend_request(Req, Cache) ->
+ Path = Req:get(path),
+ case ets:lookup(Cache, Path) of
+ [{Path, Headers, Body}] ->
+ io:format("cache HIT for ~p~n", [Path]),
+ Req:respond({200, Headers, Body});
+ _ ->
+ io:format("cache MISS for ~p~n", [Path]),
+ {ok, Headers, Body} = fetch_from_backend(Path),
+ ets:insert(Cache, {Path, Headers, Body}),
+ Req:respond({200, Headers, Body})
+ end.
+
+loop(Req, Cache) ->
+ case Req:get(method) of
+ M when M =:= 'GET' ->
+ backend_request(Req, Cache);
+ _ ->
+ Req:respond({501, [], ""})
+ end.
More information about the Bknr-cvs
mailing list