• Stars
    star
    338
  • Rank 124,931 (Top 3 %)
  • Language
    Clojure
  • License
    Other
  • Created over 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A little demo web app in Clojure, using Component, Ring, Compojure, Selmer (and a database)

Example Web Application in Clojure

This is a simple web application using Component, Ring, Compojure, and Selmer connected to a local SQLite database.

Clojure beginners often ask for a "complete" example that they can look at to see how these common libraries fit together and for a long time I pointed them at the User Manager example in the Framework One for Clojure repo -- but since I EOL'd that framework and I'd already rewritten the example app to no longer use the framework, it's just confusing to point them there, so this is a self-contained repo containing just that web app example.

A variant using Integrant and Reitit (instead of Component and Compojure), inspired by this example repo, can be found in MichaΓ«l Salihi's repo.

A version of this application that uses the Polylith architecture is also available, on the polylith branch.

Requirements

This example assumes that you have a recent version of the Clojure CLI installed (at least 1.10.3.933), and provides a deps.edn file, and a build.clj file.

Clojure 1.10 (or later) is required. The "model" of this example app uses namespace-qualified keys in hash maps. It uses next.jdbc -- the "next generation" JDBC library for Clojure -- which produces namespace-qualified hash maps from result sets.

Usage

Clone the repo, cd into it, then follow below to Run the Application or Run the application in REPL or Run the tests or Build an Uberjar.

Run the Application

clojure -M -m usermanager.main

It should create a SQLite database (usermanager_db) and populate two tables (department and addressbook) and start a Jetty instance on port 8080.

If that port is in use, start it on a different port. For example, port 8100:

clojure -M -m usermanager.main 8100

Run the Application in REPL

Start REPL

$ clj

Once REPL starts, start the server as an example on port 8888:

