forked from webmachine/webmachine
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
540 additions
and
256 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ deps/* | |
ebin/*.app | ||
doc/* | ||
.local_dialyzer_plt | ||
/.rebar |
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,3 +1,4 @@ | ||
%%-*- mode: erlang -*- | ||
{["demo", '*'], webmachine_demo_resource, []}. | ||
{["fs", '*'], webmachine_demo_fs_resource, [{root, "/tmp/fs"}]}. | ||
{["upload", '*'], webmachine_demo_upload_resource, []}. |
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 @@ | ||
# Ignore everything in this directory | ||
* | ||
# Except this file | ||
!.gitignore |
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,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"}} | ||
]}. |
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 |
---|---|---|
@@ -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}. |
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,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> |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -14,5 +14,8 @@ | |
webmachine | ||
]}, | ||
{mod, { {{appid}}_app, []}}, | ||
{env, []} | ||
{env, [ | ||
{web_ip, "0.0.0.0"}, | ||
{web_port, 8080} | ||
]} | ||
]}. |
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,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. |
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,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. |
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,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()} | ||
]. |
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,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}. | ||
|
Oops, something went wrong.