Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from choptastic/master
Add Cowboy support. Fix webmachine compilation
- Loading branch information
Showing
13 changed files
with
290 additions
and
9 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
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,10 @@ | ||
% vim: ts=4 sw=4 et | ||
[ | ||
{cowboy,[ | ||
{bind_address,"0.0.0.0"}, | ||
{bind_port,8000}, | ||
{server_name,nitrogen}, | ||
{document_root,"./site/static"}, | ||
{static_paths, ["js/","images/","css/","nitrogen/"]} | ||
]} | ||
]. |
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,26 @@ | ||
{sub_dirs, [ | ||
"site", | ||
"deps" | ||
]}. | ||
|
||
{require_otp_vsn, "R13B04|R14|R15"}. | ||
|
||
{cover_enabled, true}. | ||
|
||
{erl_opts, [debug_info, fail_on_warning]}. | ||
|
||
{deps_dir, ["lib"]}. | ||
|
||
{deps, [ | ||
{cowboy, "0.5.*", {git, "git://github.com/extend/cowboy",{tag, "HEAD"}}}, | ||
{mimetypes, ".*", {git, "git://github.com/spawngrid/mimetypes", {tag, "HEAD"}}}, | ||
|
||
{nitrogen_core, "2.1.*", {git, "git://github.com/nitrogen/nitrogen_core", "HEAD"}}, | ||
{nprocreg, "0.2.*", {git, "git://github.com/nitrogen/nprocreg", "HEAD"}}, | ||
{simple_bridge, "1.2.*", {git, "git://github.com/nitrogen/simple_bridge", "HEAD"}}, | ||
{sync, "0.1.*", {git, "git://github.com/rustyio/sync.git", "HEAD"}} | ||
%% {nitrogen_core, "2.1.*", {git, "git://github.com/nitrogen/nitrogen_core", {tag, "v2.1.0"}}}, | ||
%% {nprocreg, "0.2.*", {git, "git://github.com/nitrogen/nprocreg", {tag, "v0.2.0"}}}, | ||
%% {simple_bridge, "1.2.*", {git, "git://github.com/nitrogen/simple_bridge", {tag, "v1.2.0"}}}, | ||
%% {sync, "0.1.*", {git, "git://github.com/rustyio/sync.git", {tag, "v0.1.0"}}} | ||
]}. |
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 @@ | ||
-module(nitrogen_app). | ||
-behaviour(application). | ||
-export([start/2, stop/1]). | ||
|
||
%% =================================================================== | ||
%% Application callbacks | ||
%% =================================================================== | ||
|
||
start(_StartType, _StartArgs) -> | ||
nitrogen_sup:start_link(). | ||
|
||
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,37 @@ | ||
%% vim: ts=4 sw=4 et | ||
|
||
-module(nitrogen_cowboy). | ||
-include_lib("nitrogen_core/include/wf.hrl"). | ||
|
||
-export([init/3, handle/2, terminate/2]). | ||
|
||
-record(state, {headers, body}). | ||
|
||
init({_Transport, http}, Req, Opts) -> | ||
Headers = proplists:get_value(headers, Opts, []), | ||
Body = proplists:get_value(body, Opts, "http_handler"), | ||
{ok, Req, #state{headers=Headers, body=Body}}. | ||
|
||
|
||
handle(Req,_Opts) -> | ||
{ok, DocRoot} = application:get_env(cowboy, document_root), | ||
RequestBridge = simple_bridge:make_request(cowboy_request_bridge, | ||
{Req, DocRoot}), | ||
|
||
%% Becaue Cowboy usese the same "Req" record, we can pass the | ||
%% previously made RequestBridge to make_response, and it'll | ||
%% parse out the relevant bits to keep both parts (request and | ||
%% response) using the same "Req" | ||
ResponseBridge = simple_bridge:make_response(cowboy_response_bridge, | ||
RequestBridge), | ||
|
||
%% Establishes the context with the Request and Response Bridges | ||
nitrogen:init_request(RequestBridge, ResponseBridge), | ||
|
||
{ok, NewReq} = nitrogen:run(), | ||
|
||
%% This will be returned back to cowboy | ||
{ok, NewReq, _Opts}. | ||
|
||
terminate(_Req, _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,81 @@ | ||
%% -*- mode: nitrogen -*- | ||
%% vim: ts=4 sw=4 et | ||
-module(nitrogen_sup). | ||
-behaviour(supervisor). | ||
-export([ | ||
start_link/0, | ||
init/1 | ||
]). | ||
|
||
%% Helper macro for declaring children of supervisor | ||
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}). | ||
|
||
%% =================================================================== | ||
%% API functions | ||
%% =================================================================== | ||
|
||
start_link() -> | ||
supervisor:start_link({local, ?MODULE}, ?MODULE, []). | ||
|
||
%% =================================================================== | ||
%% Supervisor callbacks | ||
%% =================================================================== | ||
|
||
init([]) -> | ||
%% Start the Process Registry... | ||
application:start(nprocreg), | ||
|
||
%% Start Cowboy... | ||
application:start(cowboy), | ||
{ok, BindAddress} = application:get_env(cowboy, bind_address), | ||
{ok, Port} = application:get_env(cowboy, bind_port), | ||
{ok, ServerName} = application:get_env(cowboy, server_name), | ||
{ok, DocRoot} = application:get_env(cowboy, document_root), | ||
{ok, StaticPaths} = application:get_env(cowboy, static_paths), | ||
DocRootBin = wf:to_binary(DocRoot), | ||
|
||
io:format("Starting Cowboy Server (~s) on ~s:~p, root: '~s'~n", | ||
[ServerName, BindAddress, Port, DocRoot]), | ||
|
||
StaticDispatches = lists:map(fun(Dir) -> | ||
Path = reformat_path(Dir), | ||
Handler = cowboy_http_static, | ||
Opts = [ | ||
{directory, filename:join([DocRootBin | Path])}, | ||
{mimetypes, {fun mimetypes:path_to_mimes/2, default}} | ||
], | ||
{Path ++ ['...'],Handler,Opts} | ||
end,StaticPaths), | ||
|
||
%% HandlerModule will end up calling HandlerModule:handle(Req,HandlerOpts) | ||
HandlerModule = nitrogen_cowboy, | ||
HandlerOpts = [], | ||
|
||
%% Start Cowboy... | ||
%% NOTE: According to Loic, there's no way to pass the buck back to cowboy | ||
%% to handle static dispatch files so we want to make sure that any large | ||
%% files get caught in general by cowboy and are never passed to the nitrogen | ||
%% handler at all. In general, your best bet is to include the directory in | ||
%% the static_paths section of cowboy.config | ||
Dispatch = [ | ||
%% Nitrogen will handle everything that's not handled in the StaticDispatches | ||
{'_', StaticDispatches ++ [{'_',HandlerModule , HandlerOpts}]} | ||
], | ||
HttpOpts = [{max_keepalive, 50}, {dispatch, Dispatch}], | ||
cowboy:start_listener(http, 100, | ||
cowboy_tcp_transport, [{port, Port}], | ||
cowboy_http_protocol, HttpOpts), | ||
|
||
{ok, { {one_for_one, 5, 10}, []} }. | ||
|
||
%% If the path is a string, it's going to get split on / and switched to the binary | ||
%% format that cowboy expects | ||
reformat_path(Path) when is_list(Path) andalso is_integer(hd(Path)) -> | ||
TokenizedPath = string:tokens(Path,"/"), | ||
[list_to_binary(Part) || Part <- TokenizedPath]; | ||
%% If path is just a binary, let's make it a string and treat it as such | ||
reformat_path(Path) when is_binary(Path) -> | ||
reformat_path(binary_to_list(Path)); | ||
%% If path is a list of binaries, then we assumed it's formatted for cowboy format already | ||
reformat_path(Path) when is_list(Path) andalso is_binary(hd(Path)) -> | ||
Path. |
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,49 @@ | ||
%% vim: ts=4 sw=4 et ft=erlang | ||
|
||
{sys, [ | ||
{lib_dirs, ["../deps"]}, | ||
{rel, "nitrogen", "2.0.4", | ||
[ | ||
kernel, | ||
stdlib, | ||
sasl, | ||
inets, | ||
crypto, | ||
runtime_tools, | ||
xmerl, | ||
compiler, | ||
ssl, | ||
mnesia, | ||
common_test, | ||
parsetools | ||
]}, | ||
{rel, "start_clean", "", | ||
[ | ||
kernel, | ||
stdlib | ||
]}, | ||
{boot_rel, "nitrogen"}, | ||
{profile, embedded}, | ||
{excl_sys_filters, [ | ||
"^bin/.*", | ||
"^erts.*/bin/(dialyzer|typer)" | ||
]}, | ||
{app, sasl, [{incl_cond, include}]}, | ||
{app, eunit, [{incl_cond, include}]}, | ||
{excl_archive_filters, ["^ebin$","^include$","^priv$"]}, | ||
{incl_cond, include} | ||
]}. | ||
|
||
{overlay, [ | ||
%% Copy common files... | ||
{copy, "./overlay/common/*"}, | ||
{copy, "./overlay/erts/*", "{{erts_vsn}}/bin"}, | ||
{copy, "../deps/nitrogen_core", "lib/nitrogen_core"}, | ||
{copy, "../deps/nitrogen_core/www", "site/static/nitrogen"}, | ||
{copy, "../deps/simple_bridge", "lib/simple_bridge"}, | ||
{copy, "../deps/nprocreg", "lib/nprocreg"}, | ||
{copy, "../deps/sync", "lib/sync"}, | ||
|
||
%% Copy Yaws files... | ||
{copy, "overlay/cowboy/*"} | ||
]}. |
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
Oops, something went wrong.