• Stars
    star
    155
  • Rank 240,864 (Top 5 %)
  • Language
    Clojure
  • Created over 10 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Library for managing environment variables in Clojure using EDN configuration files

yogthos/config

A library for managing configuration using environment variables and EDN configuration files.

The configuration is resolved in the following order, the variables found in later configurations will replace those declared earlier:

  1. config.edn on the classpath
  2. .lein-env file in the project directory
  3. .boot-env file in the project directory
  4. EDN file specified using the config environment variable
  5. Environment variables
  6. Java System properties

The library parses configuration keys into Clojure keywords with names lowercased, then _ and . characters converted to dashes, e.g:

  • foo_bar -> foo-bar
  • Foo_bar -> foo-bar
  • Foo.BAR -> foo-bar

namespaced keys can be declared using __:

  • db__spec -> db/spec

The values are parsed using the following strategy:

  1. [0-9]+ -> number
  2. ^(true|false)$ -> boolean
  3. \w+ -> string
  4. try parse as EDN, and return the original value as the default

following environment variables:

* BOOL=true
* text="true"
* number=15
* quoted-number="12"
* db__spec="jdbc:sqlite:myapp_dev.db"
* edn_string="{:foo :bar :baz [1 2 \"foo\"]}"
* unparsed.text="some text here"

are translated as:

* :bool          true,
* :text          "true",
* :number        15,
* :quoted-number "12",
  :db/spec       "jdbc:sqlite:myapp_dev.db"
* :edn-string    {:foo :bar, :baz [1 2 "foo"]},
* :unparsed-text "some text here"

Installation

Include the following dependency in your project.clj file:

Clojars Project

Usage

external configuration

In most cases you'll likely want to specify the configuration file using the config environment variable. We will add the dependency to our project.clj file and specify the configuration file location using :jvm-opts:

(defproject edn-config-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [yogthos/config <VERSION>]]
  ;; configuration will be read from the dev-config.edn file               
  :jvm-opts ["-Dconfig=dev-config.edn"]               
  :main edn-config-test.core)

embedded configuration

In some cases you may wish to package configuration in the jar along with the application. In this case the config.edn file must be present on the classpath.

Let's take a look at setting up separate configurations for development and production by adding profiles to project.clj. We'll create dev and prod profiles each pointing to a different resource path containing the desired configuration.

First, we'll need to create a config folder in the root of the project. Under the config we will create dev and prod folders. Each of this will contain a file called config.edn.

We'll create a development configuration in config/dev/config.edn:

{:db "jdbc:sqlite:dev.db"}

and a production configuration in config/prod/config.edn:

{:db "jdbc:sqlite:prod.db"}

Next, we will add the dependency and the profiles to our project.clj:

(defproject edn-config-test "0.1.0-SNAPSHOT"
  :description "FIXME: write description"
  :url "http://example.com/FIXME"
  :license {:name "Eclipse Public License"
            :url "http://www.eclipse.org/legal/epl-v10.html"}
  :dependencies [[org.clojure/clojure "1.10.0"]
                 [yogthos/config <VERSION>]]
  :profiles {:prod {:resource-paths ["config/prod"]}
             :dev  {:resource-paths ["config/dev"]}}
  :main edn-config-test.core)

Accessing the configuration

We can now access the config variables the config.edn found under the resource path specified in the profile. There are two ways of doing this. We can load a version of config defined as config.core/env:

(ns edn-config-test.core
  (:require [config.core :refer [env]])
  (:gen-class))

(defn -main []
  (println (:db env)))

The config in env can be reloaded at runtime by calling the reload-env function.

Alternatively, we can call the config.core/load-env explicitly to manage the state of the config in the app. For example, if we use the mount library, we could write the following:

(ns edn-config-test.core
  (:require [mount.core :refer [defstate]]
            [config.core :refer [load-env]])
  (:gen-class))

(defstate env
  :start (load-env))

  (defn -main []
    (mount.core/start)
    (println (:db env)))    

Packaging for release

The application can be packaged using a specific profile by using the Leiningen with-profile option. For example, if we wanted to package with the prod profile then we'd run the following:

lein with-profile prod uberjar

The resulting jar will contain the config found in config/prod/config.edn in case an embedded configuration was used:

