Module Sihl.Configuration
Configuration
type ('ctor, 'ty) schema
= (string, 'ctor, 'ty) Conformist.t
type data
= (string * string) list
A list of key-value pairs of strings representing the configuration key like SMTP_HOST and a value.
type config
= Sihl__.Core_configuration.config
=
{
name : string;
description : string;
type_ : string;
default : string option;
}
The configuration contains configuration data and a configuration schema.
type t
= Sihl__.Core_configuration.t
Storing configuration
Configuration might come from various sources like .env files, environment variables or as data provided directly to services or the app.
val store : data -> unit
store data
stores the configurationdata
.
Reading configuration
Using the schema validator conformist it is easy to validate and decode configuration values. Conformist schemas can express a richer set of requirements than static types, which can be used in services to validate configurations at start time.
Validating configuration when starting services can lead to run-time exceptions, but they occur early in the app lifecycle. This minimizes the feedback loop and makes sure, that services start only with valid configuration.
val env_files_path : unit -> string option
val read_env_file : unit -> data option
read_env_file ()
reads an.env
file from the directory given byenv_files_path
and returns the key-value pairs asdata
. IfSIHL_ENV
is set totest
,.env.test
is read. Otherwise.env
is read. If the file doesn't exist or the directory containing the file can't be found,None
is returned.If you just want to access configuration values, use the read functions instead. Every time you call
read_env_file
the file is read from disk.
val load_env_file : unit -> unit
load_env_file ()
reads an.env
file usingread_env_file
and stores its contents into the environment variables.
val load : unit -> unit
load ()
callsload_env_file
and makes sure thatSIHL_ENV
was set.
val read : ('ctor, 'ty) schema -> 'ty
read schema
returns the decoded, statically typed version of configurationt
of theschema
. This is used in services to declaratively define a valid configuration.The configuration data
t
is merged with the environment variable and, if present, an .env file.It fails with
Exception
and prints descriptive message of invalid configuration.
val require : ('ctor, 'ty) schema -> unit
require t
raises an exception if the stored configuration doesn't contain the configurations provided by the list of configurationst
.
val read_string : string -> string option
read_string key
returns the configuration value withkey
if present. The function is memoized, the first call caches the returned value and subsequent calls are fast.
val read_secret : unit -> string
read_secret unit
returns the value of SIHL_SECRET if it is set. If SIHL_SECRET was not set, it fails in production and in testing or local development the value is set to "secret".
val read_int : string -> int option
read_int key
returns the configuration value withkey
if present. the first call caches the returned value and subsequent calls are fast.
val read_bool : string -> bool option
read_bool key
returns the configuration value withkey
if present. the first call caches the returned value and subsequent calls are fast.
val is_test : unit -> bool
is_test ()
returns true ifSIHL_ENV
istest
. This is usually the case when Sihl tests are executed.