Liking cljdoc? Tell your friends :D

Example app

A minimal but realistic web service wired together with DI. It runs a Jetty server, routes requests via Reitit, queries an H2 database through HikariCP, and applies Flyway migrations on startup.

The source lives in the repository:

What it demonstrates

The example is intentionally small but exercises most of the features covered in the tutorial:

  • System lifecycledi/start / di/stop from user.clj.
  • Components with cleanup{::di/stop ...} metadata on jetty/server (.stop) and hikari/datasource (close-datasource).
  • :component kind — explicit {::di/kind :component} for reitit/handler and flyway/migrate.
  • Plain servicesroot-handler is a regular function that takes its deps as the first argument.
  • Three key shapes side by side:
    • symbols (`hikari/datasource) resolve to vars,
    • keywords (::handler, ::options) represent abstract dependencies that the registry binds,
    • strings ("PORT", "H2_URL") are environment variables.
  • di/env-parsing — registers a :env.long parser so that port :env.long/PORT arrives as a number.
  • di/template — embeds di/ref calls inside data structures (Reitit route data and Hikari options).
  • di/update-key — extends the shared reitit/route-data from example.core so each subsystem can append its own routes without the routing component knowing about them.
  • di/add-side-dependency — pulls in flyway/migrate so migrations run before the server starts, even though nothing references the migrator directly.
  • Registry compositionbase-registry returns a vector of maps and middlewares; dev-registry layers dev-only env values on top.

Running it

From the example/ directory:

clj -M:dev
user=> (start)
;; open http://localhost:8888
user=> (stop)

Redefine example.core/root-handler at the REPL and re-evaluate — the running system picks up the new implementation without a restart, as described in the Intro chapter.

Can you improve this documentation?Edit on GitHub

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close