]> git.pond.sub.org Git - eow/blob - web.lisp
093769224ec938c3d91b3669e6250be4ec0959fc
[eow] / web.lisp
1 (in-package :empire-web)
2
3 (declaim (optimize (space 0) (speed 0) (safety 3) (debug 3)))
4
5
6 (defvar *this-file* (load-time-value
7                      (or #.*compile-file-pathname* *load-pathname*)))
8
9 (defvar *this-dir* (make-pathname :host (pathname-host *this-file*)
10                                   :device (pathname-device *this-file*)
11                                   :directory (pathname-directory *this-file*)))
12
13 (defparameter +templates-root+ (namestring *this-dir*))
14 (defparameter +root-url+ "/eow/static/test.html")
15 (defparameter +web-root-base+ "/eow")
16 (defparameter +web-root+ (concatenate 'string +web-root-base+ "/"))
17 (defparameter +static-web-root+ (concatenate 'string +web-root+ "static/"))
18 (defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/"))
19
20 (defun string-starts-with (string prefix)
21   ;; (from Hunchentoot)
22   (let ((mismatch (mismatch string prefix :test #'char=)))
23     (or (null mismatch)
24         (>= mismatch (length prefix)))))
25
26 (defun serve-static ()
27   "Handle a request for a file under static/ directory"
28   (let* ((script-name (script-name))
29          (fname (subseq script-name (length +static-web-root+)))
30          (fullname (concatenate 'string +static-files-root+ fname)))
31     (handle-static-file fullname)))
32
33 (defvar *update-queue* (locked-queue:create))
34
35 (defun update ()
36   "Send stream of updates to client"
37   (locked-queue:dequeue *update-queue*))
38
39 (defun send (string)
40   "Push a javascript update fragment to the client."
41   (locked-queue:enqueue *update-queue* string))
42
43 (defun dispatch (request)
44   (let ((script-name (script-name request)))
45     (cond
46       ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
47       ((string= script-name "/eow/update") 'update)
48       ((or (string-equal script-name +web-root-base+)
49            (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page
50       ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
51
52
53 (defun start ()
54   (pushnew 'dispatch *dispatch-table* :test #'eq))
55