user=> (require 'usermanager.main)                             ; load the code
user=> (in-ns 'usermanager.main)                               ; move to the namesapce
usermanager.main=> (def system (new-system 8888))              ; specify port
usermanager.main=> (alter-var-root #'system component/start)   ; start the server

Run the tests with:

clojure -T:build test

You should see something like this:

Running task for: test

Running tests in #{"test"}
2023-01-24 22:31:01.269:INFO::main: Logging initialized @4050ms to org.eclipse.jetty.util.log.StdErrLog

Testing usermanager.model.user-manager-test
Created database and addressbook table!
Populated database with initial data!

Ran 3 tests containing 9 assertions.
0 failures, 0 errors.

This uses the :build alias to load the build.clj file, based on tools.build, and run the test task.

Build an Uberjar

For production deployment, you typically want to build an "uberjar" -- a .jar file that contains Clojure itself and all of the code from your application and its dependencies, so that you can run it with the java -jar command.

The build.clj file -- mentioned above -- contains a ci task that:

  • runs all the tests
  • cleans up the target folder
  • compiles the application (sometimes called "AOT compilation")
  • produces a standalone .jar file
clojure -T:build ci

That should produce the same output as test above, followed by something like:

Copying source...

Compiling usermanager.main...
2023-01-24 22:35:37.922:INFO::main: Logging initialized @2581ms to org.eclipse.jetty.util.log.StdErrLog

Building JAR...

The target folder will be created if it doesn't exist and it will include a classes folder containing all of the compiled Clojure source code from the usermanager application and all of its dependencies including Clojure itself:

$ ls target/classes/
camel_snake_kebab  clout  compojure  instaparse  medley  public  selmer       views
clojure            com    crypto     layouts     next    ring    usermanager

It will also include the standalone .jar file which you can run like this:

java -jar target/usermanager/example-standalone.jar

This should behave the same as the Run the Application example above.

This JAR file can be deployed to any server that have Java installed and run with no other external dependencies or files.

Stuff I Need To Do

  • I might add a datafy/nav example.

License & Copyright

Copyright (c) 2015-2023 Sean Corfield.

Distributed under the Apache Source License 2.0.

More Repositories

1

honeysql

Turn Clojure data structures into SQL
Clojure
1,763
star
2

next-jdbc

A modern low-level Clojure wrapper for JDBC-based access to databases.
Clojure
752
star
3

dot-clojure

My .clojure/deps.edn file
Clojure
615
star
4

deps-new

Create new projects for the Clojure CLI / deps.edn
Clojure
353
star
5

build-clj

Common build tasks abstracted into a library.
Clojure
155
star
6

readme

A testing library that turns your README into executable Clojure tests!
Clojure
142
star
7

vscode-calva-setup

My VS Code / Calva / Portal / Joyride setup
Clojure
93
star
8

om-sente

Playground to create Om + Sente test app
JavaScript
46
star
9

jsql

Basic DSL for generating SQL/DDL, formerly java.jdbc.sql and java.jdbc.ddl
Clojure
43
star
10

boot-tools-deps

A Boot task (deps) that wraps tools.deps(.alpha) to read deps.edn files
Clojure
39
star
11

engine

A Clojure library to implement a query -> logic -> updates workflow, to separate persistence updates from business logic, to improve testing etc.
Clojure
22
star
12

clj-soap

Fork of https://bitbucket.org/taka2ru/clj-soap updated to latest Clojure version
Clojure
21
star
13

lein-fregec

A Leiningen plugin to compile Frege (http://www.frege-lang.org) code.
Clojure
20
star
14

atom-chlorine-setup

My Atom / Chlorine setup
Clojure
17
star
15

polylith-external-test-runner

An external (subprocess) test runner for Polylith
Clojure
13
star
16

socket-rebl

A Socket REPL that also submits forms to Cognitect's REBL
Clojure
13
star
17

next.jdbc.xt

Experimental extension of next.jdbc to work with XTDB 2.0 (snapshots)
Clojure
12
star
18

ring-cfml

A version of Ring (Clojure) for CFML
ColdFusion
12
star
19

logging4j2

A Clojure wrapper for log4j2
Clojure
11
star
20

java-clojure-example

Trivial example to show using Java from Clojure in deps.edn project
Clojure
9
star
21

datamapper

A couple of CFCs from the World Singles data mapper to show how we wrap Clojure (vectors of) hashmaps to present a thin OO veneer to our CFML code.
ColdFusion
8
star
22

liquid-setup

Configuration for the Liquid in-process Clojure editor
Clojure
7
star
23

boot-kotlinc

A Kotlin compilation task for Boot
Clojure
6
star
24

macro-day

Code examples I wrote during Amit Rathore's "A day with Clojure macros" training
Clojure
6
star
25

build-uber-log4j2-handler

A conflict handler for log4j2 plugins cache files for the tools.build uber task.
Clojure
6
star
26

edmund

Edmund is an Event-Driven Model micro-framework for CFML / ColdFusion
ColdFusion
5
star
27

avowal

Futures and Promises for modern CFML, inspired by my earlier cfconcurrency library
ColdFusion
3
star
28

datafy-nav-example

Examples of datafy and nav
Clojure
3
star
29

lein-fw1

A Leiningen plugin to create and manage FW/1 projects.
Clojure
3
star
30

cfml-interop

CFML/Clojure interop library extracted from World Singles code and open sourced!
Clojure
3
star
31

clojure-dining-car

A Categorized and Annotated Directory of Clojure Libraries
2
star
32

intro2fp

Code samples for Introduction to Functional Programming talk, cf.Objective() 2011
ColdFusion
2
star
33

cat-genetics

Utilities to help calculate TICA-specific color cat genetics etc
Clojure
2
star
34

clojure-lucee

A toy example of running CFML pages via Lucee as an embedded engine in a Clojure Ring application.
Clojure
2
star
35

lightstuff

Historical archive of Peter Bell's LightBase and LightGen utility framework/code
ColdFusion
2
star
36

orm-blog

A very simple blog/cms built in ColdFusion using Framework 1 and ORM.
ColdFusion
2
star
37

poly-classloader-bug

Repro for a potential classloader bug for poly test
Clojure
1
star
38

dojo-anthem

Team 1's code from the January 2013 San Francisco Clojure Dojo - to play the National Anthem
Clojure
1
star
39

cursive-expectations

Example of using Expectations with Cursive
Clojure
1
star
40

spd1

Introduction to Systematic Program Design Part 1 - some Clojure examples
Clojure
1
star
41

ordered-subset

Clojure
1
star
42

HUnitFrege

A port of HUnit from Haskell to Frege (WIP -- not even compiling yet!)
Frege
1
star
43

cfo2013

Code examples for my cf.Objective() 2013 presentations
ColdFusion
1
star
44

clojure-test

A place to discuss clojure.test concerns and possible enhancements
1
star
45

boot-localrepo

Boot tasks that wrap lein-localrepo functionality
Clojure
1
star
46

seancorfield.github.io

An Architect's View - my blog and personal web site
HTML
1
star
47

polylith-issue146

Repro for the setup-fn problem with the issue-146 branch
Clojure
1
star
48

WeeklyWrongChallenge

The weekly challenge repo for WPW
Clojure
1
star
49

ring-async-bug

SSCCE of Ring with async on Jetty that fails with thread death
Clojure
1
star
50

avaus-tv

Simulation of Avaus-TV prize draw, in Clojure
Clojure
1
star
51

hello-compojure

The Compojure template, but without lein-ring
Clojure
1
star
52

cfbloggers

Clojure scratch code to parse and analyze the feeds at coldfusionbloggers.org
Clojure
1
star
53

boxlang-clojure

An example of calling Clojure code from BoxLang
Clojure
1
star