Permalink
Browse files

refactoring and clean-up

  • Loading branch information...
davebryson committed Feb 14, 2009
1 parent eba7485 commit e1be49917faaf77b2f2462f42e8d4e9c64813647
Showing with 44 additions and 9 deletions.
  1. +3 −1 priv/skel/src/home_controller.erl
  2. +3 −1 priv/skel/src/skel_sup.erl
  3. +38 −7 priv/skel/src/skel_web.erl
@@ -1,5 +1,5 @@
%%
-%% Sample default controller
+%% Sample default "/" controller, implement this to your needs
%%
-module(home_controller,[Env]).
@@ -15,6 +15,8 @@ handle_request("show",[Year]) ->
before_filter() ->
+ %% Shows how to potentially filter on certain actions in this
+ %% controller
FilterOnly = ["show"],
case lists:member(beepbeep_args:get_action(Env),FilterOnly) of
true ->
@@ -41,8 +41,10 @@ init([]) ->
{port, 8000}
],
+ %% Sets up the BeepBeep environment. Removing any of the below
+ %% will cause something to break.
BaseDir = skel_deps:get_base_dir(),
-
+
Web = {skel_web,
{skel_web, start, [WebConfig]},
permanent, 5000, worker, dynamic},
View
@@ -1,3 +1,8 @@
+%% @author Dave Bryson [http://weblog.miceda.org]
+%% @copyright Dave Bryson 2008-2009
+%%
+%% Creates a MochiWeb Server with the BeepBeep hook
+%%
-module(skel_web).
-author('Dave Bryson <http://weblog.miceda.org>').
@@ -17,14 +22,32 @@ loop(Req) ->
%% Setup env...
InitialEnv = mochiweb_env:setup_environment(Req),
Env = setup_session(Req,InitialEnv),
- %%error_logger:info_report(Env),
+
+ %% Possible return values
+ %% {render,View,Data}
+ %% {render,View,Data,Options}
+ %% {text,Data}
+ %% {json,Data}
+ %% {redirect,Url}
+ %% {static,File}
+ %% {error,_}
case beepbeep:dispatch(Env) of
- {ok,Status,ContentType,H,Content} ->
- Cookie = get_cookie(Env),
- Headers = [Cookie|H],
- Req:respond({Status,[{"Content-Type",ContentType}|Headers],Content});
- %%Req:ok({"text/html",Headers,Content});
+ {render,View,Data} ->
+ {ok,Content} = render_template(View,Data),
+ Req:respond({200,
+ [{"Content-Type","text/html"}|[get_cookie(Env)]],
+ Content});
+ {render,View,Data,Options} ->
+ {Status,ContentType,Headers} = extract_options(Options),
+ {ok,Content} = render_template(View,Data),
+ Req:respond({Status,
+ [{"Content-Type",ContentType}|[get_cookie(Env)|Headers]],
+ Content});
+ {text,Content} ->
+ Req:respond({200,
+ [{"Content-Type","text/plain"}|[get_cookie(Env)]],
+ Content});
{redirect,Url} ->
Req:respond({302,
[{"Location", Url},
@@ -36,7 +59,15 @@ loop(Req) ->
{error,_} ->
Req:respond({500,[],"Server Error"})
end.
-
+
+render_template(ViewFile,Data) ->
+ FullPathToFile = filename:join([skel_deps:local_path(["views"]),ViewFile]),
+ beepbeep:render_template(FullPathToFile,ViewFile,Data).
+
+extract_options(Options) ->
+ {proplists:get_value(status,Options,200),
+ proplists:get_value(content_type,Options,"text/html"),
+ proplists:get_value(headers,Options,[])}.
get_cookie(Env) ->
mochiweb_cookies:cookie(?BEEPBEEP_SID,beepbeep_args:get_session_id(Env),[{path, "/"}]).

0 comments on commit e1be499

Please sign in to comment.