Merge pull request #205 from shakatoday/api-for-lack-middleware

Api for lack middleware
This commit is contained in:
David Botton 2022-08-09 12:30:10 -04:00 committed by GitHub
commit 47b869f80a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 20 deletions

View file

@ -378,6 +378,7 @@ the default answer. (Private)"
(host "0.0.0.0") (host "0.0.0.0")
(port 8080) (port 8080)
(server :hunchentoot) (server :hunchentoot)
(lack-middleware-list nil)
(extended-routing nil) (extended-routing nil)
(long-poll-first nil) (long-poll-first nil)
(boot-file "/boot.html") (boot-file "/boot.html")
@ -387,8 +388,10 @@ the default answer. (Private)"
(static-root #P"./static-files/")) (static-root #P"./static-files/"))
"Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the "Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the
default route for '/' to establish web-socket connections and static files default route for '/' to establish web-socket connections and static files
located at STATIC-ROOT. The webserver used with CLACK can be chosed with located at STATIC-ROOT. The webserver used with CLACK can be chosen with
:SERVER. If LONG-POLLING-FIRST is t, the output is sent as HTML instead of :SERVER and middlewares prepended with :LACK-MIDDLEWARE-LIST,
NOT supporting LACK.BUILDER DSL.
If LONG-POLLING-FIRST is t, the output is sent as HTML instead of
websocket commands until the end of the on-new-window-handler, if websocket commands until the end of the on-new-window-handler, if
LONG-POLLING-FIRST is a number will keep long polling till that number of LONG-POLLING-FIRST is a number will keep long polling till that number of
queries to browser. LONG-POLLING-FIRST is used in webserver applications to queries to browser. LONG-POLLING-FIRST is used in webserver applications to
@ -452,8 +455,17 @@ the contents sent to the brower."
(setf post-data id))) (setf post-data id)))
(when (equal (getf env :content-type) (when (equal (getf env :content-type)
"application/x-www-form-urlencoded") "application/x-www-form-urlencoded")
(setf post-data (make-string (getf env :content-length))) (setf post-data (cond ((eq (class-name (class-of (getf env :raw-body)))
(read-sequence post-data (getf env :raw-body))) 'circular-streams:circular-input-stream)
(let ((array-buffer (make-array (getf env :content-length)
:adjustable t
:fill-pointer t)))
(read-sequence array-buffer (getf env :raw-body))
(flex:octets-to-string array-buffer)))
(t
(let ((string-buffer (make-string (getf env :content-length))))
(read-sequence string-buffer (getf env :raw-body))
string-buffer)))))
(cond (long-poll-first (cond (long-poll-first
(let ((id (random-hex-string))) (let ((id (random-hex-string)))
(setf (gethash id *connection-data*) (make-hash-table* :test #'equal)) (setf (gethash id *connection-data*) (make-hash-table* :test #'equal))
@ -513,6 +525,11 @@ the contents sent to the brower."
;; Handle Websocket connection ;; Handle Websocket connection
(lambda (env) (lambda (env)
(clog-server env)))) (clog-server env))))
;; Wrap lack middlewares
(setf *app* (reduce #'funcall
lack-middleware-list
:initial-value *app*
:from-end t))
(setf *client-handler* (clack:clackup *app* :server server :address host :port port)) (setf *client-handler* (clack:clackup *app* :server server :address host :port port))
(format t "HTTP listening on : ~A:~A~%" host port) (format t "HTTP listening on : ~A:~A~%" host port)
(format t "HTML root : ~A~%" static-root) (format t "HTML root : ~A~%" static-root)

View file

@ -60,10 +60,12 @@ the same as the clog directy this overides the relative paths used in them.")
(defun initialize (defun initialize
(on-new-window-handler (on-new-window-handler
&rest rest
&key &key
(host "0.0.0.0") (host "0.0.0.0")
(port 8080) (port 8080)
(server :hunchentoot) (server :hunchentoot)
(lack-middleware-list nil)
(extended-routing nil) (extended-routing nil)
(long-poll-first nil) (long-poll-first nil)
(boot-file "/boot.html") (boot-file "/boot.html")
@ -74,8 +76,9 @@ the same as the clog directy this overides the relative paths used in them.")
(asdf:system-source-directory :clog)))) (asdf:system-source-directory :clog))))
"Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the "Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE as the
default route to establish web-socket connections and static files located at default route to establish web-socket connections and static files located at
STATIC-ROOT. The webserver used with CLACK can be chosed with :SERVER. If STATIC-ROOT. The webserver used with CLACK can be chosen with :SERVER and
EXTENDED-ROUTING is t routes will match even if extend with additional / and middlewares prepended with :LACK-MIDDLEWARE-LIST, NOT supporting LACK.BUILDER DSL.
If EXTENDED-ROUTING is t routes will match even if extend with additional / and
additional paths. If LONG-POLLING-FIRST is t then long polling continues until additional paths. If LONG-POLLING-FIRST is t then long polling continues until
the on-new-window-handler ends, if LONG-POLLING-FIRST is a number continues long the on-new-window-handler ends, if LONG-POLLING-FIRST is a number continues long
polling until that number of queries to browser. LONG-POLLING-FIRST is used in polling until that number of queries to browser. LONG-POLLING-FIRST is used in
@ -92,25 +95,26 @@ compiled version. boot-function if set is called with the url and the contents
of boot-file and its return value replaces the contents sent to the brower, this of boot-file and its return value replaces the contents sent to the brower, this
allows adding content for search engine optimization, see tutorial 12 for an allows adding content for search engine optimization, see tutorial 12 for an
example." example."
(declare (ignorable host
port
server
extended-routing
long-poll-first
boot-file
boot-function
static-boot-html
static-boot-js
static-root))
(setf *extended-routing* extended-routing) (setf *extended-routing* extended-routing)
(when on-new-window-handler (when on-new-window-handler
(set-on-new-window on-new-window-handler :path "/" :boot-file boot-file)) (set-on-new-window on-new-window-handler :path "/" :boot-file boot-file))
(unless *clog-running* (unless *clog-running*
(setf *clog-running* t) (setf *clog-running* t)
(setf *static-root* (truename (if *overide-static-root* (setf *static-root* (truename (or *overide-static-root*
*overide-static-root*
static-root))) static-root)))
(clog-connection:initialize #'on-connect (apply #'clog-connection:initialize
:host host (append (list #'on-connect :static-root *static-root*)
:port port rest))))
:server server
:long-poll-first long-poll-first
:extended-routing extended-routing
:boot-file boot-file
:boot-function boot-function
:static-boot-html static-boot-html
:static-boot-js static-boot-js
:static-root *static-root*)))
;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;
;; set-on-new-window ;; ;; set-on-new-window ;;