java -jar target/edn-config-test.jar
=> jdbc:sqlite:prod.db

Alternatively, we can create a file called custom-config.edn that looks as follows:

{:db "jdbc:sqlite:prod-custom.db"}

Then we can start the app and pass it the config environment variable pointing to the location of the file:

java -Dconfig="custom-config.edn" -jar target/edn-config-test.jar
=> jdbc:sqlite:prod-custom.db

Attributions

The yogthos/config project is based on the environ library.

License

Distributed under the Eclipse Public License, the same as Clojure.

More Repositories

1

Selmer

A fast, Django inspired template system in Clojure.
Clojure
982
star
2

migratus

MIGRATE ALL THE THINGS!
Clojure
642
star
3

markdown-clj

Markdown parser in Clojure
Clojure
540
star
4

clojure-error-message-catalog

a catalog of common Clojure errors and their meaning
448
star
5

memory-hole

Memory Hole is a support issue organizer application
Clojure
261
star
6

mastodon-bot

a bot for mirroring Twitter/Tumblr accounts and RSS feeds on Mastodon
Clojure
192
star
7

json-html

Provide EDN/JSON and get a DOM node with a human representation of the data
Clojure
160
star
8

yuggoth

my blog engine (no longer maintained)
Clojure
147
star
9

graal-web-app-example

example web application using HTTP Kit and Reitit compiled with GraalVM
Clojure
122
star
10

instant-pdf

A reporting service which generates PDFs from JSON encoded text
Clojure
109
star
11

json-to-pdf

A Library for easily generating PDF documents given JSON markup
Clojure
99
star
12

maestro

FSM library for managing workflows
Clojure
90
star
13

clj-rss

a library for generating RSS feeds
Clojure
59
star
14

compojure-template

A Leiningen template for batteries included projects using Compojure.
Clojure
52
star
15

doc-builder

data driven HTML/PDF document builder using Hiccup and EDN
Clojure
48
star
16

clj-tetris

Example Tetris in Clojure
Clojure
44
star
17

cheatsheets

Shell
40
star
18

lein-asset-minifier

Leiningen plugin for CSS/Js asset minifcation
Clojure
40
star
19

cljs-eval-example

Example of using ClojureSript eval with Reagent
Clojure
32
star
20

gif-to-html

converts GIFs to HTML animation
Clojure
28
star
21

migratus-lein

Clojure
27
star
22

asset-minifier

a library to minify CSS/Js resources
Clojure
27
star
23

reagent-example

Clojure
27
star
24

clj-log

structural logging for Clojure
Clojure
24
star
25

graviton

a small game written in ClojureScript and Pixi.js
Clojure
24
star
26

lein-sass

SASS plugin for Leiningen using Sass.js
Clojure
20
star
27

emoji-id

converts a numeric id to emoji because reasons
Clojure
20
star
28

pg-feed-demo

example application for push notifications from PostgreSQL
Clojure
19
star
29

genetic-algorithm-example

Example of a simple GA using Clojure
Clojure
18
star
30

reagent-secretary-example

Clojure
17
star
31

quil-reagent-demo

example of using Quil with Reagent
Clojure
17
star
32

resume

Clojure
17
star
33

reagent-serverside

example of using Hiccup to generate Reagent elements server-side
Clojure
16
star
34

boids

boids example using Quil and PixiJS
Clojure
14
star
35

components-example

example of creating reusable re-frame components
Clojure
14
star
36

swagger-service

swagger-service tutorial using Duct
Clojure
13
star
37

figwheel-library-template

a template for developing ClojureScript libraries using Figwheel
Clojure
13
star
38

clojure-maven-examples

Some examples for building Clojure with the clojure-maven-plugin
Clojure
12
star
39

krueger

federated news
Clojure
12
star
40

reagent-dnd

Reagent wrapper for react-dnd
Clojure
11
star
41

pulsar-example

Clojure
11
star
42

reagent-server-rendering

An example using Nashorn to render Reagent on the server
Clojure
10
star
43

alpha-id

creates a short alphanumeric ID from a numeric ID
Clojure
10
star
44

prag-prog-re-frame-article

source code for the re-frame article
Clojure
10
star
45

