Skip to content

Instantly share code, notes, and snippets.

@idozorenko

idozorenko/core.clj Secret

Created Dec 7, 2020
Embed
What would you like to do?
(ns example.core
(:require [org.httpkit.server :as http-kit]
[compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[clojure.spec.alpha :as s]))
(def users [{:id 1 :name "Alice"}
{:id 2 :name "Bob"}])
(def users-by-id (zipmap (map :id users) users))
(s/def ::id int?)
(s/def ::name string?)
(s/def ::user (s/keys ::req-un [::id ::name]))
(def app
(api
{:coercion :spec
:swagger {:ui "/swagger"
:spec "/swagger/swagger.json"}}
(GET "/users/:id" []
:path-params [id :- ::id]
:return (s/nilable ::user)
(let [user (users-by-id id)]
(if user
(ok user)
(not-found (str "user " id " not found")))))))
(defonce server
(http-kit/run-server #'app {:port 8080}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment