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 层的逻辑。
#!/usr/bin/env ruby
$:<< '../lib' << 'lib'
require 'goliath'
其中'$:<< '../lib' << 'lib'是什么意思?
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.
看上去主要是并行性能比较高效咯