Set up /sketches endpoint and tweak sketch rendering endpoint
This commit is contained in:
parent
97719b4c3c
commit
6627dd2adb
@ -1,7 +1,9 @@
|
||||
(ns sketchbook.handler
|
||||
(:require [compojure.core :refer :all]
|
||||
[compojure.route :as route]
|
||||
[ring.util.response :refer [response content-type]]
|
||||
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
|
||||
[ring.middleware.json :refer [wrap-json-response]]
|
||||
[ring.server.standalone :refer [serve]]
|
||||
[clojure.core.async :refer [<!! <! go]]
|
||||
[sketchbook.sketches :as sketches])
|
||||
@ -37,6 +39,10 @@
|
||||
|
||||
(defroutes app-routes
|
||||
(context "/api" []
|
||||
(GET "/sketches" []
|
||||
(content-type
|
||||
(response (sketches/get-sketches))
|
||||
"application/json"))
|
||||
(GET "/sketches/:sketch" [sketch width height] sketch-handler)
|
||||
(GET "/sketches/:sketch/:seed" [sketch seed width height] sketch-handler))
|
||||
(route/resources "/")
|
||||
@ -49,6 +55,7 @@
|
||||
(def app
|
||||
(-> app-routes
|
||||
(wrap-defaults site-defaults)
|
||||
(wrap-json-response)
|
||||
(wrap-index)))
|
||||
|
||||
(defn -main [& args]
|
||||
|
@ -8,25 +8,45 @@
|
||||
|
||||
(def sketches
|
||||
{:color {:setup sketchbook.sketches.color/setup
|
||||
:draw sketchbook.sketches.color/draw}})
|
||||
:draw sketchbook.sketches.color/draw
|
||||
:title "Color Play"}
|
||||
:foo {:setup sketchbook.sketches.color/setup
|
||||
:draw sketchbook.sketches.color/draw
|
||||
:title "Foo"}})
|
||||
|
||||
(defn sketch-bytes [& {:keys [size setup draw seed]}]
|
||||
(defn get-sketches []
|
||||
(map (fn [sketch]
|
||||
(let [path (name (first sketch))]
|
||||
{:path path
|
||||
:title (:title (second sketch))}))
|
||||
sketches))
|
||||
|
||||
(defn sketch-bytes [& {:keys [name size setup draw seed]}]
|
||||
(let [channel (chan)
|
||||
nonce (str (System/currentTimeMillis))
|
||||
filename (str "sketch-" nonce ".png")
|
||||
nonce (str (System/nanoTime))
|
||||
filename (str "sketch-files/"
|
||||
name "-"
|
||||
(first size) "-"
|
||||
(second size) "-"
|
||||
seed
|
||||
".png")
|
||||
out (ByteArrayOutputStream.)]
|
||||
(q/sketch :size size
|
||||
:setup (fn []
|
||||
(r/set-seed seed)
|
||||
(setup))
|
||||
:draw (fn []
|
||||
(draw)
|
||||
(q/save filename)
|
||||
(with-open [in (io/input-stream filename)]
|
||||
(io/copy in out))
|
||||
(io/delete-file filename)
|
||||
(go (>! channel (.toByteArray out))
|
||||
(q/exit))))
|
||||
(if (.exists (io/file filename))
|
||||
(do
|
||||
(with-open [in (io/input-stream filename)]
|
||||
(io/copy in out))
|
||||
(go (>! channel (.toByteArray out))))
|
||||
(q/sketch :size [(/ (first size) 2) (/ (second size) 2)] ;; For some reason images were coming out twice as large as they should be
|
||||
:setup (fn []
|
||||
(r/set-seed seed)
|
||||
(setup))
|
||||
:draw (fn []
|
||||
(draw)
|
||||
(q/save filename)
|
||||
(with-open [in (io/input-stream filename)]
|
||||
(io/copy in out))
|
||||
(go (>! channel (.toByteArray out))
|
||||
(q/exit)))))
|
||||
channel))
|
||||
|
||||
(defn render-sketch
|
||||
@ -37,7 +57,8 @@
|
||||
[name seed size]
|
||||
(let [sketch (get sketches name)]
|
||||
(when-not (nil? sketch)
|
||||
(sketch-bytes :size size
|
||||
(sketch-bytes :name name
|
||||
:size size
|
||||
:seed seed
|
||||
:setup (:setup sketch)
|
||||
:draw (:draw sketch)))))
|
||||
|
Loading…
Reference in New Issue
Block a user