Using envvault with Clojure
Note: Please ensure that you have completed the previous steps
Prerequisites
- Java Development Kit (JDK) installed
- Leiningen (Clojure build tool) installed
- envvault CLI tool installed
- An existing Clojure project
Usage
Running Your Application
To run your Clojure application with environment variables from envvault:
$ envvault run --env=dev lein run
Caching Environment Variables
For better performance, you can cache your environment variables:
$ envvault run --env=dev -c -- lein run
Example Implementation
Here is how to set up a basic Clojure web server with envvault:
; src/my_app/core.clj
(ns my-app.core
(:require [ring.adapter.jetty :as jetty]
[compojure.core :refer :all]
[compojure.route :as route]))
(defn get-env [key default]
(or (System/getenv key) default))
; Your environment variables are automatically loaded
(def port (Integer/parseInt (get-env "PORT" "3000")))
(def db-url (get-env "DB_URL" ""))
(defroutes app-routes
(GET "/" [] "Hello from envvault!")
(route/not-found "Not Found"))
(defn -main [& args]
(println (str "Server running at http://localhost:" port "/"))
(jetty/run-jetty app-routes {:port port :join? false}))
; project.clj
(defproject my-app "0.1.0-SNAPSHOT"
:description "My Clojure App"
:dependencies [[org.clojure/clojure "1.11.1"]
[ring/ring-core "1.9.5"]
[ring/ring-jetty-adapter "1.9.5"]
[compojure "1.7.0"]]
:main ^:skip-aot my-app.core
:target-path "target/%s"
:profiles {:uberjar {:aot :all
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]}})