@@ -23,6 +23,8 @@ writing web applications. This documentation will take you through
2323Duct's setup and operation, using a web application as an example
2424project.
2525
26+ WARNING: This documentation assumes knowledge of Clojure.
27+
2628== Setup
2729
2830This section will cover setting up a new Duct project. You'll first need
@@ -113,6 +115,8 @@ implement a minimal '`Hello World`' application. While it may be
113115tempting to skip ahead, a sound understanding of how to use Duct will
114116make later sections easier to follow.
115117
118+ === Hello World
119+
116120As mentioned previously, Duct uses a file, `duct.edn`, to define the
117121structure of your application. We'll begin by adding a new component
118122key to the system:
@@ -164,3 +168,105 @@ Hello World
164168----
165169
166170Congratulations on your first Duct application!
171+
172+ === Component Options
173+
174+ One advantage of having a configuration file is that we can customize
175+ the behavior of components. Let's change our `hello` function to take
176+ a `:name` option.
177+
178+ [,clojure]
179+ ----
180+ (ns tutorial.print)
181+
182+ (defn hello [{:keys [name]}]
183+ (println "Hello" name))
184+ ----
185+
186+ Now that `hello` expects an option, we'll need to add it to the
187+ `duct.edn` file.
188+
189+ [,clojure]
190+ ----
191+ {:system
192+ {:tutorial.print/hello {:name "World"}}}
193+ ----
194+
195+ Naturally this produces the same result as before when we run the
196+ application.
197+
198+ [,shell]
199+ ----
200+ $ duct --main
201+ ✓ Initiating system...
202+ Hello World
203+ ----
204+
205+ === Variables
206+
207+ Sometimes we want to supply options from an external source, such as an
208+ environment variable or command line option. Duct allows variables, or
209+ *vars*, to be defined in the `duct.edn` configuration.
210+
211+ Let's add a var to our configuration file.
212+
213+ [,clojure]
214+ ----
215+ {:vars
216+ {name {:arg name, :env NAME, :type :str, :default "World"
217+ :doc "The name of the person to greet"}}
218+ :system
219+ {:tutorial.print/hello {:name #ig/var name}}}
220+ ----
221+
222+ This defines a var called `name` with two sources. In order of priority:
223+
224+ . A command-line argument `--name` (set via `:arg`)
225+ . An environment variable `$NAME` (set via `:env`)
226+
227+ This value can be inserted into the system map with the `#ig/var` data
228+ reader. If the variable has no value, an error will be raised, so it's a
229+ good idea to set a default value using the `:default` key.
230+
231+ NOTE: The '`ig`' in `#ig/var` stands for _Integrant_. This is the
232+ library that Duct relies on to turn configurations into running
233+ applications.
234+
235+ The `:type` of a var determines the data type it should be coerced into.
236+ Duct supports three types natively: `:str`, `:int` and `:float`. The
237+ default type when the key is omitted is `:str`.
238+
239+ Duct integrates these vars into its help message. The `:doc` option
240+ specifies a description of the var.
241+
242+ [,shell,highlight=13]
243+ ----
244+ $ duct --help
245+ Usage:
246+ clojure -M:duct [--main | --repl]
247+ Options:
248+ -c, --cider Start an NREPL server with CIDER middleware
249+ --init Create a blank duct.edn config file
250+ -p, --profiles PROFILES A concatenated list of profile keys
251+ -n, --nrepl Start an NREPL server
252+ -m, --main Start the application
253+ -r, --repl Start a command-line REPL
254+ -s, --show Print out the expanded configuration and exit
255+ -v, --verbose Enable verbose logging
256+ -h, --help Print this help message and exit
257+ --name NAME The name of the person to greet
258+ ----
259+
260+ The var can then be specified at the command line to produce different
261+ results.
262+
263+ [,shell]
264+ ----
265+ $ duct --main --name=Clojurian
266+ ✓ Initiating system...
267+ Hello Clojurian
268+
269+ $ NAME=Clojurist duct --main
270+ ✓ Initiating system...
271+ Hello Clojurist
272+ ----
0 commit comments