diff --git a/lib/kafee/consumer.ex b/lib/kafee/consumer.ex index cac94a9..594b085 100644 --- a/lib/kafee/consumer.ex +++ b/lib/kafee/consumer.ex @@ -1,5 +1,16 @@ defmodule Kafee.Consumer do @options_schema NimbleOptions.new!( + otp_app: [ + doc: """ + The name of the OTP application to read configuration from. + + If the option is set, the configuration will be read from the application environment under the + given name. For example, if the OTP application is `:my_app`, the configuration will be read from + `Application.get_env(:my_app, Myapp.KafeeConsumer)`. + """, + required: true, + type: :atom + ], adapter: [ default: nil, doc: """ @@ -197,7 +208,13 @@ defmodule Kafee.Consumer do @doc false @spec child_spec(Kafee.Consumer.options()) :: Supervisor.child_spec() def child_spec(args) do - full_opts = Keyword.merge(unquote(Macro.escape(opts)), args) + opts = unquote(Macro.escape(opts)) + env_opts = Application.get_env(opts[:otp_app], unquote(__MODULE__), []) + + full_opts = + opts + |> Keyword.merge(env_opts) + |> Keyword.merge(args) %{ id: __MODULE__, diff --git a/lib/kafee/producer.ex b/lib/kafee/producer.ex index bfac161..bb53484 100644 --- a/lib/kafee/producer.ex +++ b/lib/kafee/producer.ex @@ -1,5 +1,16 @@ defmodule Kafee.Producer do @options_schema NimbleOptions.new!( + otp_app: [ + doc: """ + The name of the OTP application to read configuration from. + + If the option is set, the configuration will be read from the application environment under the + given name. For example, if the OTP application is `:my_app`, the configuration will be read from + `Application.get_env(:my_app, Myapp.KafeeProducer)`. + """, + required: true, + type: :atom + ], adapter: [ default: nil, doc: """ @@ -213,7 +224,13 @@ defmodule Kafee.Producer do @doc false @spec child_spec(Kafee.Producer.options()) :: Supervisor.child_spec() def child_spec(args) do - full_opts = Keyword.merge(unquote(Macro.escape(opts)), args) + opts = unquote(Macro.escape(opts)) + env_opts = Application.get_env(opts[:otp_app], unquote(__MODULE__), []) + + full_opts = + opts + |> Keyword.merge(env_opts) + |> Keyword.merge(args) %{ id: __MODULE__, diff --git a/test/kafee/consumer/broadway_adapter_integration_test.exs b/test/kafee/consumer/broadway_adapter_integration_test.exs index 624636a..d6a02ea 100644 --- a/test/kafee/consumer/broadway_adapter_integration_test.exs +++ b/test/kafee/consumer/broadway_adapter_integration_test.exs @@ -3,6 +3,7 @@ defmodule Kafee.Consumer.BroadwayAdapterIntegrationTest do defmodule MyConsumer do use Kafee.Consumer, + otp_app: :kafee, adapter: {Kafee.Consumer.BroadwayAdapter, []} def handle_message(%Kafee.Consumer.Message{} = message) do diff --git a/test/kafee/consumer/brod_adapter_integration_test.exs b/test/kafee/consumer/brod_adapter_integration_test.exs index 7245388..ec5b93e 100644 --- a/test/kafee/consumer/brod_adapter_integration_test.exs +++ b/test/kafee/consumer/brod_adapter_integration_test.exs @@ -3,6 +3,7 @@ defmodule Kafee.Consumer.BrodAdapterIntegrationTest do defmodule MyConsumer do use Kafee.Consumer, + otp_app: :kafee, adapter: Kafee.Consumer.BrodAdapter def handle_message(%Kafee.Consumer.Message{} = message) do diff --git a/test/support/example/my_consumer.ex b/test/support/example/my_consumer.ex index a3dfa77..f317c8f 100644 --- a/test/support/example/my_consumer.ex +++ b/test/support/example/my_consumer.ex @@ -4,6 +4,7 @@ defmodule MyConsumer do """ use Kafee.Consumer, + otp_app: :kafee, adapter: nil, host: "localhost", consumer_group_id: "test", diff --git a/test/support/example/my_producer.ex b/test/support/example/my_producer.ex index 0db94fb..f23557b 100644 --- a/test/support/example/my_producer.ex +++ b/test/support/example/my_producer.ex @@ -4,6 +4,7 @@ defmodule MyProducer do """ use Kafee.Producer, + otp_app: :kafee, adapter: nil, topic: "test-topic", partition_fun: :random