在 Clojure 中开发 RESTful Web 服务的良好起点是什么? [英] What is the good starting point to developing RESTful web service in Clojure?
问题描述
我正在研究一些轻量级的东西,至少应该支持以下功能:
I am looking into something lightweight, that, at a minimum should support the following features:
- 支持通过元数据轻松定义操作
- 从请求中提取参数到 clojure 映射或作为函数参数的包装器
- 支持多种形式的身份验证(基本、表单、cookie)
- 基于api方法元数据的基本授权
- 封装在 clojure 映射中的会话对象
- 从 REPL 实时编码(无需重启服务器)
- 返回值自动序列化为json和xml
- 有很好的(可插入的)url 参数处理(例如/action/par1/par2 而不是/action?par1=val1&par2=val2)
我知道为这些选项中的每一个滚动我自己的微框架相对容易,但是如果已经存在类似的东西,为什么要重新发明轮子呢?特别是如果它是:
I know it is relatively easy to roll my own micro-framework for each one of these options, but why reinvent the wheel if something like that already exists? Especially if it is:
- 活跃的项目,贡献者/用户数量不断增加
- 至少有基本的在线文档和教程.
推荐答案
首先,我认为您不太可能在 Clojure 中找到一个单一的收缩包装解决方案来完成所有这些工作(除非以 Java 库的形式)通过互操作使用).Clojure 的标准 Web 堆栈由许多库组成,人们以各种方式混合和匹配(因为它们很乐意完美兼容).1
First of all, I think that you are unlikely to find a single shrinkwrapped solution to do all this in Clojure (except in the form of a Java library to be used through interop). What is becoming Clojure's standard Web stack comprises a number of libraries which people mix and match in all sorts of ways (since they happily tend to be perfectly compatible).1
这里列出了一些您可能会觉得有用的构建块:
Here's a list of some building blocks which you might find useful:
Ring -- Clojure 的基本 HTTP 请求处理库;我知道的所有其他 webby 库(用于编写路由和 c.)都与 Ring 兼容.Ring 正在积极开发中,拥有强大的社区,编写得非常好,并且有一个很好的 SPEC 文档,详细说明了其设计理念.这篇博文提供了一个很好的如何使用它的示例(对 GitHub 提交做出反应).
Ring -- Clojure's basic HTTP request handling library; all the other webby libraries (for writing routes &c.) that I know of are compatible with Ring. Ring is being actively developed, has a robust community, is very well-written and has a nice SPEC document detailing its design philosophy. This blog post provides a nice example of how it might be used (reacting to GitHub commits).
Sandbar -- 目前是一个身份验证库,正在计划更多类型的功能;正在开发中.
Sandbar -- currently an authentication library, more types of functionality planned; under development.
Compojure -- 一个成熟而健壮的库,为编写路由提供了很好的 DSL用于 Ring 之上.这将为您提供很好的 URL 参数处理.
Compojure -- a mature and robust library which provides a nice DSL for writing routes to be used on top of Ring. This will give you the nice URL parameter handling.
Compojure-rest -- "一个用于在顶层构建 RESTful 应用程序的库Compojure".据我所知,Compojure-rest 还处于开发的早期阶段.也许您可能会将此视为影响其设计的机会.:-)
Compojure-rest -- "a library for building RESTful applications on top of Compojure". Compojure-rest is, as far as I can tell, in its early stages of development; perhaps you might see this as an opportunity to influence its design. :-)
为了处理 XML,有 clojure.contrib.lazy-xml
(和辅助库 clojure.contrib.zip-filter.xml
)和 Enlive(内置的 clojure.xml
命名空间目前不太好用);这些将串联使用(尽管对于您的目的,前者可能就足够了).
For dealing with XML, there's clojure.contrib.lazy-xml
(and the helper library clojure.contrib.zip-filter.xml
) and Enlive (the built-in clojure.xml
namespace is currently not very usable); these would be used in tandem (though for your purposes the former might suffice).
对于 JSON,contrib 中有一个库和 clojure-json(和我认为至少还有一个我现在似乎忘记了的其他库......);选择你最喜欢的那个.
For JSON, there a library in contrib and clojure-json (and I think there was at least one other lib I seem to be forgetting now...); pick the one you like best.
所有人都会对 REPL 驱动的开发风格非常满意(请参阅对 this SO question 用于 Ring 技巧,这非常符合此处的目的).我想上面的链接集合确实留下了一些盲点(特别是,据我所知,身份验证的故事仍在解决中),但希望这是一个好的开始.
All of will be perfectly happy with a REPL-driven development style (see the accepted answer to this SO question for a Ring trick which is very much to the purpose here). I suppose the above collection of links does leave a few blind spots (in particular, the authentication story is still being ironed out, as far as I can tell), but hopefully it's a good start.
1我所知道的在 Clojure 中构建 web 应用程序的唯一单包解决方案是 Conjure,灵感来自 Rails;不幸的是,我不得不承认我对此知之甚少,所以如果您有兴趣,请点击链接并查看来源、wiki &c.
1The only single-package solution for building webapps in Clojure that I know of is Conjure, inspired by Rails; unfortunately I have to admit that I don't know much about it, so if you feel interested, follow the link and look around the sources, wiki &c.
这篇关于在 Clojure 中开发 RESTful Web 服务的良好起点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!