Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
frenchy64 committed May 13, 2024
1 parent 76f8c6e commit f6a160c
Show file tree
Hide file tree
Showing 20 changed files with 764 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Example projects

## TODO

* Component
* Buddy
* Mount
32 changes: 32 additions & 0 deletions examples/resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Resources

Simple [compojure-api](https://github.com/metosin/compojure-api)-project using [`resources`](https://github.com/metosin/compojure-api/blob/master/src/compojure/api/resource.clj).

Demonstrates how to use resources to build data-driven apis.

(not REST, just http-apis here).

<img src="https://raw.githubusercontent.com/metosin/compojure-api/master/examples/resources/screenshot.png" />

## Usage

### Run the application locally

`lein ring server`

### Packaging and running as standalone jar

```
lein do clean, ring uberjar
java -jar target/server.jar
```

### Packaging as war

`lein ring uberwar`

## License

Copyright © 2014-2016 [Metosin Oy](http://www.metosin.fi)

Distributed under the Eclipse Public License, the same as Clojure.
7 changes: 7 additions & 0 deletions examples/resources/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(defproject example "0.1.0-SNAPSHOT"
:description "Example on Compojure-api resources"
:dependencies [[org.clojure/clojure "1.8.0"]
[metosin/compojure-api "1.1.8"]]
:ring {:handler example.handler/app}
:uberjar-name "server.jar"
:profiles {:dev {:plugins [[ikitommi/lein-ring "0.9.8-SNAPSHOT"]]}})
Binary file added examples/resources/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions examples/resources/src/example/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
(ns example.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[ring.util.http-status :as http-status]
[schema.core :as s]))

;;
;; Schemas
;;

(s/defschema Pizza
{:id s/Int
:name s/Str
(s/optional-key :description) s/Str
:size (s/enum :L :M :S)
:origin {:country (s/enum :FI :PO)
:city s/Str}})

(s/defschema NewPizza (dissoc Pizza :id))
(s/defschema UpdatedPizza NewPizza)

;;
;; Database
;;

(def pizzas (atom {}))

(let [ids (atom 0)]
(defn update-pizza! [maybe-id maybe-pizza]
(let [id (or maybe-id (swap! ids inc))]
(if maybe-pizza
(swap! pizzas assoc id (assoc maybe-pizza :id id))
(swap! pizzas dissoc id))
(@pizzas id))))

;;
;; Application
;;

(def app
(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {:info {:title "Resource sample"
:description "Example app using `compojure.api.resource`."
:contact {:url "https://github.com/metosin/compojure-api/examples/resources"}}
:tags [{:name "pizza", :description "pizzas"}]}}}

(context "/pizza/" []
(resource
{:tags ["pizza"]
:get {:summary "get pizzas"
:description "get all pizzas!"
:responses {http-status/ok {:schema [Pizza]}}
:handler (fn [_] (ok (vals @pizzas)))}
:post {:summary "add's a pizza"
:parameters {:body-params NewPizza}
:responses {http-status/created {:schema Pizza
:description "the created pizza"
:headers {"Location" s/Str}}}
:handler (fn [{body :body-params}]
(let [{:keys [id] :as pizza} (update-pizza! nil body)]
(created (path-for ::pizza {:id id}) pizza)))}}))

(context "/pizza/:id" []
:path-params [id :- s/Int]

(resource
{:tags ["pizza"]
:get {:x-name ::pizza
:summary "gets a pizza"
:responses {http-status/ok {:schema Pizza}}
:handler (fn [_]
(if-let [pizza (@pizzas id)]
(ok pizza)
(not-found)))}
:put {:summary "updates a pizza"
:parameters {:body-params UpdatedPizza}
:responses {http-status/ok {:schema Pizza}}
:handler (fn [{body :body-params}]
(if (@pizzas id)
(ok (update-pizza! id body))
(not-found)))}
:delete {:summary "deletes a pizza"
:handler (fn [_]
(update-pizza! id nil)
(no-content))}}))))
32 changes: 32 additions & 0 deletions examples/reusable-resources/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Reusable resources

