-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclack-tool.lisp
58 lines (51 loc) · 1.98 KB
/
clack-tool.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
(in-package #:webhax-core)
;;;
;;; Base class for making webhax services as clack middleware components.
;;;
(defun logged-in-p (env)
(getf env :remote-user))
(defun url-splitter (url base)
(split-sequence #\/ (subseq url (length base)) :remove-empty-subseqs t))
(defclass clack-tool (lack.component:lack-component)
((base-url :type string
:initarg :base-url
:initform "")
(login-p :initarg :login-p
:initform t)
(default-content-type :type string
:initarg :default-content-type
:initform "text/json")
(function :type function
:initarg :function
:initform (lambda () ""))))
;FIXME: Needs updating to new version of clack
#|
(defmethod call ((this clack-tool) env)
(with-slots (base-url login-p) this
(if (starts-with-subseq base-url (getf env :request-uri))
(if (and login-p (not (logged-in-p env)))
'(403 nil ("This service not available without login."))
(function-wrapper this env))
(call-next this env))))
|#
(defgeneric function-wrapper (obj env)
(:method ((this clack-tool) env)
(with-slots (default-content-type function base-url) this
(let* ((*request* (lack.request:make-request env))
(*response* (lack.response:make-response 200))
(*key-web-input* (lack.request:request-parameters *request*))
(*regular-web-input*
(cdr
(url-splitter
(lack.request:request-path-info *request*) base-url))))
(setf (lack.response:response-headers *response* :content-type)
default-content-type)
(setf (lack.response:response-body *response*)
(list (execute this)))
(lack.response:finalize-response *response*)))))
(defgeneric execute (obj)
(:method ((this clack-tool))
(with-slots (function) this
(when function
(funcall function)))))
;;;FIXME: Implement url producer?