[Common Lisp]Common LispのWebアプリケーション環境 hunchentootの使い方メモ(1)

SBCL/ubuntu8.04を想定。

参考URL:
http://techneet.blog39.fc2.com/blog-entry-4.html

インストール

(require :asdf-install)
(asdf-install:install :hunchentoot)

gpg-check(署名チェック)ができないけどどうするか?みたいな質問が出るが、とりあえずスキップでOK。ubuntu環境にはgpgがインストールされているはずなのに、なぜか呼ばれない・・・。

テストページ表示

;; Webアプリケーションフレームワーク
(require :hunchentoot)
;; S式→HTML生成 ライブラリ
(require :cl-who)

;; サーバ起動
(setq *server* (hunchentoot:start-server :port 8080))

;; ログファイル指定
(setf (hunchentoot:log-file) "./server.log")

;; 出力フォーマット指定 (これがないと日本語でエラー)
(setq hunchentoot:*hunchentoot-default-external-format*
      (flex:make-external-format :utf-8 :eol-style :lf))

;; デフォルト Content-Type の指定 (これがないとブラウザが文字コードを誤認識)
(setq hunchentoot:*default-content-type* "text/html; charset=utf-8")

;; URLマッピング
(setq hunchentoot:*dispatch-table*
      (list (hunchentoot:create-regex-dispatcher "^/$" 'hello-world)))

;; ハンドラ定義
(defun hello-world ()
  (cl-who:with-html-output-to-string
      (str nil :prologue t)
    (:html (:head (:title "ほげほげ"))
	   (:body (:h1 "ほげほげのページ")))))

これでhttp://localhost:8080にアクセスすれば、ページが表示されるはず。