Simple [compojure-api](https://github.com/metosin/compojure-api)-project using [`resources`](https://github.com/metosin/compojure-api/blob/master/src/compojure/api/resource.clj).

Demonstrates how to build reusable resource apis - both predefined & runtime-generated.

(not REST, just http-apis here).

<img src="https://raw.githubusercontent.com/metosin/compojure-api/master/examples/reusable-resources/screenshot.png" />

## Usage

### Run the application locally

`lein ring server`

### Packaging and running as standalone jar

```
lein do clean, ring uberjar
java -jar target/server.jar
```

### Packaging as war

`lein ring uberwar`

## License

Copyright © 2014-2016 [Metosin Oy](http://www.metosin.fi)

Distributed under the Eclipse Public License, the same as Clojure.
7 changes: 7 additions & 0 deletions examples/reusable-resources/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(defproject example "0.1.0-SNAPSHOT"
:description "Example on reusable Compojure-api resources"
:dependencies [[org.clojure/clojure "1.8.0"]
[metosin/compojure-api "1.1.8"]]
:ring {:handler example.handler/app}
:uberjar-name "server.jar"
:profiles {:dev {:plugins [[ikitommi/lein-ring "0.9.8-SNAPSHOT"]]}})
Binary file added examples/reusable-resources/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions examples/reusable-resources/src/example/domain.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns example.domain
(:require [schema.core :as s]
[plumbing.core :as p]
[clojure.string :as str]))

(s/defschema Pizza
{:id s/Int
:name s/Str
(s/optional-key :description) s/Str
:size (s/enum :L :M :S)
:origin {:country (s/enum :FI :PO)
:city s/Str}})

(s/defschema Kebab
{:id s/Int
:name s/Str
:type (s/enum :doner :shish :souvlaki)})

(s/defschema Sausage
{:id s/Int
:type (s/enum :musta :jauho)
:meat s/Int})

(s/defschema Beer
{:id s/Int
:type (s/enum :ipa :apa)})

(defn entities []
(p/for-map [[n v] (ns-publics 'example.domain)
:when (s/schema-name @v)]
(str/lower-case n) @v))
73 changes: 73 additions & 0 deletions examples/reusable-resources/src/example/entity.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
(ns example.entity
(:require [compojure.api.sweet :as sweet]
[ring.util.http-response :refer :all]
[ring.util.http-status :as http-status]
[clojure.string :as str]
[schema.core :as s]))

;;
;; Database
;;

(defn update! [db name maybe-id maybe-entity]
(let [id (or maybe-id (::ids (swap! db update ::ids (fnil inc 0))))]
(if maybe-entity
(swap! db update name assoc id (assoc maybe-entity :id id))
(swap! db update name dissoc id))
(get-in @db [name id])))

;;
;; Routes
;;

(defn resource
([db Schema]
(resource db Schema nil))
([db Schema tag]
(let [entity (str/lower-case (s/schema-name Schema))
tag (or tag entity)
update! (partial update! db entity)
qualified-name (keyword (-> Schema meta :ns str) (-> Schema meta :name (str tag)))
NewSchema (s/schema-with-name (dissoc Schema :id) (str "New" (s/schema-name Schema)))
UpdatedSchema (s/schema-with-name NewSchema (str "Updated" (s/schema-name Schema)))]

(sweet/routes
(sweet/context (format "/%s/" entity) []
(sweet/resource
{:tags [tag]
:get {:summary (format "get %ss" entity)
:description (format "get all %ss!" entity)
:responses {http-status/ok {:schema [Schema]}}
:handler (fn [_] (ok (-> @db (get entity) vals)))}
:post {:summary "add's a pizza"
:parameters {:body-params NewSchema}
:responses {http-status/created {:schema Schema
:description (format "the created %s" entity)
:headers {"Location" s/Str}}}
:handler (fn [{body :body-params}]
(let [{:keys [id] :as entity} (update! nil body)]
(created (sweet/path-for qualified-name {:id id}) entity)))}}))

(sweet/context (format "/%s/:id" entity) []
:path-params [id :- s/Int]

(sweet/resource
{:tags [tag]
:get {:x-name qualified-name
:summary (format "gets a %s" entity)
:responses {http-status/ok {:schema Schema}}
:handler (fn [_]
(if-let [entity (get-in @db [entity id])]
(ok entity)
(not-found)))}
:put {:summary (str "updates a %s" entity)
:parameters {:body-params UpdatedSchema}
:responses {http-status/ok {:schema Schema}}
:handler (fn [{body :body-params}]
(if (get-in @db [entity id])
(ok (update! id body))
(not-found)))}
:delete {:summary (str "deletes a %s" entity)
:handler (fn [_]
(update! id nil)
(no-content))}}))))))
33 changes: 33 additions & 0 deletions examples/reusable-resources/src/example/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns example.handler
(:require [compojure.api.sweet :refer [api context routes GET]]
[ring.util.http-response :refer :all]
[clojure.string :as str]
[example.entity :as entity]
[example.domain :as domain]))

(def app
(let [db (atom {})
entities (domain/entities)
entity-resource (partial entity/resource db)
entity-resource-routes (->> entities vals (map entity-resource) (apply routes))
entity-tags (->> entities keys (map (fn [name] {:name name, :description (str "api to manage " name "s")})))]

(api
{:swagger
{:ui "/"
:spec "/swagger.json"
:data {:info {:title "Reusable resources"
:description (str "Example app using `compojure.api.resource`.<br> "
"The `*runtime*`-routes are generated at runtime, "
"based on the path. <br>Despite the swagger-ui only "
"shows `sausage` as runtime entity api, <br> apis exist for all "
"defined entities: `pizza`, `kebab`, `sausage` and `beer`.<br>"
"try `/runtime/pizza/`, `/runtime/kebab/` etc.")
:contact {:url "https://github.com/metosin/compojure-api/"}}
:tags entity-tags}}}

entity-resource-routes

(context "/runtime" request
(if-let [entity (or (some-> request :path-info (str/replace #"/" "")) "sausage")]
(entity-resource (entities entity) "*runtime*"))))))
24 changes: 24 additions & 0 deletions examples/simple/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Simple compojure-api app

## Usage

### Run the application locally

`lein ring server`

### Packaging and running as standalone jar

```
lein do clean, ring uberjar
java -jar target/server.jar
```

### Packaging as war

`lein ring uberwar`

## License

Copyright © 2016 [Metosin Oy](http://www.metosin.fi)

Distributed under the Eclipse Public License, the same as Clojure.
7 changes: 7 additions & 0 deletions examples/simple/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(defproject example "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:dependencies [[org.clojure/clojure "1.8.0"]
[metosin/compojure-api "1.1.8"]]
:ring {:handler example.handler/app}
:uberjar-name "server.jar"
:profiles {:dev {:plugins [[ikitommi/lein-ring "0.9.8-SNAPSHOT"]]}})
35 changes: 35 additions & 0 deletions examples/simple/src/example/handler.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
(ns example.handler
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[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 "/"
:spec "/swagger.json"
:data {:info {:title "Simple"
: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)))))
4 changes: 4 additions & 0 deletions examples/thingie/dev-src/user.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns user
(:require [reloaded.repl :refer [set-init! system init start stop go reset]]))

(set-init! #(do (require 'examples.server) ((resolve 'examples.server/new-system))))
Loading

0 comments on commit f6a160c

Please sign in to comment.