A Clojure library for config coercion.
[squeeze "0.3.3"]
The Twelve-Factor App recommends to only use environment variables to pass configuration to applications. That means, no files, no web-services that application calls to receive its config, only reading from the environment.
Process environment is platform-independent and immutable.
This brings several challenges:
This library does not:
All examples assume the following imports:
(ns my-app.core
(:require [squeeze.core :as squeeze]
[environ.core :as env]
[schema.core :as s]))
Simple example:
(def default-config
{:http-port 8090
:http-bind "0.0.0.0"})
(s/defschema Config
{(s/optional-key :http-port) s/Int
(s/optional-key :http-bind) s/Str
(s/optional-key :http-public) s/Bool}
(squeeze/coerce-config Config (merge default-config environ/env))
Given the following process environment:
LC_ALL=en_US.UTF-8
LANG=en_US.UTF-8
HTTP_PORT=4200
TERM=xterm-256color
SHELL=/bin/bash
The example would yield the following:
{:http-port 4200
:http-bind "0.0.0.0"}
Environment variables are perfectly suited for passing big documents in them:
# whitelist.yaml
- 1.1.1.1
- 2.2.2.2
- 3.3.3.3
When run as:
HTTP_IP_WHITELIST=$(cat http.yaml) java -jar ...
Using schema:
(s/defschema HttpConfig
{:http-ip-whitelist [s/Str]})
(squeeze/coerce-config HttpConfig environ/env)
Would yield:
{:http-ip-whitelist[squeeze "0.3.3"]}
If you need to get rid of the name prefix, like :http-
, use remove-key-prefix
:
(squeeze/remove-key-prefix :db- {:db-port 1234})
; => {:port 1234}
In order to remap some keys in a map and only keep the remapped ones, use remap-keys
:
(squeeze/remap-keys {:whitelist :http-ip-whitelist} {:http-ip-whitelist[squeeze "0.3.3"] :http-port 8000})
; => {:whitelist[squeeze "0.3.3"]}
Squeeze relies on Prismatic Schema and clj-yaml.
Copyright © 2017 Dmitrii Balakhonskii
Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close