Skip to content

metosin/compojure-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Jun 17, 2024
afc1a93 · Jun 17, 2024
May 14, 2024
Oct 21, 2016
Oct 19, 2016
Feb 17, 2016
Jun 17, 2024
Apr 22, 2024
May 22, 2015
May 14, 2024
Nov 28, 2015
May 1, 2024
May 14, 2024

Repository files navigation

Compojure-api 1.1.x

Clojars 1.1.14 Slack

Stuff on top of Compojure for making sweet web apis.

Latest version

1.1.14

For information and help

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

Examples

Hello World

(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)})))

Api with Schema & Swagger-docs

(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)))))

swagger-api

More samples

https://github.com/metosin/compojure-api/tree/master/examples

To try it yourself, clone this repository and do either:

  1. lein run
  2. lein repl & (go)

Quick start for new project

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

License

Copyright © 2014-2016 Metosin Oy

Distributed under the Eclipse Public License, the same as Clojure.