ReagentPerf

Reagent version of the VueReactPerf benchmark
Clojure
9
star
46

semantic-ui-react-example

Example of using semantic-ui-react with shadow-cljs
HTML
8
star
47

markov-chains

a simple markov-chain generator example
Clojure
7
star
48

hiccup-template

a minimal Clojure/Script library for Hiccup style templating
Clojure
6
star
49

Space-Invaders

Sample Java 2D game of Space Invaders
Java
6
star
50

semantic-ui-example

an example of using React Semantic UI from Reagent
Clojure
6
star
51

yogthos.net

source for my blog yogthos.net
HTML
5
star
52

liberator-example

Sample project for Liberator
Clojure
5
star
53

particle-constellations

Clojure
4
star
54

escher-mask

example of using Escher.js from Reagent
Clojure
4
star
55

ring-http-cat-status

Ring middleware for serving status images from https://http.cat/
Clojure
4
star
56

kit-workshop

Clojure
4
star
57

reagent-pythagorean-tree

Clojure
4
star
58

clojurescript-error-reporting-example

Sample project illustrating reporting client-side errors to the server with source maps
Clojure
3
star
59

configs

my configs
Shell
3
star
60

selmer-java

Java wrapper for Selmer
Clojure
3
star
61

guestbook-with-auth

Guestbook example for Web Development With Clojure
Clojure
3
star
62

cli-test

CLI example using GraalVM
Clojure
3
star
63

guestbook

Web development with Clojure (2nd edition) - Guestbook application
Clojure
3
star
64

clj-forum

a forum for Clojure related news and discussions
Clojure
3
star
65

oauth-example

Clojure
3
star
66

json-to-pdf-example

example project for the json-to-pdf library
Java
2
star
67

picture-gallery

sample Reagent project
Clojure
2
star
68

buddhabrot

Nakkaya's Buddhabrot Fractal (http://nakkaya.com/2009/10/04/fractals-in-clojure-buddhabrot-fractal/)
Clojure
2
star
69

Lorenz-Attractor

Clojure
2
star
70

repo-tracker

Clojure
2
star
71

hoplon-app

sample Hoplon project using Leiningen and Figwheel
Clojure
2
star
72

Noir-tutorial

the project for my Noir tutorial http://yogthos.net/blog/22-Noir+tutorial+-+part+1
Clojure
2
star
73

luminusdiff

Clojure
1
star
74

guestbook-sente

sample sente app
Clojure
1
star
75

ring-external-assets

Ring middleware to serve static assets from the specified fielsystem location
Clojure
1
star
76

lein-js

updated version of https://github.com/maravillas/lein-js
Clojure
1
star
77

mojs-reagent

Clojure
1
star
78

kit-ruuter-example

Clojure
1
star
79

reagent-talk

example for the Reagent lightning talk
Clojure
1
star
80

undertow-test

Clojure
1
star
81

selmer-test

Clojure
1
star
82

yogthos.github.com

docs
HTML
1
star
83

instant-html-pdf

HTML to PDF generation service
Clojure
1
star
84

quil-lorenz-attractor

Clojure
1
star
85

re-frame-mobile-test

Clojure
1
star
86

rewrite-clj-experiments

Clojure
1
star
87

forms-test

test project for reagent-forms
Clojure
1
star
88

jar-migrations

Clojure
1
star
89

hyperfiler

fork from https://codeberg.org/chowderman/hyperfiler
TypeScript
1
star
90

puppeteer-service

a service for generating PDF documents from HTML
Clojure
1
star
91

luminus-reitit-swagger

Clojure
1
star
92

bb-htmx-examples

Clojure
1
star
93

metaballs

metaballs test in Clojure
Clojure
1
star
94

jipsi

an implementation of the Java Print Service API (JPS) for IPP / CUPS
Java
1
star
95

clojure.jdbc

JDBC library for Clojure
Clojure
1
star
96

http-kit-ws-benchmark

HTTP Kit WebSocket benchmark
Clojure
1
star
97

parity-translator

translates through languages until parity is reached :)
Clojure
1
star
98

ga-image-evolver-test

An experminet in evolving an image using polygons, based on the Clojure-Genetic-Algorithm-Example
Clojure
1
star