mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Add a web server example
Explore re-routing in middleware.
This commit is contained in:
parent
e3daf439da
commit
f112cdf643
14 changed files with 223 additions and 0 deletions
|
@ -40,5 +40,8 @@ Cowboy Examples
|
|||
* [static_world](./static_world):
|
||||
static file handler
|
||||
|
||||
* [web_sever](./web_server):
|
||||
serves files with lists directory entries
|
||||
|
||||
* [websocket](./websocket):
|
||||
websocket example
|
||||
|
|
27
examples/web_server/README.md
Normal file
27
examples/web_server/README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
Cowboy Static File Handler with Index Support
|
||||
=============================================
|
||||
|
||||
To compile this example you need rebar in your PATH.
|
||||
|
||||
Type the following command:
|
||||
```
|
||||
$ rebar get-deps compile
|
||||
```
|
||||
|
||||
You can then start the Erlang node with the following command:
|
||||
```
|
||||
./start.sh
|
||||
```
|
||||
|
||||
Cowboy will serve all the files you put in the priv/ directory. You can replace
|
||||
the filename given in the example URL with the one of a file you added to this
|
||||
directory to receive that file. A middleware has been added that will re-route
|
||||
the request to a different handler if the requested path is a directory.
|
||||
|
||||
Example
|
||||
-------
|
||||
|
||||
Point your browser to http://localhost:8080 to see the contents of `priv/`. You
|
||||
can click on a link to see that file. If HTML is not preferred, the contents of
|
||||
a directory will be listed as a JSON array (e.g. with `curl
|
||||
http://localhost:8080`).
|
BIN
examples/web_server/priv/small.mp4
Normal file
BIN
examples/web_server/priv/small.mp4
Normal file
Binary file not shown.
BIN
examples/web_server/priv/small.ogv
Normal file
BIN
examples/web_server/priv/small.ogv
Normal file
Binary file not shown.
1
examples/web_server/priv/test.txt
Normal file
1
examples/web_server/priv/test.txt
Normal file
|
@ -0,0 +1 @@
|
|||
If you read this then the static file server works!
|
11
examples/web_server/priv/video.html
Normal file
11
examples/web_server/priv/video.html
Normal file
|
@ -0,0 +1,11 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<body>
|
||||
<h1>HTML5 Video Example</h1>
|
||||
<video controls>
|
||||
<source src="small.ogv" type="video/ogg"/>
|
||||
<source src="small.mp4" type="video/mp4"/>
|
||||
</video>
|
||||
<p>Videos taken from <a href="http://techslides.com/sample-webm-ogg-and-mp4-video-files-for-html5/">TechSlides</a></p>
|
||||
</body>
|
||||
</html>
|
8
examples/web_server/rebar.config
Normal file
8
examples/web_server/rebar.config
Normal file
|
@ -0,0 +1,8 @@
|
|||
{deps, [
|
||||
{cowboy, ".*",
|
||||
{git, "git://github.com/extend/cowboy.git", "master"}},
|
||||
{mimetypes, ".*",
|
||||
{git, "git://github.com/spawngrid/mimetypes.git", "master"}},
|
||||
{jsx, ".*",
|
||||
{git, "git://github.com/talentdeficit/jsx.git", "master"}}
|
||||
]}.
|
51
examples/web_server/src/directory_handler.erl
Normal file
51
examples/web_server/src/directory_handler.erl
Normal file
|
@ -0,0 +1,51 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
%% @doc Directory handler.
|
||||
-module(directory_handler).
|
||||
|
||||
%% REST Callbacks
|
||||
-export([init/3]).
|
||||
-export([rest_init/2]).
|
||||
-export([allowed_methods/2]).
|
||||
-export([resource_exists/2]).
|
||||
-export([content_types_provided/2]).
|
||||
|
||||
%% Callback Callbacks
|
||||
-export([list_json/2]).
|
||||
-export([list_html/2]).
|
||||
|
||||
init(_Transport, _Req, _Paths) ->
|
||||
{upgrade, protocol, cowboy_rest}.
|
||||
|
||||
rest_init(Req, Paths) ->
|
||||
{ok, Req, Paths}.
|
||||
|
||||
allowed_methods(Req, State) ->
|
||||
{[<<"GET">>], Req, State}.
|
||||
|
||||
resource_exists(Req, {ReqPath, FilePath}) ->
|
||||
case file:list_dir(FilePath) of
|
||||
{ok, Fs} -> {true, Req, {ReqPath, lists:sort(Fs)}};
|
||||
_Err -> {false, Req, {ReqPath, FilePath}}
|
||||
end.
|
||||
|
||||
content_types_provided(Req, State) ->
|
||||
{[
|
||||
{{<<"application">>, <<"json">>, []}, list_json},
|
||||
{{<<"text">>, <<"html">>, []}, list_html}
|
||||
], Req, State}.
|
||||
|
||||
list_json(Req, {Path, Fs}) ->
|
||||
Files = [[ <<(list_to_binary(F))/binary>> || F <- Fs ]],
|
||||
{jsx:encode(Files), Req, Path}.
|
||||
|
||||
list_html(Req, {Path, Fs}) ->
|
||||
Body = [[ links(Path, F) || F <- [".."|Fs] ]],
|
||||
HTML = [<<"<!DOCTYPE html><html><head><title>Index</title></head>",
|
||||
"<body>">>, Body, <<"</body></html>\n">>],
|
||||
{HTML, Req, Path}.
|
||||
|
||||
links(<<>>, File) ->
|
||||
["<a href='/", File, "'>", File, "</a><br>\n"];
|
||||
links(Prefix, File) ->
|
||||
["<a href='/", Prefix, $/, File, "'>", File, "</a><br>\n"].
|
37
examples/web_server/src/directory_lister.erl
Normal file
37
examples/web_server/src/directory_lister.erl
Normal file
|
@ -0,0 +1,37 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(directory_lister).
|
||||
-behaviour(cowboy_middleware).
|
||||
|
||||
-export([execute/2]).
|
||||
|
||||
execute(Req, Env) ->
|
||||
case lists:keyfind(handler, 1, Env) of
|
||||
{handler, cowboy_static} -> redirect_directory(Req, Env);
|
||||
_H -> {ok, Req, Env}
|
||||
end.
|
||||
|
||||
redirect_directory(Req, Env) ->
|
||||
{Path, Req1} = cowboy_req:path_info(Req),
|
||||
Path1 = << <<S/binary, $/>> || S <- Path >>,
|
||||
{handler_opts, StaticOpts} = lists:keyfind(handler_opts, 1, Env),
|
||||
{dir_handler, DirHandler} = lists:keyfind(dir_handler, 1, StaticOpts),
|
||||
FullPath = resource_path(Path1),
|
||||
case valid_path(Path) and filelib:is_dir(FullPath) of
|
||||
true -> handle_directory(Req1, Env, Path1, FullPath, DirHandler);
|
||||
false -> {ok, Req1, Env}
|
||||
end.
|
||||
|
||||
handle_directory(Req, Env, Prefix, Path, DirHandler) ->
|
||||
Env1 = lists:keydelete(handler, 1,
|
||||
lists:keydelete(handler_opts, 1, Env)),
|
||||
{ok, Req, [{handler, DirHandler}, {handler_opts, {Prefix, Path}} | Env1]}.
|
||||
|
||||
valid_path([]) -> true;
|
||||
valid_path([<<"..">> | _T]) -> false;
|
||||
valid_path([<<"/", _/binary>> | _T]) -> false;
|
||||
valid_path([_H | Rest]) -> valid_path(Rest).
|
||||
|
||||
resource_path(Path) ->
|
||||
{ok, Cwd} = file:get_cwd(),
|
||||
filename:join([Cwd, "priv", Path]).
|
15
examples/web_server/src/web_server.app.src
Normal file
15
examples/web_server/src/web_server.app.src
Normal file
|
@ -0,0 +1,15 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
{application, web_server, [
|
||||
{description, "Cowboy static file handler with directory indexes."},
|
||||
{vsn, "1"},
|
||||
{modules, []},
|
||||
{registered, []},
|
||||
{applications, [
|
||||
kernel,
|
||||
stdlib,
|
||||
cowboy
|
||||
]},
|
||||
{mod, {web_server_app, []}},
|
||||
{env, []}
|
||||
]}.
|
14
examples/web_server/src/web_server.erl
Normal file
14
examples/web_server/src/web_server.erl
Normal file
|
@ -0,0 +1,14 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
-module(web_server).
|
||||
|
||||
%% API.
|
||||
-export([start/0]).
|
||||
|
||||
%% API.
|
||||
|
||||
start() ->
|
||||
ok = application:start(crypto),
|
||||
ok = application:start(ranch),
|
||||
ok = application:start(cowboy),
|
||||
ok = application:start(web_server).
|
30
examples/web_server/src/web_server_app.erl
Normal file
30
examples/web_server/src/web_server_app.erl
Normal file
|
@ -0,0 +1,30 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
%% @private
|
||||
-module(web_server_app).
|
||||
-behaviour(application).
|
||||
|
||||
%% API.
|
||||
-export([start/2]).
|
||||
-export([stop/1]).
|
||||
|
||||
%% API.
|
||||
|
||||
start(_Type, _Args) ->
|
||||
Dispatch = cowboy_router:compile([
|
||||
{'_', [
|
||||
{"/[...]", cowboy_static, [
|
||||
{directory, {priv_dir, web_server, []}},
|
||||
{dir_handler, directory_handler},
|
||||
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
|
||||
]}
|
||||
]}
|
||||
]),
|
||||
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
|
||||
{env, [{dispatch, Dispatch}]},
|
||||
{middlewares, [cowboy_router, directory_lister, cowboy_handler]}
|
||||
]),
|
||||
web_server_sup:start_link().
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
23
examples/web_server/src/web_server_sup.erl
Normal file
23
examples/web_server/src/web_server_sup.erl
Normal file
|
@ -0,0 +1,23 @@
|
|||
%% Feel free to use, reuse and abuse the code in this file.
|
||||
|
||||
%% @private
|
||||
-module(web_server_sup).
|
||||
-behaviour(supervisor).
|
||||
|
||||
%% API.
|
||||
-export([start_link/0]).
|
||||
|
||||
%% supervisor.
|
||||
-export([init/1]).
|
||||
|
||||
%% API.
|
||||
|
||||
-spec start_link() -> {ok, pid()}.
|
||||
start_link() ->
|
||||
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
|
||||
|
||||
%% supervisor.
|
||||
|
||||
init([]) ->
|
||||
Procs = [],
|
||||
{ok, {{one_for_one, 10, 10}, Procs}}.
|
3
examples/web_server/start.sh
Executable file
3
examples/web_server/start.sh
Executable file
|
@ -0,0 +1,3 @@
|
|||
#!/bin/sh
|
||||
erl -pa ebin deps/*/ebin -s web_server \
|
||||
-eval "io:format(\"Point your browser at http://localhost:8080/~n\")."
|
Loading…
Add table
Add a link
Reference in a new issue