forked from m8nware/ann
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hunch.lisp
198 lines (169 loc) · 7.26 KB
/
hunch.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
(ql:quickload '#:cl-ppcre)
(ql:quickload '#:rutilsx)
(ql:quickload '#:hunchentoot)
(ql:quickload '#:named-readtables)
(eval-when (:load-toplevel :compile-toplevel :execute)
(ql:quickload '#:swank))
(eval-when (:load-toplevel :compile-toplevel :execute)
(cl:rename-package "CL-PPCRE" "CL-PPCRE" '("PPCRE" "RE"))
(cl:rename-package "HUNCHENTOOT" "HUNCHENTOOT" '("TBNL" "HTT")))
(defpackage #:hunch
(:use #:cl #:rutilsx #:named-readtables)
(:import-from #:hunchentoot #:acceptor-log-message)
(:export #:*hunch-acceptor*
#:*port*
#:*swank-port*
#:*debug*
#:abort-request
#:start-web
#:restart-web
#:stop-web
#:url
#:fmt-url
#:list-urls
#:parse-url-template
#:argv))
(in-package #:hunch)
(in-readtable rutilsx-readtable)
;;; config
;; List of program's command-line arguments.
(define-symbol-macro *argv*
#+:sbcl (nthcdr 2 sb-ext:*posix-argv*)
#+:ccl (nthcdr 4 ccl:*command-line-argument-list*))
(defvar *hunch-acceptor* nil
"Hunch acceptor.")
(defvar *port* 8080
"Port at which the application will be started.")
(defvar *swank-port* nil
"Port for starting swank. If nil swank won't be started.")
(defvar *script* nil
"Script file to load.")
(defvar *debug* nil
"Start in debug mode.")
;; configure vars from command line
(loop :for args :on *argv* :do
(when (char= #\- (char (first args) 0))
(setf (symbol-value (mksym (sub (first args) 1) :format "*~A*"))
(let ((val (second args)))
(if (digit-char-p (char val 0 ))
(read-from-string val)
val)))))
(setf htt:*show-lisp-errors-p* *debug*)
;;; web
(defun start-web (&optional port)
(setf *hunch-acceptor* (make-instance 'htt:easy-acceptor
:port (or port *port*)
:error-template-directory nil))
(bt:make-thread #`(htt:start *hunch-acceptor*) :name "hunch-acceptor")
(acceptor-log-message *hunch-acceptor* :info
"Started hunch acceptor at port: ~A." (or port *port*)))
(defun stop-web ()
(when *hunch-acceptor*
(ignore-errors (htt:stop *hunch-acceptor*)))
(mapc #'bt:destroy-thread
(remove-if-not #`(member (bt:thread-name %) '("hunch-")
:test #`(starts-with %% %))
(bt:all-threads)))
(acceptor-log-message *hunch-acceptor* :info
"Stopped hunch acceptor at port: ~A."
(htt:acceptor-port *hunch-acceptor*))
(void *hunch-acceptor*))
(defun restart-web (&optional port)
(stop-web)
(sleep 0.1)
(start-web port))
;;; swank
(defun start-swank (&optional port)
(when-it (or port *swank-port*)
(let ((*debug-io* (make-broadcast-stream)))
(swank:create-server :port it
:dont-close t)
(acceptor-log-message *hunch-acceptor* :info
"Started swank at port: ~A." it))))
(defun stop-swank (&optional port)
(when-it (or port *swank-port*)
(swank:stop-server it)
(acceptor-log-message *hunch-acceptor* :info "Stopped swank at port: ~A." it)))
;;; URL routing
(defun parse-url-template (url)
"Split URL into parts of 2 types:
- constant string
- url parameter names (symbols)"
(let ((prev 0)
parts)
(do ((pos (position #\: url) (position #\: url :start prev)))
((or (null pos) (= pos prev))
(push (slice url prev) parts))
(unless (= prev pos)
(push (slice url prev pos) parts))
(:= prev (position-if ^(char= #\/ %) url :start (1+ pos)))
(push (mksym (slice url (1+ pos) prev)) parts)
(if prev
(when (= prev (1+ pos))
(error "Param name is blank in hunch url definition at: ~A" pos))
(return)))
(reverse parts)))
(defmacro url (url-template (&rest params) &body body)
"Define a handler function for URL-TEMPLATE
on top of HUNCHENTOOT:DEFINE-EASY-HANDLER.
The function will be called after HANDLE + <URL-TEMPLATE>
(like HANDLE-/FOO for url '/foo'). If the URL contains parameter names
(basically keywords, like in '/foo/:bar/baz' bar will be a parameter name)
they may be referenced in easy-handler url parameters."
(with-gensyms (req url parts cur pos end)
(let ((url-parts (parse-url-template url-template)))
`(htt:define-easy-handler
(,(mksym url-template :format "handle-~A")
:uri ,(if (rest url-parts)
`(lambda (,req)
(let ((,pos 0)
(,url (htt:request-uri ,req)))
(loop :for ,parts :on ',url-parts :do
(let ((,cur (first ,parts)))
(if (stringp ,cur)
(let ((,end (mismatch ,cur ,url :start2 ,pos)))
(cond
((or (not ,end)
(string= "/" (sub ,cur ,end)))
(return
(not (or (cddr ,parts)
(stringp (second ,parts))))))
((= ,end (length ,cur))
(:+ ,pos ,end))
(t (return))))
(let (,end)
(when-it (and (rest ,parts)
(stringp (second ,parts))
(search (second ,parts) ,url
:start2 ,pos
:test 'string=))
(:= ,end it
,parts (rest ,parts)))
(push (cons (string-downcase (string ,cur))
(sub ,url ,pos ,end))
(? ,req 'htt:get-parameters))
(if (rest ,parts)
(:= ,pos (1+ ,end))
(return t))))))))
url-template))
(,@(remove-if 'stringp params))
,@body))))
(defmacro fmt-url (handler &rest args &key &allow-other-keys)
"Return a string representation of HANDLER's url
with url-parameters substitutted for values of ARGS.
If some parameter is missing, UNBOUND-VARIABLE will be signalled."
`(let (,@(loop :for (var val) :on args :by #'cddr
:collect (list (mksym var) val)))
(strcat ,@(parse-url-template (sub (symbol-name handler)
#.(length "handle-"))))))
(defun print-urls ()
"Print defined urls with their handler functions."
(dolist (record htt::*easy-handler-alist*)
(format t "~A ~A~%" (first record) (third record))))
(defun abort-request (code)
"Abort request with a provided HTTP return CODE."
(:= (htt:return-code*) code)
(htt:abort-request-handler))
;;; main
(when *script*
(load *script* :external-format :utf-8))