在Clojure中开发RESTful Web服务的好起点是什么? [英] What is the good starting point to developing RESTful web service in Clojure?
问题描述
我正在寻找轻量级的东西,至少应该支持以下功能:
- 支持轻松定义操作元数据
- 从请求中提取参数到clojure地图中或作为函数参数的包装器
- 支持多种形式的认证(basic,form,cookie)
- 基于api方法元数据的基本授权
- 包含在clojure地图中的会话对象
- (不需要重新启动服务器)
- 自动序列化json和xml的返回值
- 有很好的可插入的URL参数处理/ par1 / par2而不是/ action?par1 = val1& par2 = val2)
我知道滚动我自己微框架为这些选项的每一个,但为什么重新发明轮子如果这样的事情已经存在?特别是如果是:
- 活动项目中贡献者/用户数量不断增加
- 基本文档和教程。
首先,我认为你不太可能找到一个单一的收缩包装解决方案做所有这一切在Clojure(除了以Java库的形式通过interop使用)。什么是Clojure的标准Web堆栈包括许多图书馆,人们以各种方式混合和匹配(因为他们高兴地倾向于完全兼容)。 1
以下是您可能会发现有用的一些构建块列表:
-
Ring - Clojure的基本HTTP请求处理库;所有其他webby库(用于写路由& c),我知道是与环兼容。 Ring正在积极开发,有一个健壮的社区,是非常好的写作,并有一个很好的SPEC文档详细的其设计理念。 此博文提供了一个很好的
-
Sandbar - 目前是一个认证库,计划的更多类型的功能;
-
Compojure - 一个成熟和健壮的图书馆,提供了一个很好的DSL写入路线,使用在环的顶部。
-
Compojure-rest - 用于在 Compojure之上构建RESTful应用程序的库 a>。就我所知,Compojure-rest在其发展的早期阶段;也许你可能认为这是一个影响其设计的机会。 : - )
-
对于处理XML,有
clojure.contrib.lazy-xml
帮助库clojure.contrib.zip-filter.xml
)和 Enlive (内置的clojure.xml
命名空间目前不是很实用); -
对于JSON,有一个库在contrib和 clojure-json (和我认为有至少一个其他lib我似乎忘记了现在...);
所有这一切都将非常满意REPL驱动的开发风格接受此SO问题的答案非常符合这里的目的)。我想上面的链接集合确实留下了一些盲点(特别是,认证故事仍然被解决,据我所知),但希望这是一个好的开始。
1 我知道在Clojure中构建webapps的唯一单包解决方案是 Conjure ,灵感来自Rails;不幸的是,我不得不承认我不太了解它,所以如果你感兴趣,请跟随链接,看看周围的来源,wiki& c。
I am looking into something lightweight, that, at a minimum should support the following features:
- Support for easy definition of actions through metadata
- Wrapper that extracts parameters from request into clojure map, or as function parameters
- Support for multiple forms of authentication (basic, form, cookie)
- basic authorization based of api method metadata
- session object wrapped in clojure map
- live coding from REPL (no need to restart server)
- automatic serialization of return value to json and xml
- have nice (pluggable) url parameter handling (eg /action/par1/par2 instead of /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:
- Active project with rising number of contributors/users
- Have at least basic documentation and tutorial online.
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'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 -- currently an authentication library, more types of functionality planned; under development.
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 -- "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. :-)
For dealing with XML, there's
clojure.contrib.lazy-xml
(and the helper libraryclojure.contrib.zip-filter.xml
) and Enlive (the built-inclojure.xml
namespace is currently not very usable); these would be used in tandem (though for your purposes the former might suffice).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.
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.
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屋!