Added multiple paths and default routes.

This commit is contained in:
David Botton 2021-01-19 18:48:24 -05:00
parent e5d1aa6d05
commit 8a62348edd
2 changed files with 69 additions and 31 deletions

View file

@ -29,6 +29,7 @@ script."
(initialize function) (initialize function)
(shutdown-clog function) (shutdown-clog function)
(set-on-connect function) (set-on-connect function)
(set-clog-path function)
(get-connection-data function) (get-connection-data function)
"CLOG system utilities" "CLOG system utilities"
@ -78,6 +79,8 @@ script."
(defvar *queries-sems* (make-hash-table) "Query ID to semiphores") (defvar *queries-sems* (make-hash-table) "Query ID to semiphores")
(defvar *query-time-out* 3 "Number of seconds to timeout waiting for a query") (defvar *query-time-out* 3 "Number of seconds to timeout waiting for a query")
(defvar *url-to-boot-file* (make-hash-table :test 'equalp) "URL to boot-file")
;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;
;; generate-id ;; ;; generate-id ;;
;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;
@ -230,23 +233,31 @@ the default answer. (Private)"
(port 8080) (port 8080)
(boot-file "/boot.html") (boot-file "/boot.html")
(static-root #P"./static-files/")) (static-root #P"./static-files/"))
"Inititalze CLOG on a socket using HOST and PORT to serve BOOT-FILE as "Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as
the default route to establish web-socket connections and static files the default route for '/' to establish web-socket connections and static files
located at STATIC-ROOT." located at STATIC-ROOT. If BOOT-FILE is nil no initial clog-path's will be
setup, use clog-path to add. The on-connect-handler needs to indentify the
path by querying the browser. See PATH-NAME (CLOG-LOCATION)."
(set-on-connect on-connect-handler) (set-on-connect on-connect-handler)
(when boot-file
(set-clog-path "/" boot-file))
(setf *app* (setf *app*
(lack:builder (lack:builder
(:static :path (lambda (path) (:static :path (lambda (path)
(let ((clog-path (gethash path *url-to-boot-file*)))
(cond ((ppcre:scan "^(?:/clog$)" path) nil) (cond ((ppcre:scan "^(?:/clog$)" path) nil)
((equal path "/") boot-file) (clog-path clog-path)
(t path))) (t path))))
:root static-root) :root static-root)
(lambda (env) (lambda (env)
(clog-server env)))) (clog-server env))))
(setf *client-handler* (clack:clackup *app* :address host :port port)) (setf *client-handler* (clack:clackup *app* :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)
(format t "Boot file default : ~A~%" boot-file)) (format t "Boot file for path / : ~A~%" boot-file))
;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;
;; shutdown-clog ;; ;; shutdown-clog ;;
@ -259,6 +270,7 @@ located at STATIC-ROOT."
(clrhash *connection-data*) (clrhash *connection-data*)
(clrhash *connections*) (clrhash *connections*)
(clrhash *connection-ids*)) (clrhash *connection-ids*))
(clrhash *url-to-boot-file*)
(setf *app* nil) (setf *app* nil)
(setf *client-handler* nil)) (setf *client-handler* nil))
@ -270,6 +282,15 @@ located at STATIC-ROOT."
"Change the ON-CONNECTION-HANDLER set during Initialize." "Change the ON-CONNECTION-HANDLER set during Initialize."
(setf *on-connect-handler* on-connect-handler)) (setf *on-connect-handler* on-connect-handler))
;;;;;;;;;;;;;;;;;;;
;; set-clog-path ;;
;;;;;;;;;;;;;;;;;;;
(defun set-clog-path (path boot-file)
(if boot-file
(setf (gethash path *url-to-boot-file*) boot-file)
(remhash path *url-to-boot-file*)))
;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;
;; escape-string ;; ;; escape-string ;;
;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;

View file

@ -13,17 +13,26 @@
;; Implementation - CLOG System ;; Implementation - CLOG System
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *url-to-on-new-window* (make-hash-table :test 'equalp)
"URL to on-new-window handlers")
(defvar *clog-running* nil "If clog running.")
;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;
;; initialize ;; ;; initialize ;;
;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;
(defvar *on-new-window* nil "Store the on-new-window handler")
(defun on-connect (connection-id) (defun on-connect (connection-id)
(when cc:*verbose-output* (when cc:*verbose-output*
(format t "Start new window handler on connection-id - ~A" connection-id)) (format t "Start new window handler on connection-id - ~A" connection-id))
(let ((body (make-clog-body connection-id))) (let ((body (make-clog-body connection-id)))
(funcall *on-new-window* body))) (let* ((path (path-name (location body)))
(on-new-window (or (gethash path *url-to-on-new-window*)
(gethash "default" *url-to-on-new-window*)
(gethash "/" *url-to-on-new-window*))))
(if on-new-window
(funcall on-new-window body)
(put-br (html-document win) "No route to on-new-window")))))
(defun initialize (on-new-window-handler (defun initialize (on-new-window-handler
&key &key
@ -35,27 +44,34 @@
the default route to establish web-socket connections and static files the default route to establish web-socket connections and static files
located at STATIC-ROOT. If CLOG was already initialized and not shut located at STATIC-ROOT. If CLOG was already initialized and not shut
down, this function does the same as set-on-new-window. If the variable down, this function does the same as set-on-new-window. If the variable
clog:*overide-static-root* is set STATIC-ROOT will be ignored." clog:*overide-static-root* is set STATIC-ROOT will be ignored. If BOOT-FILE
(if *on-new-window* is nil no default boot-file will be set for /."
(set-on-new-window on-new-window-handler)
(progn
(set-on-new-window on-new-window-handler)
(set-on-new-window on-new-window-handler :path "/" :boot-file boot-file)
(unless *clog-running*
(setf *clog-running* t)
(cc:initialize #'on-connect (cc:initialize #'on-connect
:host host :host host
:port port :port port
:boot-file boot-file :boot-file boot-file
:static-root (if (boundp '*overide-static-root*) :static-root (if (boundp '*overide-static-root*)
*overide-static-root* *overide-static-root*
static-root))))) static-root))))
;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;
;; set-on-new-window ;; ;; set-on-new-window ;;
;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;
(defun set-on-new-window (on-new-window-handler) (defun set-on-new-window (on-new-window-handler
"Change the on-new-window handler." &key (path "/") (boot-file "/boot.html"))
(setf *on-new-window* on-new-window-handler)) "Set or change the on-new-window handler or set a new one for PATH
using BOOT_FILE. If PATH is set to default will use boot-file when
the path can not be determined."
(cc:set-clog-path path boot-file)
(if boot-file
(setf (gethash path *url-to-on-new-window*) on-new-window-handler)
(remhash path *url-to-on-new-window*)))
;;;;;;;;;;;;;; ;;;;;;;;;;;;;;
;; shutdown ;; ;; shutdown ;;
@ -63,5 +79,6 @@ clog:*overide-static-root* is set STATIC-ROOT will be ignored."
(defun shutdown () (defun shutdown ()
"Shutdown CLOG." "Shutdown CLOG."
(set-on-new-window nil) (clrhash *url-to-on-new-window*)
(setf *clog-running* nil)
(cc:shutdown-clog)) (cc:shutdown-clog))