mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 12:20:24 +00:00
Convert the web_server example to a release
Temporary mimetypes list here too.
This commit is contained in:
parent
8bb7c180ff
commit
6a90d00cee
8 changed files with 37 additions and 50 deletions
14
examples/web_server/Makefile
Normal file
14
examples/web_server/Makefile
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
PROJECT = web_server
|
||||||
|
|
||||||
|
DEPS = cowboy
|
||||||
|
dep_cowboy = pkg://cowboy master
|
||||||
|
|
||||||
|
.PHONY: release clean-release
|
||||||
|
|
||||||
|
release: clean-release all
|
||||||
|
relx
|
||||||
|
|
||||||
|
clean-release:
|
||||||
|
rm -rf _rel
|
||||||
|
|
||||||
|
include ../../erlang.mk
|
|
@ -1,27 +1,20 @@
|
||||||
Cowboy Static File Handler with Index Support
|
Static file handler example
|
||||||
=============================================
|
===========================
|
||||||
|
|
||||||
To compile this example you need rebar in your PATH.
|
To try this example, you need GNU `make`, `git` and
|
||||||
|
[relx](https://github.com/erlware/relx) in your PATH.
|
||||||
|
|
||||||
Type the following command:
|
To build the example, run the following command:
|
||||||
```
|
|
||||||
$ rebar get-deps compile
|
``` bash
|
||||||
|
$ make
|
||||||
```
|
```
|
||||||
|
|
||||||
You can then start the Erlang node with the following command:
|
To start the release in the foreground:
|
||||||
```
|
|
||||||
./start.sh
|
``` bash
|
||||||
|
$ ./_rel/bin/web_server_example console
|
||||||
```
|
```
|
||||||
|
|
||||||
Cowboy will serve all the files you put in the priv/ directory. You can replace
|
Then point your browser at [http://localhost:8080](http://localhost:8080)
|
||||||
the filename given in the example URL with the one of a file you added to this
|
to browse the contents of the `priv` directory.
|
||||||
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`).
|
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
{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"}}
|
|
||||||
]}.
|
|
2
examples/web_server/relx.config
Normal file
2
examples/web_server/relx.config
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{release, {web_server_example, "1"}, [web_server]}.
|
||||||
|
{extended_start_script, true}.
|
|
@ -33,5 +33,4 @@ valid_path([<<"/", _/binary>> | _T]) -> false;
|
||||||
valid_path([_H | Rest]) -> valid_path(Rest).
|
valid_path([_H | Rest]) -> valid_path(Rest).
|
||||||
|
|
||||||
resource_path(Path) ->
|
resource_path(Path) ->
|
||||||
{ok, Cwd} = file:get_cwd(),
|
filename:join([code:priv_dir(web_server), Path]).
|
||||||
filename:join([Cwd, "priv", Path]).
|
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
%% 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(cowlib),
|
|
||||||
ok = application:start(ranch),
|
|
||||||
ok = application:start(cowboy),
|
|
||||||
ok = application:start(web_server).
|
|
|
@ -16,7 +16,12 @@ start(_Type, _Args) ->
|
||||||
{"/[...]", cowboy_static, [
|
{"/[...]", cowboy_static, [
|
||||||
{directory, {priv_dir, web_server, []}},
|
{directory, {priv_dir, web_server, []}},
|
||||||
{dir_handler, directory_handler},
|
{dir_handler, directory_handler},
|
||||||
{mimetypes, {fun mimetypes:path_to_mimes/2, default}}
|
{mimetypes, [
|
||||||
|
{<<".html">>, [<<"text/html">>]},
|
||||||
|
{<<".txt">>, [<<"text/plain">>]},
|
||||||
|
{<<".mp4">>, [<<"video/mp4">>]},
|
||||||
|
{<<".ogv">>, [<<"video/ogg">>]}
|
||||||
|
]}
|
||||||
]}
|
]}
|
||||||
]}
|
]}
|
||||||
]),
|
]),
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
#!/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