CGI语言选择

发布于 2024-08-12 10:23:22 字数 853 浏览 2 评论 0原文

好吧,我在这里问了一些相关的问题,但最终却提出了更多问题,我现在意识到这是因为我没有足够的背景信息。所以我会让它更通用:

我需要制作一个简单的 Web 应用程序。静态 HTML/JQuery 页面将向某些服务器端代码发送 AJAX POST 请求,这些代码将:

  • 读取传入的 POST 变量
  • 运行一些非常简单的逻辑
  • 点击 MySQL 数据库进行简单的 CRUD 操作
  • 返回一个纯字符串页面上的 javascript 使用的数据

我认为 Ruby 是一个不错的选择,因为每个人都对它的设计有多好赞不绝口,而且我一直在使用它 - 不是 RoR,只是用于简单脚本任务的 Ruby - 以及我有点喜欢它。

我的问题是,我对数以万亿计的辅助库和框架感到非常困惑。我不知道这些是什么,因此如果我需要它们中的任何一个/全部:Rack、Sinatra、Camping、mod_ruby、FastCGI 等。

仅仅学习 PHP 和我们会更容易吗?或者我可以直接将 .rb 文件放入 cgi-bin 文件夹(我使用 Apache 进行托管)并使用 ruby​​ cgi 库来获取我的变量吗?

编辑:就 Rails 而言,我只是假设它对于我想要的东西来说太过分了,但我可能是错的。我查看了它,它对于快速生成基于数据的网站似乎很酷,但这不是我想要做的。我不想要用户的任何表单页面。我不希望他们输入数据或查看记录。我什至不想返回任何 HTML。我只想要一个 ruby​​ 脚本位于服务器上,在 post 请求中传递一些变量,并返回一个 JSON 字符串作为响应。我需要一些基本的 cookie/会话/状态管理

这在 C# 和 ASP.NET 中使用 Web 服务确实很容易做到,但它似乎与开源技术非常令人困惑。

Ok, I've asked a few related questions here and only ended up with more questions and I realized now it's because I don't have enough background info. So I'll make it more generic:

I need to make a simple web application. Static HTML/JQuery pages will send AJAX POST requests to some server side code, which will:

  • Read the POST variables passed in
  • Run some very simple logic
  • Hit a MySQL database for simple CRUD ops
  • Return a plain string of data to be consumed by the javascript on the page

I was assuming Ruby was a good choice for this as everyone is raving about how well it's designed, and I've been playing with it - not RoR, just Ruby for simple scripting tasks - and I kind of like it.

My question is, I'm hopelessly confused by the trillion helper libraries and frameworks out there. I don't know what these are and therefore if I need any/all of them: Rack, Sinatra, Camping, mod_ruby, FastCGI, etc.

