ring
library to your
deps.edn
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.12.0"}
ring/ring {:mvn/version "1.13.0"}}}
Ring is the library most everyone in the Clojure world uses for HTTP servers.
src/example/main.clj
file so it has the following
contents.
(ns example.main
(:require [ring.adapter.jetty :as jetty]))
(defn handler
[request]
{:status 200
:headers {"Content-Type" "text/html"}
:body "Hello, world"})
(defn -main []
(jetty/run-jetty #'handler {:port 9999}))
The handler
function takes in an HTTP Request as outlined
by
The Ring Specification
and returns a map representing an HTTP Response.
When we call jetty/run-jetty
we give it that
handler
function. A small wrinkle is that little
#'
in front. That makes it so that if you reload that
function in a REPL
the server will use the new definition
right away.
For more details on how that works you can dig into
Clojure Vars. All you
need to know is that when you want to pass a function as an argument
and have it work with the REPL
, you need to put
#'
in front.
just run
You should see a warning like the following. Ignore it for now.
SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.
http://localhost:9999
You should see Hello, world
.