ruby 中最简单的 servlet 库是什么?

发布于 2024-11-17 19:32:46 字数 340 浏览 4 评论 0原文

您推荐使用什么框架在 ruby​​ 中编写简单的 Web 应用程序,介于 WebRick、< a href="http://mongrel.rubyforge.org/" rel="nofollow">Mongrel 和 西纳特拉

我想以 json 格式回答客户端的请求。我希望自己的代码尽可能与 Http 框架解耦。

你还知道其他框架吗?

What framework do you recommand for writing simple web applications in ruby, between WebRick, Mongrel and Sinatra ?

I would like to answer in json to requests from a client. I would like to have my own code decoupled from the Http framework as much as possible.

Do you know any other framework ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

再可℃爱ぅ一点好了 2024-11-24 19:32:46

我不建议使用 WEBrick,就这样。与 Rack 兼容的框架将为您提供最好的服务。为了提高速度,您可以直接在 Rack 中编写,但这确实没有必要,因为 Sinatra 更令人愉快并且仍然非常快。

您可能还想查看 Halcyon。我不知道它是否仍在维护,但它是为编写以 JSON 响应的 API 而设计的。

I wouldn't recommend using WEBrick, period. You would best be served by a Rack-compatible framework. You could write directly in Rack for speed, but it's really unnecessary since Sinatra is so much more pleasant and still very fast.

You may also want to check out Halcyon. I don't know if it's still maintained, but it's designed for writing APIs that respond in JSON.

二智少女猫性小仙女 2024-11-24 19:32:46

WEBrick 和 Mongrel 是服务器,而不是用于构建 Web 应用程序的框架。因此,它们具有较低级别的 API,并且与它们自己的特性相关联,这使得如果您想要设计 Web 应用程序以便它可以在不同的服务器上运行,那么它们是一个不好的起点。

我会寻找一个基于 Rack 构建的框架,这是构建 Web 应用程序和 Web 框架的标准基础层这些天在 Ruby 中。

如果您正在制作非常简单的东西,那么学习 Rack 的界面本身就是一个很好的起点。

EG,一个机架应用程序,它从 post 请求的正文中解析 json 并将其打印回来并进行美化。

# in a file named config.ru
require 'json'
class JSONPrettyPrinterPrinter
  def call env
    request  = Rack::Request.new env
    if request.post?
      object = JSON.parse request.body
      [200, {}, [JSON.pretty_generate(object)]]
    else
      [200, {}, ["nothing to see here"]]
    end
  end
end

run JSONPrettyPrinterPrinter

您可以通过在与该文件相同的目录中运行 rackup 来运行它。

或者,如果你想要更高层次的东西,你可以使用 sinatra,看起来

require 'sinatra'

post '/' do
  object = JSON.parse request.body
  JSON.pretty_generate(object)
end

Sinatra 的 README 是一个很好的介绍其特点。

WEBrick and Mongrel are servers, not frameworks for building web applications. As such, they have APIs that are lower level and tied to their own idiosyncrasies which makes them a bad place to start if you want to design your web application so that it can run on different servers.

I would look for a framework that builds on Rack, which is the standard base layer for building web apps and web frameworks in Ruby these days.

If you are making something really simple, learning Rack's interface by itself is a good place to start.

E.G., a Rack Application that parses json out of a post request's body and prints it back out prettified.

# in a file named config.ru
require 'json'
class JSONPrettyPrinterPrinter
  def call env
    request  = Rack::Request.new env
    if request.post?
      object = JSON.parse request.body
      [200, {}, [JSON.pretty_generate(object)]]
    else
      [200, {}, ["nothing to see here"]]
    end
  end
end

run JSONPrettyPrinterPrinter

you can run it by running rackup in the same dir as the file.

Or, if you want something a bit more high level, you can use sinatra, which looks like this

require 'sinatra'

post '/' do
  object = JSON.parse request.body
  JSON.pretty_generate(object)
end

Sinatra's README is a good introduction to it's features.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文