Stuff on top of Compojure for making sweet web apis.
- API Docs & Wiki
- Schema for input & output data coercion
- Swagger for api documentation, via ring-swagger
- Extendable route DSL via metadata handlers
- Client negotiable formats: JSON, EDN, YAML & Transit (JSON & MessagePack)
- Data-driven resources
- Bi-directional routing
- Bundled middleware for common api behavior (exception handling, parameters & formats)
- Route functions & macros for putting things together, including the Swagger-UI via ring-swagger-ui
Clojurians slack (join) has a channel #ring-swagger for talk about any libraries using Ring-swagger. You can also ask questions about Compojure-api and Ring-swagger on other channels at Clojurians Slack or at #clojure on Freenode IRC (mention compojure-api or ring-swagger to highlight us).
(require '[compojure.api.sweet :refer :all])
(require '[ring.util.http-response :refer :all])
(defapi app
(GET "/hello" []
:query-params [name :- String]
(ok {:message (str "Hello, " name)})))(require '[schema.core :as s])
(s/defschema Pizza
{:name s/Str
(s/optional-key :description) s/Str
:size (s/enum :L :M :S)
:origin {:country (s/enum :FI :PO)
:city s/Str}})
(def app
(api
{:swagger
{:ui "/api-docs"
:spec "/swagger.json"
:data {:info {:title "Sample API"
:description "Compojure Api example"}
:tags [{:name "api", :description "some apis"}]}}}
(context "/api" []
:tags ["api"]
(GET "/plus" []
:return {:result Long}
:query-params [x :- Long, y :- Long]
:summary "adds two numbers together"
(ok {:result (+ x y)}))
(POST "/echo" []
:return Pizza
:body [pizza Pizza]
:summary "echoes a Pizza"
(ok pizza)))))https://github.com/metosin/compojure-api/tree/master/examples
To try it yourself, clone this repository and do either:
lein runlein repl&(go)
Clone the examples-repository.
Use a Leiningen template, with or without tests:
lein new compojure-api my-api
lein new compojure-api my-api +midje
lein new compojure-api my-api +clojure-test
Copied code from tools.macro has license:
Copyright (c) Rich Hickey. All rights reserved.
The use and distribution terms for this software are covered by the Eclipse Public License 1.0 (https://opensource.org/license/epl-1-0/)
which can be found in the file epl-v10.html at the root of this distribution. By using this software in any fashion, you are agreeing to
be bound bythe terms of this license. You must not remove this notice, or any other, from this software.
Copied code from compojure has license:
Copyright © 2024 James Reeves
Distributed under the Eclipse Public License, the same as Clojure.
All other code:
Copyright © 2014-2016 Metosin Oy
Distributed under the Eclipse Public License, the same as Clojure.
