-
Notifications
You must be signed in to change notification settings - Fork 134
/
user.clj
116 lines (100 loc) · 3.24 KB
/
user.clj
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(ns user
(:import (java.io ByteArrayOutputStream))
(:require [ring.server.standalone :as rsa]
[ring.middleware.params :as params]
[ring.middleware.edn :as rme]
[ring.middleware.file :as rmf]
[ring.middleware.multipart-params :as rmmp]
[ring.middleware.transit :as tr]
[cognitect.transit :as t]
[clojure.tools.namespace.repl
:refer (refresh refresh-all)]
[ring.util.response :as rur]
[hiccup.core :refer :all]))
(defn edn-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/edn"}
:body (pr-str data)})
(def system nil)
(defn write-transit [x]
(let [baos (ByteArrayOutputStream.)
w (t/writer baos :json)
_ (t/write w x)
ret (.toString baos)]
(.reset baos)
ret))
(defn transit-response [response]
{:status 200
:headers {"Content-Type" "application/transit+json; charset=utf-8"}
:body (write-transit response)})
(defn png-response [response]
{:status 200
:headers {"Content-Type" "image/png"
"Content-Disposition" "inline; filename=\"foo.png\""}
:body "im not even a real png!"})
(defn ajax-handler
([{{:keys [id timeout input output]} :params :as x}]
(println x)
(ajax-handler id timeout input output))
([id timeout input]
(ajax-handler id timeout input nil))
([id timeout input output]
(when timeout
(println "Timeout " timeout)
(Thread/sleep timeout))
(if id
(doto
((or output edn-response)
{:id id :output (str "INPUT: " input)})
println)
(rur/not-found ""))))
(defn ajax-uri-handler [{{:strs [id timeout input]} :params}]
(ajax-handler (read-string id) (read-string timeout) input))
(defn ajax-form-data-handler [request]
(ajax-uri-handler request))
(defn sc-handler [{:keys [uri] :as request}]
(case uri
"/" {:status 200
:body (html
[:h1 "Ajax Tester"]
[:script {:src "/integration.js" :type "text/javascript"}])}
;;; "/js/unit-test.js" (rur/file-response "target/unit-test.js")
"/ajax" (ajax-handler request)
"/ajax-transit" (ajax-handler
(update-in request [:params]
#(assoc % :output transit-response)))
"/ajax-url" (ajax-uri-handler request)
"/ajax-form-data" (ajax-form-data-handler request)
"/ajax-form-data-png" (png-response request)
"/favicon.ico" (rur/not-found "")))
(defn sc-system [] nil)
(defn sc-start [config]
(-> sc-handler
(rmf/wrap-file "target-int")
rme/wrap-edn-params
params/wrap-params
rmmp/wrap-multipart-params
tr/wrap-transit-params
rsa/serve))
(defn init
"Constructs the current development system."
[]
(alter-var-root #'system
(constantly (sc-system))))
(defn start
"Starts the current development system."
[]
(alter-var-root #'system sc-start))
(defn stop
"Shuts down and destroys the current development system."
[]
(alter-var-root #'system
(fn [s] (when s (.stop s)))))
(defn go
"Initializes the current development system and starts it running."
[]
(init)
(start))
(defn reset []
(stop)
(refresh :after 'user/go))