RefInspector v1.0.0 RefInspector.Config View Source

Module to simplify access to configuration values with default values.

There should be no configuration required to start using :ref_inspector if you rely on the default values:

config :ref_inspector,
  database_files: ["referers.yml"],
  database_path: Application.app_dir(:ref_inspector, "priv"),
  ets_cleanup_delay: 30_000,
  http_opts: [],
  remote_urls: [{"referers.yml", "https://s3-eu-west-1.amazonaws.com/snowplow-hosted-assets/third-party/referer-parser/referers-latest.yml"}]

The default :database_path is evaluated at runtime and not compiled into a release!

How to Configure

There are two ways to change the configuration values with the preferred way depending on your environment and personal taste.

Static Configuration

If you can ensure the configuration are static and not dependent on i.e. the server your application is running on, you can use a static approach by modifying your config.exs file:

config :ref_inspector,
  database_files: ["referers_search.yml", "referers_social.yml"],
  database_path: "/path/to/ref_inspector/database_files"

Dynamic Configuration

If a compile time configuration is not possible or does not match the usual approach taken in your application you can use a runtime approach.

This is done by defining an initializer module that will automatically be called by RefInspector.Supervisor upon startup/restart. The configuration is expected to consist of a {mod, fun} or {mod, fun, args} tuple:

# {mod, fun}
config :ref_inspector,
  init: {MyInitModule, :my_init_mf}

# {mod, fun, args}
config :ref_inspector,
  init: {MyInitModule, :my_init_mfa, [:foo, :bar]}

defmodule MyInitModule do
  @spec my_init_mf() :: :ok
  def my_init_mf(), do: my_init_mfa(:foo, :bar)

  @spec my_init_mfa(atom, atom) :: :ok
  def my_init_mfa(:foo, :bar) do
    priv_dir = Application.app_dir(:my_app, "priv")

    Application.put_env(:ref_inspector, :database_path, priv_dir)
  end
end

The function is required to always return :ok.

Database Configuration

Configuring the database to use can be done using two related values:

  • :database_files
  • :database_path
  • :remote_urls

The :database_path is the directory to look for when loading the databases. It is also the place where RefInspector.Downloader stores a copy of the configured files.

For the actual files loaded there is :database_files, a list of filenames to load in the order specified. All files are expected to be inside the configured database path.

When downloading the databases through RefInspector.Downloader the value :remote_urls is of utmost importance. It defines where each file is located.

config :ref_inspector,
  :remote_urls, [
    "http://example.com/database.yml"
    {"database_local.yml", "http://example.com/database_remote.yml"}
  ]

To configure a remote database name you can either define a plain URL. It will be stored locally under the filename that is extracted from the url. In above example that would be "database.yml".

If the remote and local names match you can configure a {local, remote} tuple to deactivate the automatic name extraction.

Download Configuration

All download request for your database files are done using :hackney. To pass custom configuration values to hackney you can use the key :http_opts:

config :ref_inspector,
  http_opts: [proxy: "http://mycompanyproxy.com"]

Please see :hackney.request/5 for a complete list of available options.

Reload Configuration

When reloading the databases the old values will be deleted in a delayed fashion to avoid false parse results.

You can manually configure this delay using the :ets_cleanup_delay configuration value.

Link to this section Summary

Functions

Returns the list of configured database files

Returns the configured database path

Returns whether the remote database matches the default

Provides access to configuration values with optional environment lookup

Calls the optionally configured init method

Returns the remote urls of the database file

Link to this section Functions

Link to this function database_files() View Source
database_files() :: list()

Returns the list of configured database files.

Link to this function database_path() View Source
database_path() :: String.t()

Returns the configured database path.

If the path is not defined the priv dir of :ref_inspector as returned by Application.app_dir(:ref_inspector, "priv") will be used.

Link to this function default_remote_database?() View Source
default_remote_database?() :: boolean()

Returns whether the remote database matches the default.

Link to this function get(key, default \\ nil) View Source
get(atom(), term()) :: term()

Provides access to configuration values with optional environment lookup.

Link to this function init_env() View Source
init_env() :: :ok

Calls the optionally configured init method.

Link to this function yaml_urls() View Source
yaml_urls() :: [String.t() | {String.t(), String.t()}]

Returns the remote urls of the database file.