Skip to content

Commit

Permalink
Add erlang server
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jan 30, 2017
1 parent 9099be0 commit 5642f98
Show file tree
Hide file tree
Showing 16 changed files with 368 additions and 0 deletions.
39 changes: 39 additions & 0 deletions erlang/cowboy/.gitignore
@@ -0,0 +1,39 @@
*~
*.DS_Store
*.log
*.cookie
/log
*~

# XCode noise
*.swp
*~.nib
/build
*.pbxuser
*.perspective
*.perspectivev3
*.mode1v3
*.mode2v3

# Sublime
*.sublime-project
*.sublime-workspace

# IDEA files
.idea/*
*.iml
out
# erlang binaries and release

deps
/
ebin/
*.beam
.eunit
*.dump

# other files
files/vm.args

_build/
*.crashdump
2 changes: 2 additions & 0 deletions erlang/cowboy/Changelog.md
@@ -0,0 +1,2 @@
## 0.0.1 [2016-09-11]
- Initial version
20 changes: 20 additions & 0 deletions erlang/cowboy/LICENSE
@@ -0,0 +1,20 @@
The MIT License (MIT)

Copyright (c) 2016 palkan

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16 changes: 16 additions & 0 deletions erlang/cowboy/Readme.md
@@ -0,0 +1,16 @@
WebSocket Shootout Erlang
=====

Simple [Cowboy](https://github.com/ninenines/cowboy) and [DelivErly](https://github.com/deliverly/deliverly-core) server.

## Dependencies

* Erlang 18+

To run the server:

```
./rebar3 shell
```

WebSocket endpoint is available on `ws://localhost:3334/ws/cable`.
19 changes: 19 additions & 0 deletions erlang/cowboy/files/app.config
@@ -0,0 +1,19 @@
[
{sasl, [{sasl_error_logger, false}]},
{lager, [
{handlers, [
{lager_console_backend, error}
]},
{crash_log, "log/crash.log"},
{crash_log_msg_size, 65536},
{crash_log_size, 10485760},
{crash_log_date, "$D0"},
{crash_log_count, 5},
{error_logger_redirect, true}
]},
{
deliverly, [
{http_port, 3334}
]
}
].
16 changes: 16 additions & 0 deletions erlang/cowboy/include/log.hrl
@@ -0,0 +1,16 @@
%% Copyright
-author("palkan").

-ifdef(TEST).

-define(D(X), lager:info("[DEBUG] ~p:~p ~p~n",[?MODULE, ?LINE, X])).
-define(I(X), lager:info("[INFO] ~p:~p ~p~n",[?MODULE, ?LINE, X])).
-define(E(X), lager:info("[ERROR] ~p:~p ~p~n",[?MODULE, ?LINE, X])).

-else.

-define(D(X), lager:debug("~p:~p ~p",[?MODULE, ?LINE, X])).
-define(I(X), lager:info("~p:~p ~p",[?MODULE, ?LINE, X])).
-define(E(X), lager:error("~p:~p ~p",[?MODULE, ?LINE, X])).

-endif.
3 changes: 3 additions & 0 deletions erlang/cowboy/include/priv.hrl
@@ -0,0 +1,3 @@
-author(palkan).
-define(APP, ws_shootout).
-define(Config(X,Y),ulitos_app:get_var(?APP,X,Y)).
44 changes: 44 additions & 0 deletions erlang/cowboy/rebar.config
@@ -0,0 +1,44 @@
%% -*- mode: Erlang; -*-
{require_otp_vsn, "18"}.
{deps, [
{deliverly, {git, "https://github.com/deliverly/deliverly-core.git", {tag, "0.0.6"}}},
lager
]}.

{profiles, [
{prod, [
{erl_opts, [no_debug_info, warn_unused_vars, warnings_as_errors]},
{relx, [{dev_mode, false}, {include_erts, true}]}
]},
{test, [
{deps, [meck, eunit_formatters]},
{erl_opts, [debug_info]}
]}
]}.

{relx, [
{release, { ws_shootout, "0.0.1"}, [ws_shootout]},
{dev_mode, true},
{include_erts, false},
{extended_start_script, true},
{sys_config, "files/app.config"}
]}.

{erl_opts, [debug_info, {i, "apps"}, {parse_transform, lager_transform}]}.

{cover_enabled, true}.

{eunit_opts, [
no_tty,
{report, {eunit_progress, [colored, profile]}}
]}.

{shell, [{config, "files/app.config"}]}.

{clean_files, ["*.eunit", "ebin/*.beam", "specs/*.beam", "specs/logs/*"]}.

{ct_opts, [
{dir, "specs"},
{logdir, "specs/logs"},
{config, "specs/test.config"}
]}.
30 changes: 30 additions & 0 deletions erlang/cowboy/rebar.lock
@@ -0,0 +1,30 @@
{"1.1.0",
[{<<"cowboy">>,
{git,"https://github.com/ninenines/cowboy.git",
{ref,"dbb636034f20736e16eb9d6c809217c9525b6cbd"}},
1},
{<<"cowlib">>,
{git,"https://github.com/ninenines/cowlib",
{ref,"0e7abe0b24593f131add272c275406d8ed231805"}},
2},
{<<"deliverly">>,
{git,"https://github.com/deliverly/deliverly-core.git",
{ref,"da7a7f647c74b28ae7b8c8f5b3b270842e419fe0"}},
0},
{<<"eredis">>,{pkg,<<"eredis">>,<<"1.0.8">>},1},
{<<"goldrush">>,{pkg,<<"goldrush">>,<<"0.1.8">>},1},
{<<"jsx">>,{pkg,<<"jsx">>,<<"2.8.0">>},1},
{<<"lager">>,{pkg,<<"lager">>,<<"3.2.1">>},0},
{<<"poolboy">>,{pkg,<<"poolboy">>,<<"1.5.1">>},1},
{<<"ranch">>,{pkg,<<"ranch">>,<<"1.2.1">>},1},
{<<"ulitos">>,{pkg,<<"ulitos">>,<<"0.3.0">>},1}]}.
[
{pkg_hash,[
{<<"eredis">>, <<"AB4FDA1C4BA7FBE6C19C26C249DC13DA916D762502C4B4FA2DF401A8D51C5364">>},
{<<"goldrush">>, <<"2024BA375CEEA47E27EA70E14D2C483B2D8610101B4E852EF7F89163CDB6E649">>},
{<<"jsx">>, <<"749BEC6D205C694AE1786D62CEA6CC45A390437E24835FD16D12D74F07097727">>},
{<<"lager">>, <<"EEF4E18B39E4195D37606D9088EA05BF1B745986CF8EC84F01D332456FE88D17">>},
{<<"poolboy">>, <<"6B46163901CFD0A1B43D692657ED9D7E599853B3B21B95AE5AE0A777CF9B6CA8">>},
{<<"ranch">>, <<"A6FB992C10F2187B46FFD17CE398DDF8A54F691B81768F9EF5F461EA7E28C762">>},
{<<"ulitos">>, <<"B22D60F72A0627F5BBCA2A42A8A140467B954EF950E4716E3BD0E42ABF135102">>}]}
].
Binary file added erlang/cowboy/rebar3
Binary file not shown.
15 changes: 15 additions & 0 deletions erlang/cowboy/specs/test.config
@@ -0,0 +1,15 @@
[
{
lager,
[
{
handlers,
[
{lager_console_backend, error},
{lager_console_backend, debug},
{lager_console_backend, info}
]
}
]
}
].
14 changes: 14 additions & 0 deletions erlang/cowboy/src/ws_shootout.app.src
@@ -0,0 +1,14 @@
{application, ws_shootout,
[
{description, ""},
{vsn, "0.0.1"},
{registered, []},
{applications, [
kernel,
stdlib,
lager,
deliverly
]},
{mod, { ws_shootout_app, []}},
{env, []}
]}.
32 changes: 32 additions & 0 deletions erlang/cowboy/src/ws_shootout.erl
@@ -0,0 +1,32 @@
%% Copyright
-module(ws_shootout).
-author(palkan).
-include_lib("ws_shootout/include/log.hrl").
-include_lib("ws_shootout/include/priv.hrl").
-define(APPS, [lager, deliverly, pg2]).

%% ------------------------------------------------------------------
%% Common Application Function Exports
%% ------------------------------------------------------------------

-export([start/0, stop/0, upgrade/0, ping/0]).

%% ------------------------------------------------------------------
%% API Function Exports
%% ------------------------------------------------------------------

-define(SERVER, ws_shootout_server).

start() ->
ulitos_app:ensure_started(?APPS),
application:start(ws_shootout).

stop() ->
application:stop(ws_shootout).

upgrade() ->
ulitos_app:reload(ws_shootout),
ok.

ping() ->
pong.
20 changes: 20 additions & 0 deletions erlang/cowboy/src/ws_shootout_app.erl
@@ -0,0 +1,20 @@
-module(ws_shootout_app).
-author(palkan).
-include_lib("ws_shootout/include/log.hrl").
-include_lib("ws_shootout/include/priv.hrl").
-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
?I("Starting application ws_shootout on /ws/cable"),
deliverly:register_handler(cable, ws_shootout_server),
ws_shootout_sup:start_link().

stop(_State) ->
ok.
68 changes: 68 additions & 0 deletions erlang/cowboy/src/ws_shootout_server.erl
@@ -0,0 +1,68 @@
-module(ws_shootout_server).
-behaviour(gen_server).
-behaviour(deliverly_handler).
-include_lib("deliverly/include/deliverly.hrl").
-include_lib("ws_shootout/include/log.hrl").
-include_lib("ws_shootout/include/priv.hrl").
-define(SERVER, ?MODULE).

%% API Function Exports
-export([start_link/0]).
%% gen_server Function Exports
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
%%% Deverly Handler callbacks.
-export([authorize/2, handle_message/2, handle_client_message/2, client_disconnected/1]).

start_link() ->
gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).

init([]) ->
{ok, #{}}.

authorize(#de_client{socket = Socket}=Client,_) ->
gen_server:call(?SERVER, {join, Socket}),
{ok, Client}.

handle_message(_,_) -> ok.

handle_client_message(Client, Raw) ->
Message = json_encoder:decode(Client, Raw),
gen_server:call(?SERVER, {handle_client_message, Message, Client, Raw});

handle_client_message(_, _) -> ok.

client_disconnected(#de_client{socket = Socket}=Client) ->
gen_server:call(?SERVER, {leave, Socket}),
ok.

handle_call({handle_client_message, #{ <<"type">> := <<"echo">> }, Client, Raw}, _, State) ->
{reply, {reply, Client, Raw}, State};

handle_call({handle_client_message, #{ <<"type">> := <<"broadcast">> } = Data, Client, Raw}, _, State) ->
self() ! {broadcast, Client, Data, Raw},
{reply, ok, State};

handle_call({join, Socket}, _From, State) ->
{reply, ok, State#{ Socket => 1}};

handle_call({leave, Socket}, _From, State) ->
{reply, ok, maps:remove(Socket, State)}.

handle_cast(_Msg, State) ->
{noreply, State}.

handle_info({broadcast, Client, Data, Raw}, Clients) ->
[Socket ! {handle_message, Raw} || Socket <- maps:keys(Clients)],
Reply = Data#{ <<"type">> => <<"broadcastResult">>},
de_client:send(Client#de_client{encoder=json_encoder}, Reply),
{noreply, Clients};

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.
30 changes: 30 additions & 0 deletions erlang/cowboy/src/ws_shootout_sup.erl
@@ -0,0 +1,30 @@
-module(ws_shootout_sup).
-author(palkan).
-include_lib("ws_shootout/include/priv.hrl").
-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([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([]) ->
Children = [
?CHILD(ws_shootout_server, worker)
],
{ok, {{one_for_one, 5, 10}, Children}}.

0 comments on commit 5642f98

Please sign in to comment.