Would it be easier to just learn PHP and us that? Or can I get away with just dropping my .rb files into the cgi-bin folder(I'm using Apache for hosting) and use the ruby cgi library to get my variables?

EDIT: As far as Rails, I'm just assuming that it's overkill for what I want but I might be wrong. I looked at it, and it seemed cool for generating data based web sites quickly, but that's not what I'm trying to do. I don't want any forms pages for the user. I don't want them entering data or viewing records. I don't even want to return any HTML. I just want a ruby script to sit on the server, get passed a few variables in a post request, and return a JSON string in response. I will need some basic cookie/session/state managment

This is a really easy thing to do in C# and ASP.NET with webservices, but it seems very confusing with the open source technologies.

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

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

发布评论

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

评论(6

挽清梦 2024-08-19 10:23:22

您不想使用成熟框架中的任何功能,所以不要使用它。更少的代码=更少的错误=更少的安全噩梦。

CGI

与其他方法相比,CGI 有一些性能缺陷,但(在我看来)仍然是最简单且最容易使用的一种。这就是使用内置 cgi 库的方式:

require "cgi"
cgi= CGI.new

answer= evaluate(cgi.params)

cgi.out do
    answer
end

rack

另一个低技术且易于使用的变体是rack。 Rack 是一个抽象层,适用于许多 Web 服务器接口(cgi、fastcgi、webrick 等)。它的简单性可以与仅使用 cgi 相比。将以下内容放入 cgi 目录中以 .ru 结尾的文件中。

#!/usr/bin/rackup
require "rack/request"

run (lambda do |env|
  request= Rack::Request(env)

  anwser= evaluate(request.params)

  return [200, {}, answer]
end)

这看起来和cgi没有太大区别,但是它给了你更多的可能性。如果您在本地计算机上执行此文件,rackup 将启动 webrick 网络服务器。该网络服务器将提供您在 .ru 文件中描述的网页。

其他接口

fast-cgi

fast-cgi 的工作方式几乎与 CGI 一样。不同之处在于,在 CGI 中,您的脚本会针对它必须处理的每个请求启动。使用 fast-cgi,您的脚本仅针对所有请求启动一次。有一个库可用于在 ruby​​ 中编写 fast-cgi 脚本。

mod_ruby

mod_ruby 是 apache 的内置 ruby​​ 解释器。它的工作方式类似于 apache 中的 mod_php。

mongrel

mongrel 是一个用于 ruby​​ 应用程序的独立网络服务器。这是一个简单的 hello world 示例。

require 'mongrel'

class SimpleHandler < Mongrel::HttpHandler
   def process(request, response)
     response.start(200) do |head,out|
       head["Content-Type"] = "text/plain"
       out.write("hello world!\n")
     end
   end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/hello", SimpleHandler.new)
h.run.join

Mongrel 通常用于 Rails 和其他 Ruby 框架。大多数人在端口 80 上使用 apache 或其他东西。该 Web 服务器会将请求分发到在其他端口上运行的多个混合服务器。我认为这对于您的需求来说完全是多余的。

phusion乘客

乘客也称为mod_rails或mod_rack。它是 apache 和 nginx 托管 Rails 和rack 应用程序的模块。根据他们的网站,载有乘客的铁轨使用的内存比单独的铁轨少 1/3。如果您为rack 编写软件,则可以通过使用passenger 而不是cgi 或fast-cgi 来使其速度更快一些。

You don't want to use any feature from a fully blown framework so don't use one. Less code = less bugs = less security nightmares.

CGI

CGI has some performance drawbacks in comparison to other methods, but is still (in my opinion) the simplest and easiest to use one. This is how you use the builtin cgi library:

require "cgi"
cgi= CGI.new

answer= evaluate(cgi.params)

cgi.out do
    answer
end

rack

Another low tech easy to use variant would be rack. Rack is an abstraction layer which works for many webserver interfaces (cgi, fastcgi, webrick, …). It's simplicity can be compared to the one of only using cgi. Put the following into a file wich ends with .ru into your cgi directory.

#!/usr/bin/rackup
require "rack/request"

run (lambda do |env|
  request= Rack::Request(env)

  anwser= evaluate(request.params)

  return [200, {}, answer]
end)

This does not seem very different from cgi, but it gives you much more possibilities. If youst execute this file on your local machine rackup will start the webrick webserver. This webserver will deliver the webpages you described in your .ru file.

Other interfaces

fast-cgi

fast-cgi works almost like CGI. The difference is, in CGI your script get's started for every request it has to work on. With fast-cgi, your script only starts once for all requests. There is a library available to write fast-cgi script in ruby.

mod_ruby

mod_ruby is a builtin ruby interpreter for apache. It works analog to mod_php in apache.

mongrel

mongrel is a standalone webserver for ruby applications. This is a simple hello world example with it.

require 'mongrel'

class SimpleHandler < Mongrel::HttpHandler
   def process(request, response)
     response.start(200) do |head,out|
       head["Content-Type"] = "text/plain"
       out.write("hello world!\n")
     end
   end
end

h = Mongrel::HttpServer.new("0.0.0.0", "3000")
h.register("/hello", SimpleHandler.new)
h.run.join

Mongrel is often used for rails and other ruby frameworks. Most people use an apache or something else on port 80. This webserver than distributes the requests to several mongrel servers running on other ports. I think this is totaly overkill for your needs.

phusion passenger

passenger is also called mod_rails or mod_rack. It is a module for apache and nginx to host rails and rack applications. According to their websites rails with passenger uses 1/3 less ram than rails alone. If you write your software for rack, you could make it a little faster by using passenger, instead of cgi or fast-cgi.

国际总奸 2024-08-19 10:23:22

使用 jQuery 和 PHP。

这两种技术都有详细记录,您应该能够在几个小时内启动并运行应用程序。你听起来好像你知道一两件事 - 谈论 CRUD 操作等等 - 所以我不会用例子来烦你。就 JSON 而言,可能有一百万个 PHP 库用于输出 JSON 对象。

Use jQuery and PHP.

Both technologies are well documented, and you should be able to get an application up and running in a matter of hours. You sound like you know a thing or two - talking about CRUD operations and so on - so I won't bore you with examples. And as far as JSON goes, there are probably a million PHP libraries out there, for outputting JSON objects.

笑红尘 2024-08-19 10:23:22

Sinatra 非常容易学习和使用。使用 Phusion Passenger(类似于 Rails 和 Sinatra 等 ruby​​ 框架的 mod_php)也可以轻松部署。说明如下: http://blog.squarefour.net/ 2009/03/06/deploying-sinatra-on-passenger/

如果您发现您需要的东西超出了 Sinatra 所能提供的范围,我推荐 Rails。使用 Passenger 进行设置甚至更容易,因为几乎不需要任何配置。 (参见 modrails.com)。

Sinatra is very simple to learn and use. It's also easy to deploy with the use of Phusion Passenger (which is like mod_php for ruby frameworks like Rails and Sinatra). Instructions here: http://blog.squarefour.net/2009/03/06/deploying-sinatra-on-passenger/

If you find that you need more than what Sinatra will give you, I recommend Rails. Setting up that with Passenger is even easier since hardly any configuration is required. (see modrails.com).

-小熊_ 2024-08-19 10:23:22

PHP 非常易于使用,因为它是专门为此类事情而设计的。想要读取 POST 变量吗?它们在 $_POST 中。想要查询MySQL? mysql_query("从`表`中选择`某事`");。如果您需要帮助,Google 搜索“php What_you_need_to_do”几乎总是会在 php.net 上返回结果,这非常有帮助。

对于您正在做的事情,您不需要任何额外的框架。

PHP is very easy to use because it's designed specifically for this sort of thing. Want to read POST variables? They're in $_POST. Want to query MySQL? mysql_query("SELECT `something` FROM `table`");. And if you ever need help, Google searches for "php what_you_need_to_do" almost always return results on php.net, which is very helpful.

And for what you're doing, you don't need any additional frameworks.

夜吻♂芭芘 2024-08-19 10:23:22

我很好奇您对尝试 Rails 的抵触情绪是什么。你说你想“花更多的时间在脚本本身上,而不是在配置上”,但你似乎立即放弃了 Rails。 Rails 的核心是约定优于配置。如果您花时间了解 Rails 是如何工作的,那么只需遵循该框架的约定,您就可以“免费”获得令人难以置信的大量功能。

如果你想制作一个简单的 Web 应用程序,Rails 确实是一个非常轻松且很好的开始方式。您可以使用 sqlite 数据库,甚至不用搞乱 MySQL(无法扩展,但对于学习或简单的应用程序来说,这很好)。确实有更简单的框架,但由于您似乎是 Web 编程新手,我建议您从文档和知识渊博的人员方面为您提供最多支持的框架开始。遵循古老的格言:先让它发挥作用,然后再优化。

我能看到的唯一症结是 Apache 集成...现在关于 Rails 部署的共识似乎集中在使用轻量级 httpds 代替 Apache。如果你可以做自定义 mods,有一个 mod_fcgid 似乎是使用 Apache 实现这一点的最佳方法(mod_ruby 已被弃用,有缺陷且缓慢,我上次读到)。或者还有 Phusion Passenger,这似乎是最新、最好的方法。由于为每个请求执行解释器 + 框架的开销,在标准 CGI 环境中运行 Rails 会产生糟糕的性能(但这确实适用于任何 CGI 框架)。如果您使用将解释器+框架保留在内存中的东西,您将获得更好的性能。

I am curious about what I perceive to be your resistance to trying Rails. You say that you want "to spend more time on the scripting itself and less on configuration", and yet you seem to dismiss Rails out of hand. Rails is all about convention over configuration. If you take the time to learn how Rails does things, you can get an incredible amount of functionality "for free" just by following the conventions of the framework.

If you want to make a simple web app, Rails is really a very painless and good way to start. You can use a sqlite database and not even mess with MySQL (won't scale, but for learning or simple apps it's fine). It's true that there are simpler frameworks, but since you seem new to web programming, I'd recommend that you start with what will get you the most support in terms of documentation and knowledgeable folks. Follow the old adage: Get it working first, then optimize later.

The only sticking point I can see is the Apache integration... The consensus on Rails deployment these days seems to be focused on using lightweight httpds in place of Apache. There is a mod_fcgid which seems to be the best way to do it with Apache (mod_ruby is deprecated, buggy and slow, last I read) if you can do custom mods. Or there's Phusion Passenger, which seems to be the latest and greatest way to do it. Running Rails in a standard CGI environment will yield awful performance (but that goes for any CGI framework, really) due to the overhead of executing the interpreter + framework for every request. You'll get much better performance if you go with something that keeps the interpreter + framework in memory.

ㄖ落Θ余辉 2024-08-19 10:23:22

我个人很喜欢姜戈。我在使用 Ruby on Rails 时遇到了一个问题,当我只想做一些简单的事情时,我就被一切淹没了,这听起来像是你想做的(因为你说 ROR 感觉有点矫枉过正)。我发现 Django 的一个很酷的事情是,如果你想要一切,那么你可以通过插入它来获得一切......但如果你想要更少,那么你就不需要插入该技术,而且它更加轻量级。

以“观点”为例。 Django 与 ROR 一样,使用 MVC。但如果你只想返回一串数据,不需要视图,那么就不需要插入视图。但是,如果稍后您决定它将在视图中更有组织性,那么您可以在那时轻松地将其插入。

这是他们的网站:http://www.djangoproject.com/

I personally like Django. I had a problem with Ruby on Rails where I just got overwhelmed by everything when I just wanted to do something simple, which it sounds like you want to do (since you said ROR feels like overkill). The cool thing I found with Django is that if you WANT everything, then you can get everything by plugging it in...but if you want less then you just don't plug in that technology and it's that much more lightweight.

Take for example "views". Django, like ROR, uses MVC. But if you just want to return a string of data and don't need the view, then you don't need to plug in the view. But if later on you decide that it will be more organized in a view then you can easily plug it in at that time.

Here's their website: http://www.djangoproject.com/

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