PHP-style web development on Ruby: creating microf

2019-07-24 00:20发布

问题:

In PHP world you can just create index.php file, put some inline code and raw HTML, run Apache and it just works.

There are a lot af talks about bad practices of using inline code an so on. So we won't discuss this theme here, please.

How can I start my Ruby application same way? I want to use ERB for code messing, so it will look this way

# index.rb
<h1>Hello world!</h1>
<div>
  1 + 1 = <%= 1 + 1 %>
</div>

So my questions are:

  1. What makes PHP just work.
    AFAIU(nderstand) There is native support for HTTP in PHP, so I have to use Rack to support it with Ruby
  2. Some basic knowledge for creating my on "microframework": working with application/http servers (Mongrel, nginx, binding on http port and all such kind of job), working with HTTP requests: sessions, params, GET/POST etc (Rack?), sending responses (ERB templating).

So I can make my own (in educational puprposes) microframework for PHP-style web development with Ruby :D

UPD

What really I want to do is an application wich will just get request url, run just that file and return HTML as a response. Also this application should be allowed to be binded on some port

index.rb
info/about.rb
info/contacts.rb
products/product.rb

so It should parse url localhost/index.rb and run index.rb, localhost/products/product.rb?product_id=10 and run products/product.rb and pass product_id=10 as a params hash.

UPD 2

I think good point to start is to dig into Camping microframework source:

https://github.com/camping/camping

It is about 5Kb weight so I shouldn't be confused in it

回答1:

It is possible to write CGI scripts with Ruby but this is generally not done because we have better solutions.

One file per page is not a very useful abstraction, it's just the one that PHP supports. Ruby has better ways to abstract a web application (like Sinatra, Rails, or even just Rack) so we prefer to use them.

Putting the file name in the url is another unfortunate side effect of PHP's design. It is implementation revealing and unnecessary (you're not getting a Ruby page, you're getting an HTML page), so we choose not to do that either.

CGI and FCGI in Ruby are also slower than the other solutions. This is not because of some limit on how performant they can be; it's mostly just because the effort to make Ruby web applications faster has been spent in more useful areas like Rack and Rails. No one really uses CGI so no one really cares to make it fast. mod_ruby makes CGI scripts somewhat faster if you really want to go this route, but again: there are better ways.



回答2:

Apache can run PHP by loading in the mod_php module.

I believe to run ruby you will need to have it installed on the server and have mod_ruby loaded into apache. take a look at: http://www.modruby.net/en/



回答3:

You are looking for CGI. The Apache modules like mod_php or mod_ruby are just packaging provided for CGI scripts written in PHP or Ruby.