How to write a small ruby ​​web server?

I have a ruby ​​script that should also serve several static files in a directory (e.g. index.html, CSS and JS directories). What is the best way to write a small embedded web server to serve these files?

Decision:

require 'webrick' web_server = WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd + '/web') trap 'INT' { web_server.shutdown } web_server.start 

Or add this to your .bash_profile for easy access to files in any directory:

 alias serve="ruby -rwebrick -e\"s = WEBrick::HTTPServer.new(:Port => 3000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start\"" 
+4
source share
4 answers

As @sarnold noted, thin is a good server, the rack will use thin with a very high priority. And below is a single-line path if you have a mounted stone installed:

rackup -b "use Rack::Static, :index => 'index.html'; run Rack::File.new('.')"

If there is no "index.html" in the current directory, the contents of the directory will be indicated.

+1
source

You can use the simplest ruby ​​HTTP server, which you can find:

 ruby -run -e httpd . -p 5000 

It will serve content from a live directory on port 5000.

+8
source

If you are looking for something pure ruby ​​and simple, WEBrick is a good choice. Because it is pure-Ruby, it will not be very fast.

Mongrel is partially implemented in C and Ruby and is better for performance than WEBrick. (Ruby on Rails development mode will use Mongrel to prefer WEBrick if Mongrel is installed.)

If you want your server to scale better than any of WEBrick or Mongrel, then thin is probably the choice - it scales Mongrel together with EventMachine further than other, easier, systems can scale.

Nothing can replace the launch of a full-blown web server like nginx , but integrating your application with a lot more work for you and your users.

+2
source

Have you watched Sinatra ?

Perhaps this is what you are looking for.

0
source

Source: https://habr.com/ru/post/1386032/


All Articles