Hunchentoot を使っての最低現の Web アプリケーション

Hunchentoot と CL-WHO があれば Web アプリケーションが作れるが、Elephant も仲間に入れよう。

(eval-when (:compile-toplevel :load-toplevel :execute)
  (require :cl-who)
  (require :hunchentoot)
  (require :elephant)
  (use-package :hunchentoot))

;; エンコーディング設定
(setf *hunchentoot-default-external-format* '(:utf-8 :eol-style :lf)
      *default-content-type* "text/html; charset=utf-8")

(setf *dispatch-table*
      (list (create-regex-dispatcher "^/hello.html$" 'hello-page)))

(defmacro with-html (&body body)
  `(cl-who:with-html-output-to-string (*standard-output* nil :indent t)
     ,@body))

(defun hello-page ()
  (with-html
    (:html
     (:head (:title "だいめい"))
     (:body
      (:h1 "まみむめも")
      (:ul
       (mapcan #'(lambda (x)
                   (cl-who:htm
                     (:li (:a :href (url x) (cl-who:str (name x))))))
               (elephant:get-instances-by-class 'link)))))))


(elephant:defpclass link ()
  ((name :initarg :name :accessor name)
   (url :initarg :url :accessor url))
  (:index t))

(defun start ()
  (ensure-directories-exist "/tmp/db/")
  (elephant:open-store '(:bdb "/tmp/db"))
  (start-server :port 7777))

;;(start)
;;(make-instance 'link :name "Google" :url "http://www.google.co.jp")
;;(make-instance 'link :name "Yahoo" :url "http://www.yahoo.co.jp")
;;(make-instance 'link :name "羊" :url "http://www.google.com/search?client=opera&rls=en&q=%E7%BE%8A&sourceid=opera&ie=utf-8&oe=utf-8")

あぁ、やっぱり Slime 上での Common Lisp プログラミングはいやされるな。

cl-dwim なんてプロジェクトが立ち上がってる。
「It can also be described as a RoR killer, but... have you ever heard the word "vapourware"? :)」だって。