Skip to content
Browse files

adds configuration options

  • Loading branch information...
1 parent b8a90f9 commit 6169ca4c2f4c28b5449b3079fa6b32139cfc5598 @videlalvaro committed
Showing with 33 additions and 10 deletions.
  1. +0 −1 .gitignore
  2. +15 −2 README.md
  3. +16 −5 src/rabbitmq_chat_rest.erl
  4. +1 −1 src/rabbitmq_chat_sup.erl
  5. +1 −1 start-dev.sh
View
1 .gitignore
@@ -3,5 +3,4 @@ deps/
erl_crash.dump
log/sasl-error.log
priv/chat.config
-priv/chat_dev.config
priv/logs
View
17 README.md
@@ -35,13 +35,26 @@ Compile the source code:
- Start RabbitMQ if is not running already.
-- Start the server:
+### Running in development mode ###
+Edit the file `./priv/chat_dev.config` to suit your RabbitMQ configuraion.
+
+Start the server:
$ ./start-dev.sh
Point your browser to [http://localhost:8080/](http://localhost:8080/). Invite your friends. Enjoy!
+### Running in production mode ###
+
+Create a file called `./priv/chat.config` with the proper values to suit your RabbitMQ configuraion.
+
+See the file `./priv/chat_dev.config` for an example of the configuraion format.
+
+Start the server:
+
+ $ ./start.sh
+
## Credits ##
Inspired by [YakRiak](https://github.com/seancribbs/yakriak)
@@ -50,4 +63,4 @@ Original design adapted from __YakRiak__. Thanks [Basho](http://basho.com/) and
## License ##
-See LICENSE.md
+See LICENSE.md
View
21 src/rabbitmq_chat_rest.erl
@@ -4,13 +4,21 @@
-include("amqp_client.hrl").
-record(http_state, {req}).
--record(websocket_state, {ws, conn, exchange, chann, consumer}).
+-record(websocket_state, {ws, conn, exchange, chann, consumer, user_nb=0}).
-define(EXCHANGE_NAME, <<"chat_room">>).
%% start misultin http server
start_link(Port) ->
- {ok, Conn} = amqp_connection:start(network, #amqp_params{}),
+ {ok, User} = application:get_env(rabbitmq_chat, rabbit_user),
+ {ok, Pass} = application:get_env(rabbitmq_chat, rabbit_pass),
+ {ok, Vhost} = application:get_env(rabbitmq_chat, rabbit_vhost),
+ {ok, Host} = application:get_env(rabbitmq_chat, rabbit_host),
+ {ok, RabbitPort} = application:get_env(rabbitmq_chat, rabbit_port),
+ ConnParams = #amqp_params{username = User, password=Pass,
+ virtual_host = Vhost, host = Host,
+ port = RabbitPort},
+ {ok, Conn} = amqp_connection:start(network, ConnParams),
{ok, Chann} = amqp_connection:open_channel(Conn),
amqp_channel:call(Chann,
#'exchange.declare'{ exchange = ?EXCHANGE_NAME,
@@ -49,10 +57,13 @@ handle('GET', ["js", FileName], #http_state{req=Req}) ->
Req:file(filename:join("./priv/www/js/", FileName), [{"Content-Type", "text/javascript"}]);
handle('GET', ["css", FileName], #http_state{req=Req}) ->
- Req:file(filename:join("./priv/www/css/", FileName), [{"Content-Type", "text/css"}]).
+ Req:file(filename:join("./priv/www/css/", FileName), [{"Content-Type", "text/css"}]);
+
+handle(_, _, #http_state{req=Req}) ->
+ Req:ok([{"Content-Type", "text/plain"}], "Page not found.").
%% callback on received websockets data
-handle_websocket(#websocket_state{ws=Ws, conn=Conn, exchange=Exchange} = State) ->
+handle_websocket(#websocket_state{ws=Ws, conn=Conn, exchange=Exchange, user_nb=_Unb} = State) ->
Chann = get_chann(State#websocket_state.chann, Conn),
Consumer = maybe_start_consumer(State#websocket_state.consumer, [Chann, Exchange, self()]),
State2 = State#websocket_state{chann=Chann, consumer=Consumer},
@@ -84,4 +95,4 @@ maybe_start_consumer(undefined, Args) ->
{ok, Pid} = rabbitmq_chat_consumer:start(Args),
Pid;
maybe_start_consumer(Consumer, _) when is_pid(Consumer) ->
- Consumer.
+ Consumer.
View
2 src/rabbitmq_chat_sup.erl
@@ -20,7 +20,7 @@ start_link() ->
%% ===================================================================
init([]) ->
- {ok, Port} = application:get_env(rabbitmq_chat, port),
+ {ok, Port} = application:get_env(rabbitmq_chat, http_port),
Rest = {rabbitmq_chat_rest, {rabbitmq_chat_rest, start_link, [Port]}, permanent, 5000, worker, [rabbitmq_chat_rest]},
{ok, { {one_for_one, 5, 10}, [Rest]} }.
View
2 start-dev.sh
@@ -1,4 +1,4 @@
#!/bin/sh
cd `dirname $0`
exec erl -sname rmqchat -pa $PWD/ebin $PWD/deps/*/ebin \
--boot start_sasl -sasl_dev ./priv/chat -s rabbitmq_chat
+-s rabbitmq_chat -config ./priv/chat_dev -boot start_sasl -config ./priv/sasl_dev

0 comments on commit 6169ca4

Please sign in to comment.
Something went wrong with that request. Please try again.