Gem Grape vs Sinatra 怎么样?

ibachue · 2013年02月19日 · 最后由 debbbbie 回复于 2013年06月08日 · 8204 次阅读

Hi all, 最近我们项目里有个组件用了 Grape 而不是 Rails 来做 REST API 服务器,我顺便学习了一下。发现是个轻量级的,似乎专门只是用来做 REST API 的框架。想请教下大家,这个 Gem 和 Sinatra 比如何,因为两个都非常轻量级而且感觉 Sinatra 可能用的人多一些,功能也更加强大些。

Grape 是一个基于 rack 的 API micro-framework,让你用 DSL 去描述 API 实现,Sinatra 跟 Grape 比较,我个人觉得虽然都能实现 API 服务,但是这两个东西的本身定位不同。

@lgn21st 赞同! Sinatra 是基于 rack 的快速创建 web 应用的 DSL;Sinatra 和 ROR 定位类似,只不过 Sinatra 分装的更简单,Grape 只是个做 RESTful API 的 gem,一般是做些 controller 层的逻辑。

一个是 DSL,一个是 Web Framework,确实没有直接可比性。

提到专门做 API 的框架,祭出大杀器 Goliath

@ashchan

#!/usr/bin/env ruby
$:<< '../lib' << 'lib'

require 'goliath'

其中'$:<< '../lib' << 'lib'是什么意思?

#4 楼 @mobiwolf $:是一个路径变量

@chenge 感谢~~在 irb 中 puts $: 哈哈,得到了路径。

#3 楼 @ashchan 好吧 感谢推荐

The framework is powered by an EventMachine reactor, a high-performance HTTP parser and Ruby 1.9 runtime. One major major advantage Goliath has over other asynchronous frameworks is the fact that by leveraging Ruby fibers introduced in Ruby 1.9+, it can untangle the complicated callback-based code into a format we are all familiar and comfortable with: linear execution, which leads to more maintainable and readable code.
Each Goliath request is executed in its own Ruby fiber and all asynchronous I/O operations can transparently suspend and later resume the processing without requiring the developer to write any additional code. Both request processing and response processing can be done in fully asynchronous fashion: streaming uploads, firehose API's, request/response, and so on.

看上去主要是并行性能比较高效咯

#1 楼 @lgn21st 好吧 不过二者确实很像啊 感觉 Ruby 中几个轻量级 web 框架都挺像的

不是说 rails4 有一个专门做 api 的 controller 来着么?

#9 楼 @ywjno Rails 再怎么样都是很占资源的 不总是推荐。。 而且从兴趣的角度来说也希望尝试不用 Rails 的框架

#9 楼 @ywjno 那个叫 rails-api,是从 rails 抽出来的专供写 API 用的框架,资源战用比 Rails 要小一些。

#10 楼 @iBachue 是的,用不同的框架和工具能开扩眼界、学到新东西:)

对并发要求不高的话用哪个都行,并发要求很高就需要整体架构上考虑了,不光 ruby 层面

需要 登录 后方可回复, 如果你还没有账号请 注册新账号