This repository has been archived by the owner on Nov 7, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Completely revamped and improved
- Loading branch information
1 parent
8803a2f
commit 5cce39e
Showing
19 changed files
with
558 additions
and
199 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
|
||
%% Session Id key | ||
-define(SID,"_beepbeep_session_id"). | ||
-define(BEEPBEEP_SID, "_beepbeep_session_id_"). | ||
|
||
%% Holds parameter information passes around | ||
-record(params,{ | ||
controller, | ||
action, | ||
id, | ||
method, | ||
data, %% Query and Post Data | ||
sid %% Session Id | ||
}). | ||
%% Environment data | ||
-define(BEEPBEEP_ENV_DATA, [{server_sw, "SERVER_SOFTWARE"}, | ||
{server_name, "SERVER_NAME"}, | ||
{server_protocol, "SERVER_PROTOCOL"}, | ||
{server_port, "SERVER_PORT"}, | ||
{method, "REQUEST_METHOD"}, | ||
{content_type, "CONTENT_TYPE"}, | ||
{content_length,"CONTENT_LENGTH"}, | ||
{path_info, "PATH_INFO"}, | ||
{remote_addr, "REMOTE_ADDR"}, | ||
{beepbeep_params, "beepbeep.data"}]). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%% | ||
%% Sample default controller | ||
%% | ||
-module(home_controller,[Env]). | ||
|
||
-export([handle_request/2,before_filter/0]). | ||
|
||
handle_request("index",[]) -> | ||
{render,"home/index.html",[{data,"Hello There From BeepBeep!"}]}; | ||
|
||
handle_request("show",[Year]) -> | ||
Sid = beepbeep_args:get_session_id(Env), | ||
Name = beepbeep_args:get_param("name",Env), | ||
{render,"home/show.html",[{year,Year},{sid,Sid},{name,Name}]}. | ||
|
||
|
||
before_filter() -> | ||
FilterOnly = ["show"], | ||
case lists:member(beepbeep_args:get_action(Env),FilterOnly) of | ||
true -> | ||
error_logger:info_report("Doing the filter for SHOW~n"), | ||
ok; | ||
false -> | ||
ok | ||
end. | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
skel, | ||
skel_app, | ||
skel_sup, | ||
skel_web, | ||
skel_deps | ||
]}, | ||
{registered, []}, | ||
|
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,46 @@ | ||
-module(skel_web). | ||
-author('Dave Bryson <http://weblog.miceda.org>'). | ||
|
||
-export([start/1, stop/0, loop/2]). | ||
|
||
-export([start/1, stop/0, loop/1]). | ||
-include("beepbeep.hrl"). | ||
|
||
start(Options) -> | ||
{DocRoot, Options1} = get_option(docroot, Options), | ||
Loop = fun (Req) -> | ||
?MODULE:loop(Req, DocRoot) | ||
end, | ||
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options1]). | ||
?MODULE:loop(Req) | ||
end, | ||
mochiweb_http:start([{name, ?MODULE}, {loop, Loop} | Options]). | ||
|
||
stop() -> | ||
mochiweb_http:stop(?MODULE). | ||
|
||
loop(Req, DocRoot) -> | ||
Path = Req:get(path), | ||
RequestMethod = Req:get(method), | ||
Params = case RequestMethod of | ||
Method when Method =:= 'GET'; Method =:= 'HEAD' -> | ||
Req:parse_qs(); | ||
_ -> | ||
Req:parse_post() | ||
end, | ||
loop(Req) -> | ||
%% Setup env... | ||
InitialEnv = mochiweb_env:setup_environment(Req), | ||
Env = setup_session(Req,InitialEnv), | ||
%%error_logger:info_report(Env), | ||
|
||
%% Setup the Session | ||
CookieKey = beepbeep_session_server:new_session(Req:get_cookie_value(?SID)), | ||
%% Setup Params structure for controllers | ||
P1 = #params{sid=CookieKey,data=Params,method=RequestMethod}, | ||
|
||
|
||
%% Route the request | ||
case dispatch(Path,P1) of | ||
{render,Template,Data,P} -> | ||
H = mochiweb_cookies:cookie(?SID,P#params.sid, [{path, "/"}]), | ||
{ok,Content} = render_template(Template,Data), | ||
Req:ok({"text/html",[H],Content}); | ||
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}); | ||
{redirect,Url} -> | ||
Req:respond({302, | ||
[{"Location", Url}, | ||
{"Content-Type", "text/html; charset=UTF-8"}], | ||
""}); | ||
{static,File} -> | ||
Req:serve_file(File,DocRoot) | ||
{static, File} -> | ||
"/" ++ StaticFile = File, | ||
Req:serve_file(StaticFile,skel_deps:local_path(["www"])); | ||
{error,_} -> | ||
Req:respond({500,[],"Server Error"}) | ||
end. | ||
|
||
%% Internal API | ||
|
||
get_option(Option, Options) -> | ||
{proplists:get_value(Option, Options), proplists:delete(Option, Options)}. | ||
|
||
%% Route the Request | ||
dispatch(Path,Params) -> | ||
PathParts = string:tokens(Path,"/"), | ||
P1 = extract_route_info(PathParts,Params), | ||
Controller = P1#params.controller, | ||
Action = P1#params.action, | ||
|
||
Reply = case catch(Controller:Action(P1)) of | ||
{'EXIT', {undef, _}} -> | ||
%% Try static | ||
"/" ++ StaticPath = Path, | ||
{static,StaticPath}; | ||
Any -> | ||
io:format("Ran with ~p~n",[Any]), | ||
Any | ||
end, | ||
Reply. | ||
|
||
|
||
%% Parse out the route information from the path | ||
extract_route_info([],P) -> | ||
%% Default route of root ("/") path | ||
P1 = P, | ||
P1#params{controller=main_controller,action=index}; | ||
extract_route_info([C],P) -> | ||
%% Default "/hello" to "hello_controller:index" | ||
P1 = P, | ||
P1#params{controller=make_controller_name(C),action=index}; | ||
extract_route_info([C,A],P) -> | ||
P1 = P, | ||
P1#params{controller=make_controller_name(C),action=list_to_atom(A)}; | ||
extract_route_info([C,A,Id],P) -> | ||
P1 = P, | ||
P1#params{controller=make_controller_name(C),action=list_to_atom(A), id=Id}. | ||
|
||
make_controller_name(ControllerName) -> | ||
list_to_atom(ControllerName ++ "_controller"). | ||
|
||
|
||
%% Render the Template via erlydtl | ||
render_template(File,Data) -> | ||
FullPathToFile = skel_deps:local_path(["templates",File]), | ||
|
||
|
||
%% Make a module name | ||
F = lists:reverse(string:tokens(File,"/")), | ||
[N,_] = string:tokens(hd(F),"."), | ||
Mn = string:join([N,"template"],"_"), | ||
ModName = list_to_atom(Mn), | ||
get_cookie(Env) -> | ||
mochiweb_cookies:cookie(?BEEPBEEP_SID,beepbeep_args:get_session_id(Env),[{path, "/"}]). | ||
|
||
erlydtl:compile(FullPathToFile,ModName), | ||
ModName:render(Data). | ||
setup_session(Req,Env) -> | ||
SessionKey = beepbeep_session_server:new_session(Req:get_cookie_value(?BEEPBEEP_SID)), | ||
beepbeep_args:set_session_id(SessionKey,Env). |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" | ||
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | ||
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | ||
<head> | ||
<meta http-equiv="content-type" content="text/html;charset=UTF-8" /> | ||
<title>Welcome to BeepBeep</title> | ||
<link href="/stylesheets/style.css" rel="stylesheet" type="text/css"/> | ||
</head> | ||
<body> | ||
<h1>BeepBeep</h1> | ||
<p>A simple example using BeepBeep</p> | ||
{% block content %}{% endblock %} | ||
</body> | ||
</html> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{% extends "../base.html" %} | ||
{% block content %} | ||
Message is: {{ data }} | ||
{% endblock %} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "../base.html" %} | ||
{% block content %} | ||
You choose year: {{ year }} | ||
<p> | ||
Session id: {{ sid }} | ||
</p> | ||
<p> | ||
Name: {{ name }} | ||
</p> | ||
{% endblock %} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
body | ||
{ | ||
background-color: #fff; color: #333; | ||
font-family: verdana, arial, helvetica, sans-serif; | ||
font-size: 13px; | ||
margin-left: 50px; | ||
margin-top: 50px; | ||
margin-right: 200px; | ||
} |
Oops, something went wrong.