Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Brett Hazen committed Jan 16, 2015
2 parents 26d04d8 + 72bc0f4 commit f6b99d9
Show file tree
Hide file tree
Showing 37 changed files with 540 additions and 256 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ deps/*
ebin/*.app
doc/*
.local_dialyzer_plt
/.rebar
12 changes: 9 additions & 3 deletions Makefile
Expand Up @@ -11,14 +11,17 @@ all: deps compile
compile: deps
./rebar compile

deps:
deps: DEV_MODE
@(./rebar get-deps)

clean:
@(./rebar clean)

distclean: clean
@(./rebar delete-deps)
# nuke deps first to avoid wasting time having rebar recurse into deps
# for clean
distclean:
@rm -rf deps ./rebar/DEV_MODE
@(./rebar clean)

edoc:
@$(ERL) -noshell -run edoc_run application '$(APP)' '"."' '[{preprocess, true},{includes, ["."]}]'
Expand All @@ -34,3 +37,6 @@ verbosetest: all
travisupload:
tar cvfz ${ARTIFACTSFILE} --exclude '*.beam' --exclude '*.erl' test.log .eunit
travis-artifacts upload --path ${ARTIFACTSFILE}

DEV_MODE:
@touch ./.rebar/DEV_MODE
2 changes: 1 addition & 1 deletion README.org
Expand Up @@ -35,7 +35,7 @@ make
./start.sh
#+END_SRC

The application will be available at [[http://localhost:8000]].
The application will be available at [[http://localhost:8080]].

To learn more continue reading [[http://webmachine.basho.com/][here]].

Expand Down
10 changes: 9 additions & 1 deletion demo/README
Expand Up @@ -13,9 +13,12 @@ start.sh : simple startup script for running webmachine_demo
/webmachine_demo_app.erl : base module for the Erlang application
/webmachine_demo_sup.erl : OTP supervisor for the application
/webmachine_demo_resource.erl : a simple example Webmachine resource
/webmachine_demo_fs_resource.erl : a simple filesystem resource
/webmachine_demo_upload_resource.erl : a simple file upload resource
/priv
/dispatch.conf : the Webmachine URL-dispatching table
/www : a convenient place to put your static web content
/www/upload : where uploaded files are saved

You probably want to do one of a couple of things at this point:

Expand All @@ -38,6 +41,11 @@ You probably want to do one of a couple of things at this point:
$ echo "Hello World." > /tmp/fs/demo.txt
Visit http://localhost:8000/fs/demo.txt

5. Add some new resources:
5. Test the file upload resource:
Visit http://localhost:8000/upload
Upload a file
Look in folder /priv/www/uploads

6. Add some new resources:
edit src/YOUR_NEW_RESOURCE.erl
edit priv/dispatch.conf
1 change: 1 addition & 0 deletions demo/priv/dispatch.conf
@@ -1,3 +1,4 @@
%%-*- mode: erlang -*-
{["demo", '*'], webmachine_demo_resource, []}.
{["fs", '*'], webmachine_demo_fs_resource, [{root, "/tmp/fs"}]}.
{["upload", '*'], webmachine_demo_upload_resource, []}.
4 changes: 4 additions & 0 deletions demo/priv/www/uploads/.gitignore
@@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore
5 changes: 4 additions & 1 deletion demo/rebar.config
@@ -1,3 +1,6 @@
%%-*- mode: erlang -*-

{deps, [{webmachine, "1.10.*", {git, "git://github.com/basho/webmachine", "HEAD"}}]}.
{deps, [
{webmachine, "1.10.*", {git, "git://github.com/basho/webmachine", "HEAD"}},
{erlydtl, ".*", {git, "git://github.com/evanmiller/erlydtl", "HEAD"}}
]}.
17 changes: 2 additions & 15 deletions demo/src/webmachine_demo.erl
Expand Up @@ -3,35 +3,22 @@
-author('Justin Sheehy <justin@@basho.com>').
-export([start/0, start_link/0, stop/0]).

ensure_started(App) ->
case application:start(App) of
ok ->
ok;
{error, {already_started, App}} ->
ok
end.

%% @spec start_link() -> {ok,Pid::pid()}
%% @doc Starts the app for inclusion in a supervisor tree
start_link() ->
ensure_started(crypto),
ensure_started(mochiweb),
LogHandlers = [{webmachine_access_log_handler, ["priv/log"]},
{webmachine_error_log_handler, ["priv/log"]}],
application:set_env(webmachine, log_handlers, LogHandlers),
ensure_started(webmachine),
webmachine_util:ensure_all_started(webmachine),
webmachine_demo_sup:start_link().

%% @spec start() -> ok
%% @doc Start the webmachine_demo server.
start() ->
ensure_started(inets),
ensure_started(crypto),
ensure_started(mochiweb),
LogHandlers = [{webmachine_access_log_handler, ["priv/log"]},
{webmachine_error_log_handler, ["priv/log"]}],
application:set_env(webmachine, log_handlers, LogHandlers),
ensure_started(webmachine),
webmachine_util:ensure_all_started(webmachine),
application:start(webmachine_demo).

%% @spec stop() -> ok
Expand Down
53 changes: 53 additions & 0 deletions demo/src/webmachine_demo_upload_resource.erl
@@ -0,0 +1,53 @@
%% @author Voila <th3rac25@gmail.com>
%% @copyright 2007-2009 Basho Technologies, Inc. All Rights Reserved.
%% @doc Example webmachine_resource.

-module(webmachine_demo_upload_resource).
-export([init/1, to_html/2, allowed_methods/2, content_types_provided/2, process_post/2]).

-include_lib("webmachine/include/webmachine.hrl").

-define(UPLOAD_DIR, 'priv/www/uploads').

init([]) -> {ok, undefined}.

allowed_methods(ReqData, Context) ->
{['GET', 'POST', 'HEAD'], ReqData, Context}.

content_types_provided(ReqData, Context) ->
{[{"text/html", to_html}], ReqData, Context}.

process_post(ReqData, Context) ->
Body = wrq:req_body(ReqData),
Boundary = webmachine_multipart:find_boundary(ReqData),
Parts = webmachine_multipart:get_all_parts(Body, Boundary),
{FileName, FileData} = get_file(Parts),
ok = save_file(FileName, FileData),
Msg = ["File '", FileName, "' successfully uploaded."],
ReqData1 = wrq:set_resp_header("location", ["/upload?msg=", Msg], ReqData),
ReqData2 = wrq:do_redirect(true, ReqData1),
{true, ReqData2, Context}.


to_html(ReqData, Context) ->
Vars = case wrq:get_qs_value("msg", ReqData) of
undefined -> [];
Msg -> [{msg, Msg}]
end,
{ok, Content} = form_dtl:render(Vars),
{Content, ReqData, Context}.

%%-----------------------------------------------------------------------------
%% Internal Functions
%%-----------------------------------------------------------------------------
binary_to_atom(Bin) ->
list_to_atom(binary_to_list(Bin)).

save_file(FileName, Bin) ->
FilePath = [?UPLOAD_DIR, '/', binary_to_atom(FileName)],
file:write_file(FilePath, Bin).

get_file(Parts) ->
{_, {Params, _}, FileData} = lists:keyfind("file", 1, Parts),
{_, FileName} = proplists:lookup(<<"filename">>, Params),
{FileName, FileData}.
13 changes: 13 additions & 0 deletions demo/templates/form.dtl
@@ -0,0 +1,13 @@
<html>
<body>
{% if msg %}<p>{{ msg }}</p>{% endif %}
<form action="/upload" method="post" enctype="multipart/form-data">
<p>
<input type="file" name="file">
</p>
<p>
<button type="submit">Upload</button>
</p>
</form>
</body>
</html>
2 changes: 0 additions & 2 deletions priv/templates/priv/dispatch.conf

This file was deleted.

5 changes: 4 additions & 1 deletion priv/templates/src/wmskel.app.src
Expand Up @@ -14,5 +14,8 @@
webmachine
]},
{mod, { {{appid}}_app, []}},
{env, []}
{env, [
{web_ip, "0.0.0.0"},
{web_port, 8080}
]}
]}.
79 changes: 36 additions & 43 deletions priv/templates/src/wmskel.erl
@@ -1,48 +1,41 @@
%% @author author <author@example.com>
%% @copyright YYYY author.

%% @doc {{appid}} startup code

-module({{appid}}).
-author('author <author@example.com>').
-export([start/0, start_link/0, stop/0]).
-export([
start/0
]).

ensure_started(App) ->
case application:start(App) of
ok ->
ok;
{error, {already_started, App}} ->
ok
end.
-spec start() -> ok.
start() ->
_ = [ application:start(Dep) || Dep <- resolve_deps({{appid}}),
not is_otp_base_app(Dep) ],
ok.

%% @spec start_link() -> {ok,Pid::pid()}
%% @doc Starts the app for inclusion in a supervisor tree
start_link() ->
ensure_started(inets),
ensure_started(crypto),
ensure_started(mochiweb),
application:set_env(webmachine, webmachine_logger_module,
webmachine_logger),
ensure_started(webmachine),
{{appid}}_sup:start_link().
-spec dep_apps(atom()) -> [atom()].
dep_apps(App) ->
application:load(App),
{ok, Apps} = application:get_key(App, applications),
Apps.

%% @spec start() -> ok
%% @doc Start the {{appid}} server.
start() ->
ensure_started(inets),
ensure_started(crypto),
ensure_started(mochiweb),
application:set_env(webmachine, webmachine_logger_module,
webmachine_logger),
ensure_started(webmachine),
application:start({{appid}}).
-spec all_deps(atom(), [atom()]) -> [atom()].
all_deps(App, Deps) ->
[[ all_deps(Dep, [App|Deps]) || Dep <- dep_apps(App),
not lists:member(Dep, Deps)], App].

-spec resolve_deps(atom()) -> [atom()].
resolve_deps(App) ->
DepList = all_deps(App, []),
{AppOrder, _} = lists:foldl(fun(A,{List,Set}) ->
case sets:is_element(A, Set) of
true ->
{List, Set};
false ->
{List ++ [A], sets:add_element(A, Set)}
end
end,
{[], sets:new()},
lists:flatten(DepList)),
AppOrder.

%% @spec stop() -> ok
%% @doc Stop the {{appid}} server.
stop() ->
Res = application:stop({{appid}}),
application:stop(webmachine),
application:stop(mochiweb),
application:stop(crypto),
application:stop(inets),
Res.
-spec is_otp_base_app(atom()) -> boolean().
is_otp_base_app(kernel) -> true;
is_otp_base_app(stdlib) -> true;
is_otp_base_app(_) -> false.
16 changes: 4 additions & 12 deletions priv/templates/src/wmskel_app.erl
@@ -1,21 +1,13 @@
%% @author author <author@example.com>
%% @copyright YYYY author.

%% @doc Callbacks for the {{appid}} application.

-module({{appid}}_app).
-author('author <author@example.com>').

-behaviour(application).
-export([start/2,stop/1]).

-export([
start/2,
stop/1
]).

%% @spec start(_Type, _StartArgs) -> ServerRet
%% @doc application start callback for {{appid}}.
start(_Type, _StartArgs) ->
{{appid}}_sup:start_link().

%% @spec stop(_State) -> ServerRet
%% @doc application stop callback for {{appid}}.
stop(_State) ->
ok.
23 changes: 23 additions & 0 deletions priv/templates/src/wmskel_config.erl
@@ -0,0 +1,23 @@
-module({{appid}}_config).

-export([
dispatch/0,
web_config/0
]).

-spec dispatch() -> [webmachine_dispatcher:route()].
dispatch() ->
lists:flatten([
{[], {{appid}}_resource, []}
]).

web_config() ->
{ok, App} = application:get_application(?MODULE),
{ok, Ip} = application:get_env(App, web_ip),
{ok, Port} = application:get_env(App, web_port),
[
{ip, Ip},
{port, Port},
{log_dir, "priv/log"},
{dispatch, dispatch()}
].
15 changes: 9 additions & 6 deletions priv/templates/src/wmskel_resource.erl
@@ -1,13 +1,16 @@
%% @author author <author@example.com>
%% @copyright YYYY author.
%% @doc Example webmachine_resource.

-module({{appid}}_resource).
-export([init/1, to_html/2]).
-export([
init/1,
to_html/2
]).

-include_lib("webmachine/include/webmachine.hrl").

init([]) -> {ok, undefined}.
-spec init(list()) -> {ok, term()}.
init([]) ->
{ok, undefined}.

-spec to_html(wrq:reqdata(), term()) -> {iodata(), wrq:reqdata(), term()}.
to_html(ReqData, State) ->
{"<html><body>Hello, new world</body></html>", ReqData, State}.

0 comments on commit f6b99d9

Please sign in to comment.