-
Notifications
You must be signed in to change notification settings - Fork 50
/
route.lisp
294 lines (254 loc) · 12.1 KB
/
route.lisp
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;;; route.lisp
;;;;
;;;; This file is part of the RESTAS library, released under Lisp-LGPL.
;;;; See file COPYING for details.
;;;;
;;;; Author: Moskvitin Andrey <archimag@gmail.com>
(in-package :restas)
(defgeneric process-route (route bindings))
(defgeneric route-module (route))
(defgeneric make-route-url (obj args))
(defclass route (routes:route)
((symbol :initarg :symbol :reader route-symbol)
(module :initarg :module :initform nil :reader route-module)
(required-method :initarg :required-method :initform nil
:reader route-required-method)
(arbitrary-requirement :initarg :arbitrary-requirement :initform nil
:reader route-arbitrary-requirement)
(render-method :initarg :render-method :initform #'identity)
(headers :initarg :headers :initform nil :reader route-headers)
(variables :initarg :variables :initarg nil)
(additional-variables :initarg :additional-variables :initform nil)))
(defun route-render-method (route)
(or (slot-value route 'render-method)
(module-render-method (route-module route))
#'identity))
(defmethod routes:route-check-conditions :around ((route routes:base-route)
bindings)
(let ((*route* route))
(with-module (route-module route)
(call-next-method))))
(defmethod routes:route-check-conditions ((route route) bindings)
(with-slots (required-method arbitrary-requirement) route
(and (if required-method
(eql (hunchentoot:request-method*) required-method)
t)
(if arbitrary-requirement
(if (listp arbitrary-requirement)
(every #'funcall arbitrary-requirement)
(funcall arbitrary-requirement))
t))))
(defmethod routes:route-name ((route route))
(string-downcase (write-to-string (slot-value route 'symbol))))
(defmethod process-route :around ((route routes:base-route) bindings)
(with-module (route-module route)
(call-next-method)))
(defmethod process-route ((route route) bindings)
(alexandria:doplist (name value (route-headers route))
(setf (hunchentoot:header-out name)
(if (functionp value)
(funcall value)
value)))
(let ((*route* route)
(rsymbol (route-symbol route)))
(render-object
(route-render-method route)
(catch 'route-done
(apply rsymbol
(append (iter (for item in (slot-value route 'variables))
(collect (cdr (assoc item bindings
:test #'string=))))
(iter (for (key fun) in
(slot-value route 'additional-variables))
(collect key)
(collect (funcall fun)))))))))
(defun abort-route-handler (obj &key return-code content-type)
(when return-code
(setf (hunchentoot:return-code*) return-code
*standard-special-page-p* nil))
(when content-type
(setf (hunchentoot:content-type*) content-type))
(throw 'route-done obj))
;;;; define-route
(defmacro define-route (name (template &key method content-type) &body body)
(multiple-value-bind (declarations-map real-body)
(split-code-declarations body)
(let* ((route-traits (make-hash-table))
(variables (routes:template-variables
(routes:parse-template template)))
(arglist (mapcar (alexandria:compose #'intern #'symbol-name)
variables)))
(setf (gethash :template route-traits) template
(gethash :method route-traits) (or method :get)
(gethash :content-type route-traits) content-type
(gethash :variables route-traits) variables)
(parse-all-declarations declarations-map
'(:sift-variables :additional-variables
:render-method :apply-render-method
:content-type :http-method
:requirement
:decorators)
route-traits)
(setf (gethash :variables route-traits) `',arglist)
`(progn
(defun ,name (,@arglist
,@(gethash :additional-variables-arglist route-traits))
,@real-body)
(register-route-traits
',name
(alexandria:plist-hash-table
(list ,@(iter (for type in
'(:template :method :content-type :render-method
:parse-vars :requirement :decorators
:additional-variables :variables))
(for value = (gethash type route-traits))
(when value
(collect type)
(collect value))))))
(reconnect-all-routes)))))
(defun get-ref-syms (route-symbol &optional syms)
(iter (for pkg in (pkgmodule-traits-references (symbol-package route-symbol)))
(iter (for (key nil) in-hashtable (pkgmodule-traits-modules pkg))
(push (alexandria:format-symbol pkg "~A.~A" key route-symbol) syms)
(setf syms (get-ref-syms (first syms) syms))))
syms)
(defun delete-route (route-symbol &aux package)
(setf package (symbol-package route-symbol))
(when package
(remhash route-symbol (pkgmodule-traits-routes package))
(mapcar (lambda (sym)
(unintern sym (symbol-package sym)))
(get-ref-syms route-symbol))
(unintern route-symbol (symbol-package route-symbol))))
(defun route-template-from-symbol (symbol module)
(flet ((call-in-context-wrap (fun)
(alexandria:named-lambda parse-in-context (value)
(with-module module
(funcall fun value)))))
(let ((traits (gethash symbol
(pkgmodule-traits-routes (symbol-package symbol)))))
(concatenate 'list
(module-real-url module)
(routes:parse-template
(gethash :template traits)
(iter (for tail on (gethash :parse-vars traits) by #'cddr)
(collect (first tail))
(collect (call-in-context-wrap (second tail)))))))))
(defun create-route-from-symbol (symbol module)
(destructuring-bind (&key content-type headers method requirement render-method
decorators variables additional-variables
&allow-other-keys)
(alexandria:hash-table-plist (gethash symbol (pkgmodule-traits-routes
(symbol-package symbol))))
(setf (getf headers :content-type)
(or content-type
(getf headers :content-type)
(pkgmodule-traits-content-type (symbol-package symbol))
"text/html"))
(apply-decorators (make-instance 'route
:template (route-template-from-symbol
symbol module)
:symbol symbol
:required-method method
:arbitrary-requirement requirement
:render-method render-method
:module module
:headers headers
:variables variables
:additional-variables additional-variables)
(append (module-decorators module)
decorators))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; generate url by route
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun route-symbol-template (route-symbol)
(routes:parse-template (gethash :template
(gethash route-symbol
(pkgmodule-traits-routes
(symbol-package route-symbol))))))
(defmethod make-route-url ((tmpl list) args)
(let* ((uri (make-instance 'puri:uri))
(bindings (iter (for rest on args by #'cddr)
(for key = (first rest))
(for value = (second rest))
(collect
(cons key
(if (or (stringp value) (consp value))
value
(write-to-string value))))))
(query-part (set-difference bindings
(routes:template-variables tmpl)
:test (alexandria:named-lambda
known-variable-p (pair var)
(eql (car pair) var)))))
(setf (puri:uri-parsed-path uri)
(cons :absolute
(routes::apply-bindings tmpl bindings)))
(when query-part
(setf (puri:uri-query uri)
(format nil
"~{~(~A~)=~A~^&~}"
(alexandria:flatten query-part))))
uri))
(defmethod make-route-url ((route symbol) args)
(if *module*
(make-route-url (or (find-route route)
(error "Unknown route: ~A" route)) args)
(make-route-url (route-symbol-template route) args)))
(defmethod make-route-url ((route route) args)
(make-route-url (routes:route-template route) args))
(defun genurl (route-symbol &rest args &key &allow-other-keys)
(puri:render-uri (make-route-url route-symbol args) nil))
(defun genurl* (route-symbol &rest args &key &allow-other-keys)
(let ((url (make-route-url route-symbol args)))
(setf (puri:uri-scheme url) :http
(puri:uri-host url) (if (boundp 'hunchentoot:*request*)
(hunchentoot:host)
"localhost"))
(puri:render-uri url nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; redirect
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun apply-format-aux (format args)
(if (symbolp format)
(apply #'restas:genurl format args)
(if args
(apply #'format nil (cons format args))
format)))
(defun redirect (route-symbol &rest args)
(hunchentoot:redirect
(hunchentoot:url-decode
(apply-format-aux route-symbol
(mapcar #'(lambda (s)
(if (stringp s)
(hunchentoot:url-encode s)
s))
args)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; parse url for route
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun parse-route-url (url route-symbol)
(let ((mapper (make-instance 'routes:mapper))
(module *module*))
(routes:connect mapper
(make-instance 'route
:template
(routes:route-template (module-find-route
module route-symbol))
;;(route-template-from-symbol route-symbol module)
:module module))
(multiple-value-bind (route bindings) (routes:match mapper url)
(if route
(alexandria:alist-plist bindings)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; proxy route
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod process-route ((route routes:proxy-route) bindings)
(process-route (routes:proxy-route-target route)
bindings))
(defmethod route-module ((route routes:proxy-route))
(route-module (routes:proxy-route-target route)))
(defmethod route-symbol ((route routes:proxy-route))
(route-symbol (routes:proxy-route-target route)))
(defmethod make-route-url ((route routes:proxy-route) bindings)
(make-route-url (routes:proxy-route-target route) bindings))