Liking cljdoc? Tell your friends :D

stavka

Build Status Clojars Cljdoc License Donate

Stavka (Ставка) is the high command of your clojure application, which manages configuration from various sources.

Features

  • Extensible configuration sources and formats
    • Sources:
      • Classpath (classpath)
      • File system (file)
      • URL (url)
      • JDBC (see examples)
    • Formats:
      • Environment variables (env)
      • JVM options (-D) (options)
      • EDN (edn)
      • JSON (json)
      • YAML (yaml)
      • Properties (property)
  • Reloading by
    • Watching file system (watch)
    • Polling the source (poll)
  • Listeners for value changing (on-change!)
  • Type conversion ($l) ($f) ($b) ($s)

Usage

Setup

Use stavka with component or mount. You can have multiple config instance and manage life-cycle of updater.

(require '[stavka.core :as sta :refer :all])

;; Use stavka with mount
(defstate config
    :start
    ;; load configuration from multiple sources and merge them like
    ;; clojure.core/merge.
    (sta/using
        ;; using environment variables by default
        (env)
        ;; also load properties from classpath
        (edn (classpath "/default.edn"))
        ;; load another properties from filesystem, and watch is for change
        (properties (watch (file "/etc/stavka.properties")))
        ;; and fetch a remote json configuration. check every 10 seconds
        ;; for update.
        (json (poll (url "http://somehost/configuration/my.json") 10000)))

    :stop (stop-updaters! config))

;; Use stavka with component
(defrecord StavkaConfiguration [config]
    component/Lifecycle
    (start [component]
        (assoc component :config
            (sta/using
                (env)
                (edn (classpath "/default.edn"))
                (properties (watch (file "/etc/stavka.properties")))
                (json (poll (url "http://somehost/configuration/my.json") 10000)))))
    (stop [component]
        (stop-updaters! config)
        (assoc component :config nil)))

Configuration file format:

EDN

{:some {:config {:key "some-value"}}}

JSON

{
  "some": {
    "config": {
      "key" : "some-value"
    }
  }
}

Properties

some.config.key=some-value

Yaml

some:
  config:
    key: some-value

Get configuration item:

;; get configuration
($ config :some.config.key)

;; get configuration with type convertion
;; $l: as long
;; $f: as double
;; $s: as string
;; $b: as boolean
($l config :some.config.key)

Global config

And you can still use stavka globally:

(sta/global!
    (env)
    (edn (classpath "/default.edn"))
    (properties (watch (file "/etc/stavka.properties")))
    (json (poll (url "http://somehost/configuration/my.json") 10000)))

($l :some.config.key)

Listeners

Add change listener on some key when you have updater configured:

(on-change! config :some.config.key
    (fn [new-value previous-value]
        ))

License

Copyright © 2018 Ning Sun

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

Donation

I'm now accepting donation on liberapay, if you find my work helpful and want to keep it going.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close