我对Common (SBCL)和Hunchentoot (使用Quicklisp)比较陌生。有人能告诉我怎样才能让这件事起作用吗?我试图将Hunchentoot服务器和一些路径作为一个单元封装在一个函数中。当我运行它时,只有Hunchentoot的索引页可用,路径/a和/b则不可用。
(defun app0 (port)
(let ((*dispatch-table* nil) (server (make-instance 'hunchentoot:acceptor :port port)))
(push (hunchentoot:create-prefix-dispatcher "/a" (lambda () "a")) *dispatch-table*)
(push (hunchentoot:create-prefix-dispatcher "/b" (lambda () "b")) *dispatch-table*)
(hunchentoot:start server) server))发布于 2017-02-28 17:42:41
据我所见,有许多问题。首先,通过*dispatch-table*进行请求处理需要接受方为easy-acceptor类型,即您必须
(make-instance 'easy-acceptor ...)文档有详细信息。
第二个问题是,在设置代码期间重新绑定*dispatch-table*,并将新值推入该绑定中。由于绑定是在let完成后恢复的(而且hunchentoot:start异步工作),因此在服务器运行时,*dispatch-table*中的条目实际上丢失了。试一试
(push (hunchentoot:create-prefix-dispatcher "/a" (lambda () "a")) *dispatch-table*)
(push (hunchentoot:create-prefix-dispatcher "/b" (lambda () "b")) *dispatch-table*)在顶层(或者在一个专用的设置函数中这样做)。如果不喜欢全局*dispatch-table*方法,还可以创建acceptor的子类,并重写acceptor-dispatch-request (因此,实现任何类型的调度)。
顺便提一句:您没有前缀*dispatch-table*,而在hunchentoot的包中添加了几乎任何其他符号。这仅仅是复制/粘贴错误,还是在实际代码中也是如此?如果您不将hunchentoot包放在代码所在的任何一个包中,那么您还必须将调度表限定为hunchentoot:*dispatch-table*。
编辑(在注释部分中解决这个问题)有一个hunchentoot文档中的示例,它似乎完全按照您想做的做:
(defclass vhost (tbnl:acceptor)
((dispatch-table
:initform '()
:accessor dispatch-table
:documentation "List of dispatch functions"))
(:default-initargs
:address "127.0.0.1"))
(defmethod tbnl:acceptor-dispatch-request ((vhost vhost) request)
(mapc (lambda (dispatcher)
(let ((handler (funcall dispatcher request)))
(when handler
(return-from tbnl:acceptor-dispatch-request (funcall handler)))))
(dispatch-table vhost))
(call-next-method))
(defvar vhost1 (make-instance 'vhost :port 50001))
(defvar vhost2 (make-instance 'vhost :port 50002))
(push
(tbnl:create-prefix-dispatcher "/foo" 'foo1)
(dispatch-table vhost1))
(push
(tbnl:create-prefix-dispatcher "/foo" 'foo2)
(dispatch-table vhost2))
(defun foo1 () "Hello")
(defun foo2 () "Goodbye")
(tbnl:start vhost1)
(tbnl:start vhost2)(为简洁起见,删除文档中的注释)。tbnl是包hunchentoot的预定义昵称。你可以交替使用这两种方法,不过我建议你选择一种,并坚持使用。两者混合可能会造成混乱。
https://stackoverflow.com/questions/42514994
复制相似问题