This repository was archived by the owner on Dec 30, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathring.clj
More file actions
75 lines (66 loc) · 2.74 KB
/
ring.clj
File metadata and controls
75 lines (66 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
(ns foreclojure.ring
(:require [clojure.java.io :as io]
[clojure.string :as s]
[compojure.route :as route]
[cheshire.core :as json])
(:import [java.net URL])
(:use [compojure.core :only [GET routes]]
[hiccup.core :only [html]]
[foreclojure.version-utils :only [strip-version-number]]
[foreclojure.ring-utils :only [get-host static-url]]
[useful.debug :only [?]]
[ring.util.response :only [response]]))
;; copied from compojure.route, modified to use File instead of Stream
(defn resources
"A route for serving resources on the classpath. Accepts the following
keys:
:root - the root prefix to get the resources from. Defaults to 'public'."
[path & [options]]
(GET path {{resource-path :*} :route-params}
(let [root (:root options "public")]
(when-let [res (io/resource (str root "/" resource-path))]
(response (io/as-file res))))))
(defn wrap-url-as-file [handler]
(fn [request]
(when-let [{body :body :as resp} (handler request)]
(if (and (instance? URL body)
(= "file" (.getProtocol ^URL body)))
(update-in resp [:body] io/as-file)
resp))))
(defn wrap-strip-trailing-slash [handler]
(fn [request]
(handler (update-in request [:uri] s/replace #"(?<=.)/$" ""))))
(defn wrap-versioned-expiry [handler]
(fn [request]
(when-let [resp (handler
(update-in request [:uri] strip-version-number))]
(assoc-in resp [:headers "Cache-control"]
"public, max-age=31536000"))))
(defn wrap-debug [handler label]
(fn [request]
(println "In" label)
(? (handler (? request)))))
(let [content-type [:headers "Content-Type"]]
(defn wrap-json [handler]
(fn [request]
(when-let [resp (handler request)]
(-> resp
(assoc-in content-type "application/json")
(update-in [:body] json/generate-string))))))
(defn split-hosts [host-handlers]
(let [default (:default host-handlers)]
(fn [request]
(let [host (get-host request)
handler (or (host-handlers host) default)]
(handler request)))))
(def render-404
(html
[:head
[:title "4clojure: Page not found"]]
[:body
[:div {:style "margin-left: auto; margin-right: auto; width: 300px;"}
[:p {:style "text-align: center; width: 100%; margin-top: 45px; font-family: helvetica; color: gray; font-size: 25px;"} "404 — Page not found."]
[:img {:style "margin-left: 18px;" :src (static-url "images/4clj-gus-confused-small.png")}]]]))
(defn wrap-404 [handler]
(routes handler
(route/not-found render-404)))