Skip to content
This repository was archived by the owner on Jul 19, 2019. It is now read-only.

Commit 579806f

Browse files
author
Matt Branton
committed
Erlang web server support
1 parent 66ccecd commit 579806f

File tree

7 files changed

+159
-0
lines changed

7 files changed

+159
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*~
22
node_modules
33
.DS_Store
4+
deps
5+
.rebar

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ go run server.go
5252
go get github.com/xyproto/algernon
5353
# or brew install algernon
5454
algernon server.lua
55+
56+
```
57+
58+
### Erlang
59+
60+
```sh
61+
rebar get-deps
62+
rebar compile
63+
rebar shell
64+
application:ensure_all_started(server).
5565
```
5666

5767
And visit <http://localhost:3000/>. Try opening multiple tabs!

rebar.config

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{sub_dirs, ["apps/", "rel"]}.
2+
3+
{deps_dir, ["deps"]}.
4+
{erl_opts, [debug_info, fail_on_warning]}.
5+
6+
{deps, [{cowboy, "1.0.3", {git, "https://github.com/ninenines/cowboy.git", {tag, "1.0.3"}}},
7+
{jiffy, ".*", {git, "git://github.com/davisp/jiffy.git"}}]}.

src/comment_handler.erl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
%% This file provided by Facebook is for non-commercial testing and evaluation
2+
%% purposes only. Facebook reserves all rights not expressly granted.
3+
%%
4+
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
%% FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
%% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
9+
%% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
%%
11+
12+
%% @doc POST echo handler.
13+
-module(comment_handler).
14+
15+
-define(FilePath, "./comments.json").
16+
17+
-export([init/3]).
18+
-export([handle/2]).
19+
-export([terminate/3]).
20+
21+
init(_Transport, Req, []) ->
22+
{ok, Req, undefined}.
23+
24+
handle(Req, State) ->
25+
{Method, Req2} = cowboy_req:method(Req),
26+
HasBody = cowboy_req:has_body(Req2),
27+
{ok, Req3} = maybe_comment(Method, HasBody, Req2),
28+
{ok, Req3, State}.
29+
30+
maybe_comment(<<"POST">>, true, Req) ->
31+
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
32+
comment(write_comments(read_comments(), PostVals), Req2);
33+
maybe_comment(<<"POST">>, false, Req) ->
34+
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
35+
maybe_comment(<<"GET">>, _, Req) ->
36+
comment(read_raw_comments(), Req);
37+
maybe_comment(_, _, Req) ->
38+
%% Method not allowed.
39+
cowboy_req:reply(405, Req).
40+
41+
comment(Comments, Req) ->
42+
%% always return json
43+
cowboy_req:reply(200, [
44+
{<<"content-type">>, <<"application/json; charset=utf-8">>},
45+
{<<"cache-control">>, <<"no-cache">>}
46+
], Comments, Req).
47+
48+
terminate(_Reason, _Req, _State) ->
49+
ok.
50+
51+
%% Private functions
52+
53+
read_raw_comments() ->
54+
{ok, F} = file:read_file(?FilePath),
55+
F.
56+
57+
read_comments() ->
58+
jiffy:decode(read_raw_comments()).
59+
60+
write_comments(Comments, NextComment) ->
61+
C = jiffy:encode(Comments ++ [{NextComment}],
62+
[force_utf8, pretty]),
63+
ok = file:write_file(?FilePath, C),
64+
C.

src/server.app.src

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{application, server,
2+
[
3+
{description, "React tutorial server"},
4+
{vsn, "1"},
5+
{registered, [server]},
6+
{applications, [
7+
kernel,
8+
stdlib,
9+
crypto,
10+
cowboy,
11+
jiffy]},
12+
{mod, { server_app, []}},
13+
{env, []}
14+
]}.

src/server_app.erl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
%% This file provided by Facebook is for non-commercial testing and evaluation
2+
%% purposes only. Facebook reserves all rights not expressly granted.
3+
%%
4+
%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5+
%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6+
%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
7+
%% FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
8+
%% ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
9+
%% WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
10+
%%
11+
%%
12+
%% @private
13+
-module(server_app).
14+
-behaviour(application).
15+
16+
-export([start/2]).
17+
-export([stop/1]).
18+
19+
start(_Type, _Args) ->
20+
Port = default_port(os:getenv("PORT")),
21+
Dispatch = cowboy_router:compile([
22+
{'_', [
23+
{"/api/comments", comment_handler, []},
24+
{"/", cowboy_static, {file, "./public/index.html"}},
25+
{"/[...]", cowboy_static, {dir, "./public",
26+
[{mimetypes, cow_mimetypes, all}]}}
27+
]}
28+
]),
29+
{ok, _} = cowboy:start_http(http, 10, [{port, Port}], [
30+
{env, [{dispatch, Dispatch}]}
31+
]),
32+
server_sup:start_link().
33+
34+
stop(_State) ->
35+
ok.
36+
37+
38+
%% Utility
39+
40+
default_port(false) -> 3000;
41+
default_port(N) -> N.

src/server_sup.erl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
%% @private
2+
-module(server_sup).
3+
-behaviour(supervisor).
4+
5+
%% API.
6+
-export([start_link/0]).
7+
8+
%% supervisor.
9+
-export([init/1]).
10+
11+
%% API.
12+
13+
-spec start_link() -> {ok, pid()}.
14+
start_link() ->
15+
supervisor:start_link({local, ?MODULE}, ?MODULE, []).
16+
17+
%% supervisor.
18+
19+
init([]) ->
20+
Procs = [],
21+
{ok, {{one_for_one, 10, 10}, Procs}}.

0 commit comments

Comments
 (0)