From 26149be9cdf416550bd80f06b02e956bb1b0bf52 Mon Sep 17 00:00:00 2001 From: Gerd Flaig Date: Sun, 23 Nov 2008 01:40:41 +0100 Subject: [PATCH] add basic web module --- web.lisp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 web.lisp diff --git a/web.lisp b/web.lisp new file mode 100644 index 0000000..763b6f3 --- /dev/null +++ b/web.lisp @@ -0,0 +1,44 @@ +(in-package :empire-web) + +(declaim (optimize (space 0) (speed 0) (safety 3) (debug 3))) + + +(defvar *this-file* (load-time-value + (or #.*compile-file-pathname* *load-pathname*))) + +(defvar *this-dir* (make-pathname :host (pathname-host *this-file*) + :device (pathname-device *this-file*) + :directory (pathname-directory *this-file*))) + +(defparameter +templates-root+ (namestring *this-dir*)) +(defparameter +root-url+ "/eow/static/test.html") +(defparameter +web-root-base+ "/eow") +(defparameter +web-root+ (concatenate 'string +web-root-base+ "/")) +(defparameter +static-web-root+ (concatenate 'string +web-root+ "static/")) +(defparameter +static-files-root+ (concatenate 'string +templates-root+ "static/")) + +(defun string-starts-with (string prefix) + ;; (from Hunchentoot) + (let ((mismatch (mismatch string prefix :test #'char=))) + (or (null mismatch) + (>= mismatch (length prefix))))) + +(defun serve-static () + "Handle a request for a file under static/ directory" + (let* ((script-name (script-name)) + (fname (subseq script-name (length +static-web-root+))) + (fullname (concatenate 'string +static-files-root+ fname))) + (handle-static-file fullname))) + +(defun dispatch (request) + (let ((script-name (script-name request))) + (cond + ((not (string-starts-with script-name +web-root+)) nil) ; do not handle this request + ((or (string-equal script-name +web-root-base+) + (string-equal script-name +web-root+)) (redirect +root-url+)) ; go to the start page + ((string-starts-with script-name +static-web-root+) 'serve-static)))) ; serve static file + + +(defun start () + (pushnew 'dispatch *dispatch-table* :test #'eq)) + -- 2.43.0