Giter Site home page Giter Site logo

matthewlisp / compojure-api Goto Github PK

View Code? Open in Web Editor NEW

This project forked from metosin/compojure-api

0.0 0.0 0.0 2.97 MB

Sweet web apis with Compojure & Swagger

Home Page: http://metosin.github.io/compojure-api/doc/

License: Eclipse Public License 1.0

Clojure 99.73% Shell 0.27%

compojure-api's Introduction

Compojure-api Build Status

Psst! If you're starting a new project, why not try out reitit?

Stuff on top of Compojure for making sweet web apis.

API Docs & Wiki

Latest version

Clojars Project

Latest non-alpha: [metosin/compojure-api "1.1.13"].

See CHANGELOG for details.

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 Api

(require '[compojure.api.sweet :refer :all])
(require '[ring.util.http-response :refer :all])

(def app
  (api
    (GET "/hello" []
      :query-params [name :- String]
      (ok {:message (str "Hello, " name)}))))

Hello World, async

(require '[compojure.api.sweet :refer :all])
(require '[clojure.core.async :as a])

(GET "/hello-async" []
  :query-params [name :- String]
  (a/go
    (a/<! (a/timeout 500))
    (ok {:message (str "Hello, " name)})))

* requires server to be run in async mode

Hello World, async & data-driven

(require '[compojure.api.sweet :refer :all])
(require '[clojure.core.async :as a])
(require '[schema.core :as s])

(context "/hello-async" []
  (resource
    {:get
     {:parameters {:query-params {:name String}}
      :responses {200 {:schema {:message String}}
                  404 {}
                  500 {:schema s/Any}}
      :handler (fn [{{:keys [name]} :query-params}]
                 (a/go
                   (a/<! (a/timeout 500))
                   (ok {:message (str "Hello, " name)})))}}))

* Note that empty body responses can be specified with {} or {:schema s/Any}

Hello World, async, data-driven & clojure.spec

(require '[compojure.api.sweet :refer :all])
(require '[clojure.core.async :as a])
(require '[clojure.spec.alpha :as s])

(s/def ::name string?)
(s/def ::message string?)

(context "/hello-async" []
  (resource
    {:coercion :spec
     :get {:parameters {:query-params (s/keys :req-un [::name])}
           :responses {200 {:schema (s/keys :req-un [::message])}}
           :handler (fn [{{:keys [name]} :query-params}]
                      (a/go
                        (a/<! (a/timeout 500))
                        (ok {:message (str "Hello, " name)})))}}))

Api with Schema & Swagger-docs

(require '[compojure.api.sweet :refer :all])
(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"}]
             :consumes ["application/json"]
             :produces ["application/json"]}}}

    (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

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

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

Quick start for a new project

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-2019 Metosin Oy and contributors.

Distributed under the Eclipse Public License, see LICENSE.

compojure-api's People

Contributors

ikitommi avatar deraen avatar miikka avatar phadej avatar kalekale avatar tomimas avatar thomaswhitcomb avatar nberger avatar michaelblume avatar hoxu avatar antonoellerer avatar amalloy avatar slipset avatar jsyrjala avatar mtkp avatar mveytsman avatar rickmoynihan avatar ryfow avatar tayhobbs avatar teropa avatar zamaterian avatar tchagnon avatar timgilbert avatar metametadata avatar mike706574 avatar joelittlejohn avatar jtmarmon avatar eunmin avatar dzhus avatar dryewo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.