Giter VIP home page Giter VIP logo

usermanager-example's Introduction

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.

A version of this application that uses the XTDB 2 database instead of SQLite/H2 is also available, on the xtdb branch.

Quickstart via Devcontainers or Github Codespaces

If you have configured your Github account, you can start the project without any other setup. It will open a web-based vscode editor backed by a Github Codespace VM. (Codespaces is Github's hosted Devcontainer solution)

Open in Github Codespaces

You can also clone this repo locally, and using vscode (with the devcontainer plugin), and Docker Desktop, run an isolated, fully setup version of this application locally. Open the repo in your editor and run the command Dev Containers: Open Folder in Container....

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 namespace
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.

usermanager-example's People

Contributors

jwhitlark avatar michaelrkytch avatar prestancedesign avatar seancorfield avatar sittim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

usermanager-example's Issues

Can I safely replace #'my-middleware with my-middleware in line 119 of main-clj?

Dear Sean, thank you for usermanager-example.
In trying to understand your code, I re-read the chapter "Def, Symbols, and Vars" in the book "Getting Clojure". Then I did the following repl experiment:

user=> (def f (fn [x] (* x x)))
#'user/f
user=> (def g #'f)
#'user/g
user=> (g 2)
4
user=> (def h f)
#'user/h
user=> (h 2)
4

So,(g 2) and (h 2) being the same, I changed #'my-middleware to my-middleware in your code. The result is, that usermanager-example seems to work flawlessly.

I tried another repl experiment:

user=> (def a 1)
#'user/a
user=> (def b #'a)
#'user/b
user=> b
#'user/a
user=> (def c a)
#'user/c
user=> c
1

From this, I understand that sometimes it might be useful to pass the variable #'a instad of the bound value 1 in some function call. It is useful, because the symbol a is bound to a number and not to a (fn ...). But, as I tried to explain above, if some symbol f is bound to a (fn ...), I do not understand the use of passing the var #'f instead of the (fn ...) bound to f.

So, to rephrase my question: did you write this #'my-middleware instead of just my-middleware only because it is "more correct" in some sense, because it is more idiomatic, because it is better style? Or is it necessary for some reason?

I hope that you found the patience to bear with my problem and read to this end, and I ask for your answer in the hope to understand usermanager-example a bit better.

Simplify/streamline web server portion

Drop all the http-kit stuff (it just confuses folks). Maybe leave comments in about what would be needed to switch from Jetty to http-kit?

Either drop the promise or explain its purpose better? Probably simpler to just drop it.

Ensure started server component is written to a REPL-friendly Var and explain that as well.

Update all outdated dependencies

|    :file |                  :name |               :current |                           :latest |
|----------+------------------------+------------------------+-----------------------------------|
| deps.edn |              ring/ring |                  1.8.2 |                             1.9.2 |
|          | seancorfield/next.jdbc | seancorfield/next.jdbc | com.github.seancorfield/next.jdbc |
|          | seancorfield/next.jdbc |                1.1.613 |                           1.1.646 |
|          |          selmer/selmer |                1.12.31 |                           1.12.33 |

Small gotcha when trying out project due to namespace change

Looks like the "polylith" branch suggests running:

clj -M:dev -m usermanager.web.main

but a newbie like myself cloned the repo and immediately was put onto the "develop" branch where you have to run:

clj -M:dev -m usermanager.main

This might be a very small detail but probably everyone trips over this when trying this cool project out for the first time. I see you even say to run using the "polylith" branch but it's a little buried.

IMHO make those two branches align so everyone doesn't have to figure that out.

Very cool project example though, thanks @seancorfield !

Swap SQL code to XTQL code?

It might be more idiomatic to have the example use XTQL instead of SQL.

The original thinking was that keeping the SQL so similar to the main example meant fewer things needed to change -- but the qualified/unqualified column name stuff kind of sank that minimalism. So the question is whether it would be worth switching the example to XTQL (in which case we could keep the qualified names, aside from using xt/id instead of <table>/id).

Webserver does not restart upon component/start /stop /start

Step 0: successful initialisation of application

With original code unchanged, in the repl I evaluated the following two forms:

(def system (new-system 8888))
(alter-var-root #'system component/start)

Then, I modified the route handlers in the function my-handler. After doing a top-level evaluation of my-handler, the application did still not reflect the changes.

Unsuccessful activation of new handlers

After Step 0, I evaluated two forms:

(alter-var-root #'system component/stop)
(alter-var-root #'system component/start)

As opposed to my expectation, the function start in the WebServer record was not called, whereas this function start was indeed called upon Step 0 (see screenshot, note the expected but missing message "in record WebServer: start" after the last line). Naturally enough, the application showed a "localhost refused to connect." error.

Screenshot_usermanager_repl

Successful activation of new handlers

After Step 0, I evaluated three forms:

(alter-var-root #'system component/stop)
(def system (new-system 8888))
(alter-var-root #'system component/start)

Is this is the right way to activate the modified handlers? It seems odd to create another system for that.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.