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")
(port 8080)
(server :hunchentoot)
(lack-middleware-list nil)
(extended-routing nil)
(long-poll-first nil)
(boot-file "/boot.html")
@ -387,8 +388,10 @@ the default answer. (Private)"
(static-root #P"./static-files/"))
"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
located at STATIC-ROOT. The webserver used with CLACK can be chosed with
:SERVER. If LONG-POLLING-FIRST is t, the output is sent as HTML instead of
located at STATIC-ROOT. The webserver used with CLACK can be chosen with
: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
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
@ -452,8 +455,17 @@ the contents sent to the brower."
(setf post-data id)))
(when (equal (getf env :content-type)
"application/x-www-form-urlencoded")
(setf post-data (make-string (getf env :content-length)))
(read-sequence post-data (getf env :raw-body)))
(setf post-data (cond ((eq (class-name (class-of (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
(let ((id (random-hex-string)))
(setf (gethash id *connection-data*) (make-hash-table* :test #'equal))
@ -513,6 +525,11 @@ the contents sent to the brower."
;; Handle Websocket connection
(lambda (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))
(format t "HTTP listening on : ~A:~A~%" host port)
(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
(on-new-window-handler
&rest rest
&key
(host "0.0.0.0")
(port 8080)
(server :hunchentoot)
(lack-middleware-list nil)
(extended-routing nil)
(long-poll-first nil)
(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))))
"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
STATIC-ROOT. The webserver used with CLACK can be chosed with :SERVER. If
EXTENDED-ROUTING is t routes will match even if extend with additional / and
STATIC-ROOT. The webserver used with CLACK can be chosen with :SERVER 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
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
@ -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
allows adding content for search engine optimization, see tutorial 12 for an
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)
(when on-new-window-handler
(set-on-new-window on-new-window-handler :path "/" :boot-file boot-file))
(unless *clog-running*
(setf *clog-running* t)
(setf *static-root* (truename (if *overide-static-root*
*overide-static-root*
(setf *static-root* (truename (or *overide-static-root*
static-root)))
(clog-connection:initialize #'on-connect
:host host
:port port
: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*)))
(apply #'clog-connection:initialize
(append (list #'on-connect :static-root *static-root*)
rest))))
;;;;;;;;;;;;;;;;;;;;;;;
;; set-on-new-window ;;