@@ -539,7 +539,7 @@ While Duct can be used for any server-side application, its most common
539
539
use-case is developing web applications and services. This section will
540
540
take you through writing a '`todo list`' web application in Duct.
541
541
542
- === Hello World Wide Web
542
+ === Hello World
543
543
544
544
We'll begin by creating a new project directory.
545
545
@@ -549,24 +549,16 @@ mkdir todo-app && cd todo-app
549
549
----
550
550
551
551
The first thing we'll need is a `deps.edn` file that to provide the
552
- project dependencies. This will include Duct main and three additional
553
- modules: logging, sql and web.
554
-
555
- https://www.sqlite.org/index.html[SQLite] will be our database, so we'll
556
- need the SQLite JDBC adapter as well. To give us a Clojure-friendly way
557
- of querying the database, we'll also add
558
- https://github.com/seancorfield/next-jdbc[next.jdbc].
552
+ project dependencies. This will include Duct main and two additional
553
+ modules: logging and web.
559
554
560
555
.deps.edn
561
556
[,clojure]
562
557
----
563
558
{:deps {org.clojure/clojure {:mvn/version "1.12.0"}
564
559
org.duct-framework/main {:mvn/version "0.1.3"}
565
560
org.duct-framework/module.logging {:mvn/version "0.6.5"}
566
- org.duct-framework/module.sql {:mvn/version "0.7.1"}
567
- org.duct-framework/module.web {:mvn/version "0.11.0"}
568
- org.xerial/sqlite-jdbc {:mvn/version "3.47.0.0"}
569
- com.github.seancorfield/next.jdbc {:mvn/version "1.3.955"}}
561
+ org.duct-framework/module.web {:mvn/version "0.11.1"}}
570
562
:aliases {:duct {:main-opts ["-m" "duct.main"]}}}
571
563
----
572
564
@@ -578,106 +570,41 @@ is the default directory Clojure uses to store source files.
578
570
$ mkdir src
579
571
----
580
572
573
+ IMPORTANT: It is especially important to ensure the source directory
574
+ exists before starting a REPL, otherwise the REPL will not be able to
575
+ load source changes.
576
+
581
577
As this is a Duct application, we'll need a `duct.edn` file. This will
582
- contain the three modules we added to the project's dependencies.
578
+ contain the two modules we added to the project's dependencies.
583
579
584
580
.duct.edn
585
581
[,clojure]
586
582
----
587
583
{:system
588
584
{:duct.module/logging {}
589
- :duct.module/sql {}
590
- :duct.module/web {}}}
591
- ----
592
-
593
- Will this minimal configuration work? Let's find out.
594
-
595
- [,shell]
596
- ----
597
- duct --main
598
- ✗ Initiating system...
599
- Execution error (ExceptionInfo) at integrant.core/unbound-vars-exception (core.cljc:343).
600
- Unbound vars: jdbc-url
601
-
602
- Full report at:
603
- /tmp/clojure-14769356862243778775.edn
604
- ----
605
-
606
- Oh dear, it looks like there's a configuration var, `jdbc-url`, that has
607
- not been assigned a value. But where did it come from?
608
-
609
- In this case it came from the `:duct.module/sql` module. Libraries can
610
- add their own vars by using Integrant's keyword annotations. We can see
611
- these new vars if we check the `--help`.
612
-
613
- [,shell]
614
- ----
615
- $ duct --help
616
- Usage:
617
- clojure -M:duct [--main | --repl]
618
- Options:
619
- -c, --cider Start an NREPL server with CIDER middleware
620
- --init Create a blank duct.edn config file
621
- -p, --profiles PROFILES A concatenated list of profile keys
622
- -n, --nrepl Start an NREPL server
623
- -m, --main Start the application
624
- -r, --repl Start a command-line REPL
625
- -s, --show Print out the expanded configuration and exit
626
- -v, --verbose Enable verbose logging
627
- -h, --help Print this help message and exit
628
- --jdbc-url JDBC-URL The JDBC database URL
629
- --port PORT The HTTP server port (default: 3000)
630
- ----
631
-
632
- At the bottom we can see that the modules have added two new options:
633
- `--jdbc-url` and `--port`. The server port has a default value of 3000,
634
- but there's no obvious default for a database URL, at least not one
635
- that will suit every project.
636
-
637
- However, we can add own own default by updating the configuration.
638
-
639
- .duct.edn
640
- [,clojure]
641
- ----
642
- {:vars {jdbc-url {:default "jdbc:sqlite:todo.db"}}
643
- :system
644
- {:duct.module/logging {}
645
- :duct.module/sql {}
646
585
:duct.module/web {}}}
647
586
----
648
587
649
- Now when we run the application we get a more promising result .
588
+ We can now start the application with `--main` .
650
589
651
590
[,shell]
652
591
----
653
592
$ duct --main
654
593
✓ Initiating system...
655
- 2024-11-25T02:51:08.268Z :warn :duct.migrator.ragtime/missing-file {:path "migrations.edn"}
656
594
2024-11-25T02:51:08.279Z :report :duct.server.http.jetty/starting-server {:port 3000}
657
595
----
658
596
659
- The application warns about a missing `migrations.edn` file, but starts
660
- up the web server. The web application should now be up and running at:
597
+ The web application should now be up and running at:
661
598
http://localhost:3000/
662
599
663
600
Visiting that URL will result in a '`404 Not Found`' error page, because
664
601
we have no routes defined. The error page will be in plaintext, because
665
602
we haven't specified what _features_ we want for our web application.
666
603
667
604
We'll fix both these issues, but before we do we should terminate the
668
- application with Ctrl-C and make a `src` directory.
669
-
670
- [,shell]
671
- ----
672
- mkdir -p src/todo
673
- ----
674
-
675
- IMPORTANT: The source directory must be created before running the REPL.
676
- This is because Clojure looks for source files in the Java classpath,
677
- and directories that don't exist yet will be omitted from this.
678
-
679
- Then we'll start a REPL. We'll keep this running while we develop the
680
- application.
605
+ application with Ctrl-C and start a REPL. We'll keep this running while
606
+ we develop the application to avoid costly restarts and to give us a way
607
+ of querying the running system.
681
608
682
609
[,shell]
683
610
----
@@ -702,10 +629,8 @@ route to handle a web request to the root of our application.
702
629
.duct.edn
703
630
[,clojure]
704
631
----
705
- {:vars {jdbc-url {:default "jdbc:sqlite:todo.db"}}
706
- :system
632
+ {:system
707
633
{:duct.module/logging {}
708
- :duct.module/sql {}
709
634
:duct.module/web
710
635
{:features #{:site}
711
636
:routes [["/" {:get :todo.routes/index}]]}}}
0 commit comments