Aserve & CL-EMB

Aserve でテンプレートエンジンの CL-EMB を使ってみました。

hello.tmplを作成

<html>
<head><title>テストページ</title></head>
<body>
<% @var name %>
</body>
</html>

Lipsコード

(defun to-octets-string (str)
  (let* ((arr (sb-ext:string-to-octets str
	:external-format sb-impl::*default-external-format*
	:null-terminate nil))

	 (len (length arr))
	 (ret (make-array len :fill-pointer 0 :element-type 'character)))
    (dotimes (i len) (vector-push (code-char (elt arr i)) ret))
    ret))

(defun MB (str)
  (princ (to-octets-string str)))

(net.aserve:publish
 :path "/hello"
 :content-type "text/html; charset=UTF-8"
 :function
 #'(lambda (req ent)
     (net.aserve:with-http-response (req ent)
       (net.aserve:with-http-body (req ent)
	 (format (net.aserve:request-reply-stream req)
		 (MB (emb:execute-emb #P"/home/lisp/hello.tmpl"
				      :env '(:name "こんにちは!"))))))))

Aserve & Swank によるホットデプロイ

サーバ側

(ql:quickload "aserve")
(ql:quickload :swank)

(defpackage cl-web
  (:use :cl
        :swank
        :net.aserve))

(in-package :cl-web)

(require :aserve)
(net.aserve:start :port 5000)

(net.aserve:publish
 :path "/"
 :content-type "text/html; charset=UTF-8"
 :function
 #'(lambda (req ent)
     (net.aserve:with-http-response
         (req ent)
       (net.aserve:with-http-body
           (req ent)
         (net.html.generator:html
           (:html
             (:head (:title "Hello world!"))
             (:body "Hello world!")))))))

(swank:create-server :port 4005 :style :spawn :dont-close t)

クライアント側
emacs で Alt + x slime-connect
Host: 127.0.0.1
Port: 4005

(in-package :cl-web)

(net.aserve:publish
 :path "/"
 :content-type "text/html; charset=UTF-8"
 :function
 #'(lambda (req ent)
     (net.aserve:with-http-response
         (req ent)
       (net.aserve:with-http-body
           (req ent)
         (net.html.generator:html
           (:html
             (:head (:title "Hello world!!!!!"))
             (:body "Hello world!!!!!")))))))

Aserveで日本語(マルチバイト)表示

Aserveで日本語(マルチバイト)を表示しようとするとできなかったのでこちらのサイトを参考にしました。

http://hirodanjyuro.blogspot.jp/2010/06/blog-post.html

(defun to-octets-string (str)
  (let* ((arr (sb-ext:string-to-octets str
        :external-format sb-impl::*default-external-format*
        :null-terminate nil))
         (len (length arr))
         (ret (make-array len :fill-pointer 0 :element-type 'character)))
    (dotimes (i len) (vector-push (code-char (elt arr i)) ret))
    ret))

(defmacro MB (str)
  `(:princ (to-octets-string ,str)))

(net.aserve:publish
 :path "/"
 :content-type "text/html; charset=UTF-8"
 :function
 #'(lambda (req ent)
     (net.aserve:with-http-response
         (req ent)
       (net.aserve:with-http-body
           (req ent)
         (net.html.generator:html
           (:html
             (:head (:title (MB "こんにちは!")))
             (:body (MB "こんにちは!")
                    :br (MB "你好!"))))))))

問題なく中国語も表示されます。

ApacheとAserveの連携

http://xxx.xxx.xxx/cl/にアクセスしたときにポート5000でリッスンしているAserveと連携するよう設定します。

Apacheの設定ファイル /etc/httpd/conf/httpd.conf の最後に以下の2行を追加しApache再起動します。

ProxyPass /cl/ http://localhost:5000/
ProxyPassReverse /cl/ http://localhost:5000/

AserveでHello World!

Aserveを使ってHTMLを生成してページを表示します。

(ql:quickload "aserve")
(require :aserve)
(net.aserve:start :port 5000)

(net.aserve:publish
 :path "/"
 :content-type "text/html; charset=UTF-8"
 :function
 #'(lambda (req ent)
     (net.aserve:with-http-response
         (req ent)
       (net.aserve:with-http-body
           (req ent)
         (net.html.generator:html
           (:html
             (:head (:title "Hello World!"))
             (:body "Hello World!")))))))

Common Lisp開発環境の構築

ASUS EeeBook Windows 10 32bit環境にCommon Lispの環境を構築

1. Clozure CLのインストール
http://ccl.clozure.com/

ダウンロードページよりWindows用のzipをダウンロードし解凍する。解凍後、環境変数のPathに追加する。

解凍場所
C:\Program Files\ccl

2. NTEmacs / Emacs for Windowsのインストール
http://cha.la.coocan.jp/doc/NTEmacs.html

ダウンロードし解凍する。

解凍場所
C:\Program Files\emacs

3. emacsのフォント設定
Options -> Set Default Font でフォントの設定。
これにより以下のファイルが作成される。
C:\Users\ユーザー名\AppData\Roaming\.emacs

4. emacsの設定ファイル(.emacs)に設定追加
C:\Users\ユーザー名\AppData\Roaming\.emacs

;; Common Lisp処理系を設定
(setq inferior-lisp-program "wx86cl")
;; SLIMEのロード
(require 'slime)
(slime-setup '(slime-repl slime-fancy slime-banner))

5. SLIMEのインストール
https://common-lisp.net/project/slime/

ダウンロドしたファイルを下記の場所に解凍

C:\Program Files\emacs\share\emacs\site-lisp\slime

6. emacsとSLIMEの起動

emacs起動
C:\Program Files\emacs\share\emacs\bin\emacs.exe

SLIME起動
M-x slime

以下のようにコンソールが出れば起動成功
; SLIME 2015-08-24
CL-USER>