Ring + reitit Clojure
一个用于 Ring + reitit 的 Clojure 代码片段,以空 GIF 响应对 /pixel.gif 的请求,GIF 在启动时只解码一次。
Ring + reitit Clojure
(ns pixel.core
(:require [reitit.ring :as ring]
[ring.adapter.jetty :as jetty])
(:import (java.util Base64)))
(def gif (.decode (Base64/getDecoder) "R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=="))
(def app
(ring/ring-handler
(ring/router
["/pixel.gif" {:get (fn [_]
{:status 200
:headers {"Content-Type" "image/gif"
"Content-Length" (str (alength gif))
"Cache-Control" "no-store"}
:body gif})}])
(ring/create-default-handler)))
(defn -main []
(jetty/run-jetty app {:port 8080 :join? true}))
我们运行了此代码片段,并比对了它返回的字节。
验证
curl -sI http://localhost:8080/pixel.gif