]> git.pond.sub.org Git - eow/blob - web.lisp
763b6f3a46621d7698aa0daa576fcb3b961060bf
[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 (defun dispatch (request)
34   (let ((script-name (script-name request)))
35     (cond
36       ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request
37       ((or (string-equal script-name +web-root-base+)
38            (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page
39       ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file
40
41
42 (defun start ()
43   (pushnew 'dispatch *dispatch-table* :test #'eq))
44