= Vert.x Config

Vert.x Config provides a way to configure your Vert.x application.

It:

* offers multiple configuration syntaxes (json, properties, yaml (extension), hocon
(extension)...
* offers multiple configuration stores (files, directories, http, git (extension), redis
(extension), system properties, environment properties)...
* lets you define the processing order and overloading
* supports runtime reconfiguration

== Concepts

The library is structured around:

* a **Config Retriever** instantiated and used by the Vert.x application. It
configures a set of configuration store
* **Configuration store** defines a location from where the configuration data is read
and and a syntax (json by default)

The configuration is retrieved as a JSON Object.

== Using the Config Retriever

To use the Config Retriever, add the following dependency to the
_dependencies_ section of your build descriptor:

* Maven (in your `pom.xml`):

[source,xml,subs="+attributes"]
----
<dependency>
  <groupId>io.vertx</groupId>
  <artifactId>vertx-config</artifactId>
  <version>3.5.1</version>
</dependency>
----

* Gradle (in your `build.gradle` file):

[source,groovy,subs="+attributes"]
----
compile 'io.vertx:vertx-config:3.5.1'
----

Once done, you first need to instantiate the `link:../../yardoc/VertxConfig/ConfigRetriever.html[ConfigRetriever]`:

[source]
----
require 'vertx-config/config_retriever'
retriever = VertxConfig::ConfigRetriever.create(vertx)

----

By default the Config Retriever is configured with the following stores (in
this order):

* The Vert.x verticle `config()`
* The system properties
* The environment variables
* A `conf/config.json` file. This path can be overridden using the `vertx-config-path` system property or
`VERTX_CONFIG_PATH` environment variable.

You can configure your own stores:

[source]
----
require 'vertx-config/config_retriever'
httpStore = {
  'type' => "http",
  'config' => {
    'host' => "localhost",
    'port' => 8080,
    'path' => "/conf"
  }
}

fileStore = {
  'type' => "file",
  'config' => {
    'path' => "my-config.json"
  }
}

sysPropsStore = {
  'type' => "sys"
}


options = {
  'stores' => [
    httpStore,
    fileStore,
    sysPropsStore
  ]
}

retriever = VertxConfig::ConfigRetriever.create(vertx, options)

----

More details about the overloading rules and available stores are available below. Each store can be marked as
`optional`. If a failure is caught while retrieving (or processing) the configuration from an optional store, the failure
is logged but the processing does not fail. Instead an empty JSON object is returned (`{}`). To mark a store as
optional, use the `optional` attribute:

[source]
----
require 'vertx-config/config_retriever'
fileStore = {
  'type' => "file",
  'optional' => true,
  'config' => {
    'path' => "my-config.json"
  }
}
sysPropsStore = {
  'type' => "sys"
}

options = {
  'stores' => [
    fileStore,
    sysPropsStore
  ]
}

retriever = VertxConfig::ConfigRetriever.create(vertx, options)

----

Once you have the instance of the Config Retriever, _retrieve_ the configuration
as follows:

[source]
----
retriever.get_config() { |ar_err,ar|
  if (ar_err != nil)
    # Failed to retrieve the configuration
  else
    config = ar
  end
}

----

== Overloading rules

The declaration order of the configuration store is important as it defines the
overloading. For conflicting key, configuration stores arriving _last_ overloads the
value provided by the previous configuration stores. Let's take an example. We have 2
configuration stores:

* `A` provides `{a:value, b:1}`
* `B` provides `{a:value2, c:2}`

Declared in this order (A, B), the resulting configuration would be:
`{a:value2, b:1, c:2}`.

If you declare them in the reverse order (B, A), you would get: * `{a:value, b:1, c:2}`.

== Available configuration stores

The Config Retriever provides a set of configuration store and format.
Some more are available as extension and you can implement your own.

=== Structure of the configuration

Each declared data store must specify the `type`. It can also define the `format`. If
not set JSON is used.

Some configurations tore requires additional configuration (such a path...). This
configuration is passed as a Json Object using `link:../dataobjects.html#ConfigStoreOptions#set_config-instance_method[config]`

=== File

This configuration store just read the configuration from a file. It supports all
supported formats.

[source, ruby]
----
file = {
  'type' => "file",
  'format' => "properties",
  'config' => {
    'path' => "path-to-file.properties"
  }
}

----

The `path` configuration is required.

=== JSON

The JSON configuration store just serves the given JSON config as it is.

[source, ruby]
----
json = {
  'type' => "json",
  'config' => {
    'key' => "value"
  }
}

----

The only supported format for this configuration store is JSON.

=== Environment Variables

This configuration store maps environment variables to a Json Object contributed to
the global configuration.

[source, ruby]
----
json = {
  'type' => "env"
}

----

This configuration store does not support the `format` configuration. By default, the retrieved value are
transformed into JSON compatible structures (number, string, boolean, json object and json array). To avoid this
conversion, configure the `raw-data` attribute:

[source, ruby]
----
json = {
  'type' => "env",
  'config' => {
    'raw-data' => true
  }
}

----

You can configure the `raw-data` attribute (`false` by default). If `raw-data` is `true` no attempts to convert
values will be made and you'll be able to get raw values using `config.getString(key)`.

If you want to select the set of keys to import, use the `keys` attributes. It filters out all non selected keys. Keys
must be listed individually:

[source, ruby]
----
json = {
  'type' => "env",
  'config' => {
    'keys' => [
      "SERVICE1_HOST",
      "SERVICE2_HOST"
    ]
  }
}

----

=== System Properties

This configuration store maps system properties to a Json Object contributed to the
global configuration.

[source, ruby]
----
json = {
  'type' => "sys",
  'config' => {
    'cache' => "false"
  }
}

----

This configuration store does not support the `format` configuration.

You can configure the `cache` attribute (`true` by default) let you decide whether or
not it caches the system properties on the first access and does not reload them.

=== HTTP

This configuration stores retrieves the configuration from a HTTP location. It can use
any supported format.

[source, ruby]
----
http = {
  'type' => "http",
  'config' => {
    'host' => "localhost",
    'port' => 8080,
    'path' => "/A"
  }
}

----

It creates a Vert.x HTTP Client with the store configuration (see next snippet). To
ease the configuration, you can also configure the `host`, `port` and `path` with the
`host`, `port` and `path`
properties.

[source, ruby]
----
http = {
  'type' => "http",
  'config' => {
    'defaultHost' => "localhost",
    'defaultPort' => 8080,
    'ssl' => true,
    'path' => "/A"
  }
}

----

=== Event Bus

This event bus configuration stores receives the configuration from the event bus. This
stores let you distribute your configuration among your local and distributed components.

[source, ruby]
----
eb = {
  'type' => "event-bus",
  'config' => {
    'address' => "address-getting-the-conf"
  }
}

----

This configuration store supports any type of format.

=== Directory

This configuration store is similar to the `file` configuration store, but instead of
reading a single file, read several files from a directory.

This configuration store configuration requires:

* a `path` - the root directory in which files are located
* at least one `fileset` - an object to select the files

Each `fileset` contains:
* a `pattern` : a Ant style pattern to select files. The pattern is applied on the
relative path of the files location in the directory.
* an optional `format` indicating the format of the files (each fileset can use a
different format, BUT files in a fileset must share the same format).

[source, ruby]
----
dir = {
  'type' => "directory",
  'config' => {
    'path' => "config",
    'filesets' => [
      {
        'pattern' => "dir/*json"
      },
      {
        'pattern' => "dir/*.properties",
        'format' => "properties"
      }
    ]
  }
}

----

== Listening for configuration changes

The Configuration Retriever periodically retrieve the configuration and if the outcome
is different from the current one, your application can be reconfigured. By default the
configuration is reloaded every 5 seconds.

[source, ruby]
----
require 'vertx/vertx'
require 'vertx-config/config_retriever'
options = {
  'scanPeriod' => 2000,
  'stores' => [
    store1,
    store2
  ]
}

retriever = VertxConfig::ConfigRetriever.create(Vertx::Vertx.vertx(), options)
retriever.get_config() { |json_err,json|
  # Initial retrieval of the configuration
}

retriever.listen() { |change|
  # Previous configuration
  previous = change['previousConfiguration']
  # New configuration
  conf = change['newConfiguration']
}

----

== Retrieving the last retrieved configuration

You can retrieved the last retrieved configuration without "waiting" to be retrieved
using:

[source, ruby]
----
last = retriever.get_cached_config()

----

== Reading configuration as a stream

The `link:../../yardoc/VertxConfig/ConfigRetriever.html[ConfigRetriever]` provide a way to access the stream of configuration.
It's a `link:../../yardoc/Vertx/ReadStream.html[ReadStream]` of `link:unavailable[JsonObject]`. By registering the right
set of handlers you are notified:

* when a new configuration is retrieved
* when an error occur while retrieving a configuration
* when the configuration retriever is closed (the
`link:../../yardoc/Vertx/ReadStream.html#end_handler-instance_method[endHandler]` is called).

[source, ruby]
----
require 'vertx/vertx'
require 'vertx-config/config_retriever'
options = {
  'scanPeriod' => 2000,
  'stores' => [
    store1,
    store2
  ]
}

retriever = VertxConfig::ConfigRetriever.create(Vertx::Vertx.vertx(), options)
retriever.config_stream().end_handler() { |v|
  # retriever closed
}.exception_handler() { |t|
  # an error has been caught while retrieving the configuration
}.handler() { |conf|
  # the configuration
}


----

== Retrieving the configuration as a Future

The `link:../../yardoc/VertxConfig/ConfigRetriever.html[ConfigRetriever]` provide a way to retrieve the configuration as a
`link:../../yardoc/Vertx/Future.html[Future]`:

[source, ruby]
----
require 'vertx-config/config_retriever'
future = VertxConfig::ConfigRetriever.get_config_as_future(retriever)
future.set_handler() { |ar_err,ar|
  if (ar_err != nil)
    # Failed to retrieve the configuration
  else
    config = ar
  end
}

----

== Extending the Config Retriever

You can extend the configuration by implementing:

* the `link:unavailable[ConfigProcessor]` SPI to add support for a
format
* the `link:unavailable[ConfigStoreFactory]` SPI to add support for
configuration store (place from where the configuration data is retrieved)

== Additional formats

In addition of the out of the box format supported by this library, Vert.x Config provides additional
formats you can use in your application.

include::hocon-format.adoc[]

include::yaml-format.adoc[]

== Additional stores

In addition of the out of the box stores supported by this library, Vert.x Config provides additional
stores you can use in your application.

include::git-store.adoc[]

include::kubernetes-store.adoc[]

include::redis-store.adoc[]

include::zookeeper-store.adoc[]

include::consul-store.adoc[]

include::spring-store.adoc[]

include::vault-store.adoc[]

include::consul-store.adoc[]