从 ruby on Rails 应用程序提供 XML-RPC 服务?
我正在研究从现有 ruby on Rails 应用程序提供 XML-RPC 服务的可行性。我希望我可以通过实现一些额外的控制器方法并通过我现有的 apache/passenger 设置来提供服务来做到这一点。这种方法是否可行,或者 XML-RPC 是否需要单独的 Web 服务器?
I'm looking at the feasibility of providing an XML-RPC service from an existing ruby on rails application. I'm hoping that I can do it by just implementing some additional controller methods and serving it through my existing apache/passenger setup. Is this approach plausible, or will XML-RPC require a separate web server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设 Rails 3:
有两种或三种基本方法可以从 Rails 应用程序提供 XMLRPC 服务。
第一种是使用 Rack 中间件(gem 'rack-rpc' - https://github.com/datagraph/ rack-rpc)支持这一点,理论上它应该可以与现有的基于 Rack 的应用程序在同一服务器上运行,但当我想使用 ActiveRecord 模型时,我发现这种体验令人沮丧。通过将rack-rpc添加到application.rb配置中的中间件列表中,我能够让rack-rpc响应xml-rpc客户端请求,但我没有花时间找出连接ActiveRecord的方法。
另一种方法是使用从 Rails 1.x 版本开始已弃用的 actionwebservice 功能的分支。在撰写本文时,我发现了一个在 Rails 3.0.5 上似乎相对有效的分支:
actionwebservice 功能已被弃用,以鼓励人们使用 RESTful API 来公开 Web 服务,但如果您有客户端希望能够发送xmlrpc 请求(就像十几个流行的博客客户端),您显然没有这个选项。
我按照文档实现了一个 ApiController,其行如下:
然后您需要实现一个 Api 和一个 Service 类。 (如果您不需要命名空间样式的“metaWeblog.methodname”xml-rpc 样式方法调用,则可以简化此操作;只需删除 :layered 调度方法并将其替换为 actionwebservice 文档中解释的替代方法之一。
您的 MyApi类继承自 ActionWebService::API::Base 并使用 api_method 等方法来指定受支持的 xml-rpc 方法签名。您的 MyService 继承自 ActionWebService::Base 并简单地实现像普通 ruby 代码一样的方法,您可能需要/想要。传递对您实现的 ApiController 类的引用,您可以在服务的初始化方法中执行此操作。
Assuming Rails 3:
There are two or three basic approaches to offering XMLRPC services from a rails application.
The first is to use Rack middleware (gem 'rack-rpc' - https://github.com/datagraph/rack-rpc) that supports this, which, in theory should be possible to run on the same server as an existing Rack based application, but I found the experience frustrating when I wanted to work with ActiveRecord models. I was able to get rack-rpc to respond to xml-rpc client requests by adding it to the middleware list in the application.rb config, but I did not spend the time to figure out a way to get ActiveRecord hooked up.
An alternative is to use a fork of the deprecated actionwebservice feature from the rails 1.x days. As of this writing, I've found one fork that seems relatively functional on Rails 3.0.5:
The actionwebservice feature was deprecated to encourage people to use RESTful APIs to expose web services, but if you have clients that expect to be able to send xmlrpc requests (like a dozen blogging clients that are popular out there) you'll obviously not have that option.
I followed the documentation to implement an ApiController that has lines like this:
Then you need to implement an Api and a Service class. (You can simplify this if you don't need the namespace-style "metaWeblog.methodname" xml-rpc style method calls; just remove the :layered dispatching method and replace it with one of the alternatives explained in the actionwebservice docs.
Your MyApi class inherits from ActionWebService::API::Base and uses methods like api_method to specify the supported xml-rpc method signatures. Your MyService inherits from ActionWebService::Base and simply implements the methods like normal ruby code. It's possible you may need/want to pass a reference to the ApiController class that you implement, which you can do in the service's initialize method.