0

我正在尝试为 hunchentoot 服务器在 common lisp 中编写类似于 ruby​​ on rails 的路由文件,即

(route "/home" :controller home :action index)

示例控制器文件如下所示:

;;; controller file

(defun index ()
  ;; do something
  )

(defun add (x)
  ;; do something
  )

这样做的原因是将控制器与视图分开。

所以,我有以下功能:

(defun build-handler (controller action)
  (intern (concatenate 'string (symbol-name controller) "-" (symbol-name action))))

(defun format-view-file (controller params)
  (let ((fstring (read-from-view-file))) ; the view file must be named after the controller. the details are not shown here
    (format nil fstring params))) 

(defun get-action-arguments (f)
  ;; read the controller file 
  ;; and find the action function
  ;; return a list of its arguments
  ;; something like this, in case f was "(defun bar (x) (1+ x))"
  (car (cdr (cdr f))))

和宏:

(defmacro route (uri &key controller action)
  (let ((var (build-handler controller action))
        (params (get-action-arguments action)))
    `(hunchentoot:define-easy-handler (,var :uri ,uri) ,params
                      (setf (hunchentoot:content-type*) "text/plain")
                      (format-view-file ,params))))

我的问题是我无法正确传递参数。参数应该如何在路由宏中?

或者,是否有更好的方法来完成此任务,或者是否有一个在 hunchentoot 之上工作的库(我找到了一些但不知道选择哪个,所以我开始编写自己的)。

非常感谢您!

4

1 回答 1

3

只是几个指针,还有更多:

有一个很好的服务器抽象层,称为缺乏:https ://github.com/fukamachi/lack 。

有了它,您可以非常简单地构建自己的路由机制,或使用现有的 ningle 或 Caveman。

还有RESTAS,它直接构建在 Hunchentoot 之上。

至于你的想法:我强烈建议不要使用语言机制的文件加载/文件名修改魔法。您应该能够通过简单地加载顶级表单来表达任何内容,以便您可以在系统定义中布局整个应用程序结构,最重要的是,保持简单的基于图像的开发周期。您的代码不需要知道它存储在文件中。

于 2016-09-07T08:31:17.603 回答