lucacorti/lapin

AMQP URI in configuration

lswith opened this issue · 5 comments

We only have the ability to set the amqp url in the configuration. This means that we can't specify the host, port, virtual_host, username and password but just a simple uri. It would be nice to be able to set a uri as part of configuration.

I've also opened pma/amqp#77 to make it easy to pass in the full configuration along with a uri.

Sure, this would be nice and should be already supported by our pma/amqp dependency.

I'll take a look soon unless you want to give it a try and submit a PR.

I found a workaround for this btw. You can do some pretty crazy stuff in the config.exs.

%URI{host: host, path: vhost, port: port, userinfo: userinfo} =
  URI.parse(System.get_env("AMQP_URL"))

[user, pass] = String.split(userinfo, ":", parts: 2)
vhost = String.replace_leading(vhost, "/", "")

vhost =
  case vhost do
    "" -> "/"
    _ -> vhost
  end

config :lapin, :connections, [
  [
    module: consumer,
    host: host,
    port: port || 5672,
    virtual_host: vhost,
    username: user,
    password: pass,
    channels: [
...

Sure config.exs is Elixir, but I guess we could move URI parsing to Lapin.Connection.cleanup_configuration to populate the host/port/virtual_host/username/password keys if absent.

It seems that the uri stuff has almost been solved but the vhost parsing is incorrect. It doesn't remove the leading "/".

Should be fixed in 0.3.0