(require 'asdf)
(asdf:load-system :cl-http-server))
-(defvar *tlbw-server* nil)
+;; Helpful high-level stuff
+(defmacro tlbw-dispatcher (type uri dispatcher)
+ `(cl-http:add-route
+ cl-http:*server* ,type ,uri ,dispatcher))
+
+(defmacro tlbw-static (type uri file)
+ `(tlbw-dispatcher ,type ,uri
+ #'(lambda ()
+ (cl-http:serve-file
+ (merge-pathnames (pathname ,file)
+ (namestring (cl-http:public-dir)))))))
+
+(defun tlbw-setup ()
+ (assert cl-http:*server* )
+
+ (tlbw-static :static "/" "index.html")
+ (tlbw-static :static "/about" "about.html")
+ (tlbw-static :regex "/archive" "archive.html")
+ (tlbw-static :regex "/rss" "rss.xml")
+
+ ;; Redirect old URIs
+ (tlbw-dispatcher :regex "^/index\(.html\)?$"
+ #'(lambda () (cl-http:redirect "/")))
+ (tlbw-dispatcher :static "/about.html"
+ #'(lambda () (cl-http:redirect "/about")))
+ (tlbw-dispatcher :static "/archive.html"
+ #'(lambda () (cl-http:redirect "/archive")))
+ (tlbw-dispatcher :regex "^/tags/?$"
+ #'(lambda () (cl-http:redirect "/archive")))
+ (tlbw-dispatcher :static "/rss.xml"
+ #'(lambda () (cl-http:redirect "/rss"))))
+
+(defun tlbw-unsetup ()
+ (assert cl-http:*server*)
+ (let ((static-route (cl-http:route-static
+ (cl-http:server-route cl-http:*server*)))
+ (regex-route (cl-http:route-regex
+ (cl-http:server-route cl-http:*server*))))
+ (maphash #'(lambda (k _)
+ (declare (ignore _))
+ (remhash k static-route)) static-route)
+ (maphash #'(lambda (k _)
+ (declare (ignore _))
+ (remhash k regex-route)) regex-route)))
+
+ ;; (cl-http:defpage index ()
+ ;; (cl-http:serve-file
+ ;; (merge-pathnames #p"index.html"
+ ;; (namestring (cl-http:public-dir)))))
(defun tlbw-start-server ()
;; Check whether the server is running
- (when *tlbw-server*
+ (when cl-http:*server*
(format t "TLBW server is already running:~%~s~%"
- *tlbw-server*)
+ cl-http:*server*)
(return-from tlbw-start-server))
;; Alles gutes
- (setq *tlbw-server*
- (cl-http-server:start-server
- (cl-http-server:make-server :public-dir *lbs-site*
- :port 8000))))
+ (setq cl-http:*server*
+ (cl-http:start-server
+ (cl-http:make-server :public-dir *lbs-site*
+ :port 8000)))
+ (tlbw-setup)
+ cl-http:*server*)
+
+(defun tlbw-reload-server ()
+ ;; Check whether the server is running
+ (when (not cl-http:*server*)
+ (format t "TLBW server not running.")
+ (return-from tlbw-reload-server))
+
+ ;; We don't kill the server, we just re-setup its routes.
+ (tlbw-unsetup)
+ (tlbw-setup)
+ cl-http:*server*)
+
(defun tlbw-stop-server ()
;; Check whether the server is running
- (when (not *tlbw-server*)
+ (when (not cl-http:*server*)
(format t "TLBW server not running.")
(return-from tlbw-stop-server))
- (cl-http-server:stop-server *tlbw-server*)
- (setq *tlbw-server* nil))
+ (cl-http:stop-server cl-http:*server*)
+ (setq cl-http:*server* nil))
+
;; A demo. First off, we load lib:
;;