Simple REST service in Clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns example.core | |
(:require [org.httpkit.server :as http-kit] | |
[compojure.api.sweet :refer :all] | |
[ring.util.http-response :refer :all])) | |
(def counter (atom 0)) ;; declare counter | |
(defn greeting-handler [req] ;; ordinary clojure function, accepts request map, returns a response map | |
(let [name (-> req :params :name (or "world"))] ;; get param from url or use "world" by default | |
(ok ;; wraps data with status code 200 | |
{:id (swap! counter inc) ;; increment counter | |
:name (str "Hello, " name "!")}))) | |
(def app | |
(api ;; wrap routes with useful middleware provided by compojure-api | |
(GET "/greeting" [] greeting-handler))) | |
(defonce server ;; declare server variable once | |
(http-kit/run-server #'app {:port 8080})) ;; #'app allows to hot swap changes in handlers |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Same service as in Spring docs example, but written in Clojure using Compojure API (routing and middleware) and http-kit server