]> review.fuel-infra Code Review - packages/trusty/rabbitmq-server.git/blob
087374b52250412d035403be81f1c7a38e37d3e6
[packages/trusty/rabbitmq-server.git] /
1 #!/usr/bin/env escript
2 %%! -smp disable +A1 +K true -pa ebin deps/cowboy/ebin -input
3 -module(cowboy_multiplex).
4 -mode(compile).
5
6 -export([main/1]).
7
8 %% Cowboy callbacks
9 -export([init/3, handle/2, terminate/2]).
10
11 main(_) ->
12     Port = 8081,
13     application:start(sockjs),
14     application:start(cowboy),
15
16     MultiplexState = sockjs_multiplex:init_state(
17                        [{"ann",  fun service_ann/3,  []},
18                         {"bob",  fun service_bob/3,  []},
19                         {"carl", fun service_carl/3, []}]),
20
21     SockjsState = sockjs_handler:init_state(
22                     <<"/multiplex">>, sockjs_multiplex, MultiplexState, []),
23
24     VhostRoutes = [{[<<"multiplex">>, '...'], sockjs_cowboy_handler, SockjsState},
25                    {'_', ?MODULE, []}],
26     Routes = [{'_',  VhostRoutes}], % any vhost
27
28     io:format(" [*] Running at http://localhost:~p~n", [Port]),
29     cowboy:start_listener(http, 100,
30                           cowboy_tcp_transport, [{port,     Port}],
31                           cowboy_http_protocol, [{dispatch, Routes}]),
32     receive
33         _ -> ok
34     end.
35
36 %% --------------------------------------------------------------------------
37
38 init({_Any, http}, Req, []) ->
39     {ok, Req, []}.
40
41 handle(Req, State) ->
42     {Path, Req1} = cowboy_http_req:path(Req),
43     {ok, Req2} = case Path of
44                      [<<"multiplex.js">>] ->
45                          {ok, Data} = file:read_file("./examples/multiplex/multiplex.js"),
46                          cowboy_http_req:reply(200, [{<<"Content-Type">>, "application/javascript"}],
47                                                Data, Req1);
48                      [] ->
49                          {ok, Data} = file:read_file("./examples/multiplex/index.html"),
50                          cowboy_http_req:reply(200, [{<<"Content-Type">>, "text/html"}],
51                                                Data, Req1);
52                      _ ->
53                          cowboy_http_req:reply(404, [],
54                                                <<"404 - Nothing here\n">>, Req1)
55                  end,
56     {ok, Req2, State}.
57
58 terminate(_Req, _State) ->
59     ok.
60
61 %% --------------------------------------------------------------------------
62
63 service_ann(Conn, init, State) ->
64     Conn:send("Ann says hi!"),
65     {ok, State};
66 service_ann(Conn, {recv, Data}, State) ->
67     Conn:send(["Ann nods: ", Data]),
68     {ok, State};
69 service_ann(_Conn, closed, State) ->
70     {ok, State}.
71
72 service_bob(Conn, init, State) ->
73     Conn:send("Bob doesn't agree."),
74     {ok, State};
75 service_bob(Conn, {recv, Data}, State) ->
76     Conn:send(["Bob says no to: ", Data]),
77     {ok, State};
78 service_bob(_Conn, closed, State) ->
79     {ok, State}.
80
81 service_carl(Conn, init, State) ->
82     Conn:send("Carl says goodbye!"),
83     Conn:close(),
84     {ok, State};
85 service_carl(_Conn, _, State) ->
86     {ok, State}.