Add configuration support for AMQP username/password/vhost
authorMatt Mullins <mmullins@mmlx.us>
Thu, 1 Sep 2011 20:33:21 +0000 (15:33 -0500)
committerMatt Mullins <mmullins@mmlx.us>
Thu, 1 Sep 2011 20:33:21 +0000 (15:33 -0500)
amqp/amqp_bot_connection.erl

index e0744eb..a851e8c 100644 (file)
@@ -34,6 +34,25 @@ handle_call(get_connection, _From, Connection) ->
     {reply, Connection, Connection}.
 
 start_link_connection() ->
-    {ok, Pid} = amqp_connection:start(#amqp_params_network{}),
+    Username = get_config(username, "guest"),
+    Password = get_config(password, "guest"),
+    VHost = get_config(vhost, "/"),
+    {ok, Pid} = amqp_connection:start(
+                    #amqp_params_network{username = Username,
+                                         password = Password,
+                                         virtual_host = VHost
+                                        }),
     link(Pid),
     {ok, Pid}.
+
+get_config(Key, Default) ->
+    DataList = case config:get_config(amqp) of
+        [Config] ->
+            case lists:keyfind(Key, 1, Config) of
+                {Key, Data} -> Data;
+                false -> Default
+            end;
+        [] ->
+            Default
+    end,
+    list_to_binary(DataList).