下面是我的lisp代码,用来完成一个简单的web服务器。
; 一些辅助函数
(require :asdf)
(defun loadlib (mod)
(asdf:oos 'asdf:load-op mod))
(defun reload ()
(load "web.lisp"))
; load 需要的库
(loadlib :html-template)
(loadlib :hunchentoot)
; 设置 hunchentoot 编码
(defvar *utf-8* (flex:make-external-format :utf-8 :eol-style :lf))
(setq hunchentoot:*hunchentoot-default-external-format* *utf-8*)
; 设置url handler 转发表
(push (hunchentoot:create-prefix-dispatcher "/hello" 'hello) hunchentoot:*dispatch-table*)
; 页面控制器函数
(defun hello ()
(setf (hunchentoot:content-type*) "text/html; charset=utf-8")
(with-output-to-string (stream)
(html-template:fill-and-print-template
#P"/home/chonglinsun/Learn/cl/lib/web/index.tmpl"
(list :name "Lisp程序员")
:stream stream)))
; 启动服务器
(defun start-web (&optional (port 4444))
(hunchentoot:start (make-instance 'hunchentoot:acceptor :port port)))
(defun restart-web ()
(progn
(reload)
(start-web)))index.tmpl的内容如下:
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Lisp Web</title>
</head>
<body>
<h1>Lisp web开发实例</h1>
hi, <!-- TMPL_VAR name -->
</body>
</html>当我启动-web时,我无法访问localhost:4444/hello,但我无法确定问题所在。我在网上搜索,有人说那是因为我的小路。但我不认为在我的代码中有什么与此相关的东西。希望会有人来告诉我为什么。我用的是ubuntu 12.10。
发布于 2012-09-18 05:15:22
您需要使用easy-acceptor而不是acceptor来使用*dispatch-table*机制。
https://stackoverflow.com/questions/12427818
复制相似问题