1

My designer recently provided me with new design folder with different pages and mentioned the following:

You need to serve the dist folder with http server. For example: python -m SimpleHTTPServer 8000 open http://localhost:8000 http://localhost:8000/listing.html and http://localhost:8000/detail_view.html for the different pages

How do I accomplish this with Rails? or Ruby?

Myst
  • 18,516
  • 2
  • 45
  • 67
Anthony Pinto
  • 287
  • 1
  • 3
  • 10
  • It's not a rails app tho, it's just the basic html. css, and js. He suggested that we serve the folder with an http server, how can we do this using ruby, instead of python? Following his example above. – Anthony Pinto Apr 17 '17 at 01:05
  • I realise from the comment above that you know the different between dynamic and static content. I'm changing my answer. Maybe change your question to the question in the comment and remove Rails tags. – fbelanger Apr 17 '17 at 01:53
  • If you don't need Rails and you have a working Python solution, why not stick with that? – tadman Apr 17 '17 at 03:01
  • 7
    `ruby -run -e httpd . -p 8000` – 7stud Apr 17 '17 at 03:15
  • 1
    @7stud that should be the answer - it's the literal equivalent of `python -m SimpleHTTPServer 8000 `. – Amadan Apr 17 '17 at 04:22
  • 1
    I agree @7stud answered the question. – fbelanger Apr 17 '17 at 04:37

4 Answers4

6

You could try with ruby -run -e httpd . -p 8000, which will start a WEBrick server on your current directory.

Check this link for more info: http://sweetme.at/2013/08/28/simple-local-http-server-with-ruby/

Jay
  • 471
  • 5
  • 3
3

Maybe look at spinning up a Webrick server with a document root of the HTML, CSS and JavaScript static assets folder (preferably called public).

require 'webrick'

server = WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: "/var/www/app/public")
server.start

The one liner equivalent:

ruby -rwebrick -e'WEBrick::HTTPServer.new(Port: 8000, DocumentRoot: "/var/www/app/public").start'

Which is just the Ruby version of the Python code provided by your front-end guy.

Maybe just use Python, honestly it wouldn't really matter.

http://ruby-doc.org/stdlib-2.0.0/libdoc/webrick/rdoc/WEBrick.html

http://tobyho.com/2009/09/16/http-server-in-5-lines-with/

EDIT

In production environments, concurrency is a necessity.

WEBrick has been strongly unrecommended by providers like Heroku because by default it behaves as a single thread when used by Rails.

https://devcenter.heroku.com/articles/ruby-default-web-server

But WEBrick itself is a multithreaded webserver.

https://github.com/rails/rails/issues/10772

Is puma the ONLY multi-threaded rails 4 http server?

Community
  • 1
  • 1
fbelanger
  • 3,522
  • 1
  • 17
  • 32
2

Once you have created a new rails app with:

rails new your_app

Just type rails s or rails server into the terminal. This will launch a server on localhost:3000.

dpalazzari
  • 94
  • 2
  • You would run Rails to have a static file server? IMHO it's kinda like using a sledge hammer to open a screw... I'm not saying it won't open whatever needs opening, but it's hardly the best tool for the job. – Myst Apr 17 '17 at 03:59
0

If I'm understanding you correctly, you're wondering about serving static files while using Ruby.

I'm adding this answer because the built-in server that comes as part of the Ruby Standard Library (WEBrick) isn't optimal for production environments or heavier loads (i.e. larger files).

In general, most production environments use the networking stack a bit differently. Often the nginx / Apache layer will handle static files while the Ruby server will handle dynamic content.

However, to make life easier (at the expense of resources), most major Ruby frameworks (Rails, Sinatra, etc') support static file serving as well as dynamic functionality.

You can either use a framework or look into Rack (which is the platform used by most frameworks) to serve static files.

Also, some Ruby servers, such as iodine (I'm the author) support static file serving...

Here's an approach that uses Rack directly and can be used with most Ruby servers (such as puma, iodine, thin, etc').

Here's a simple Rack application, save the following in a file named config.ru at the root of your application:

# Our app will simply return a 404 not found code
RESPONSE = [404, { 'Content-Type'.freeze => 'text/html'.freeze,
        'Content-Length'.freeze => '14'.freeze }.freeze,
 ['File Not Found'.freeze]].freeze
# This is the application object
app = proc do |_env|
  RESPONSE
end

# We will use the Rack static file service middleware.
# You might want to update the folder name.
use Rack::Static, :root => 'public'

run app

make sure to install the ruby server gem from the command line run... you can use any of the following:

gem install puma
# or
gem install iodine
# or
gem install thin

Next, simply run the server from the command line (in the folder where your ruby application config.ru is placed). i.e.:

puma -p 8888
# or
iodine -p 8888

P.S.

Having said that... a web server such as nginx or apache is probably the best tool for the job.

Barring that, you could probably use iodine for unencrypted (no SSL) static file serving with (remember to install first):

iodine -www ./public

You don't need a Ruby application for this, You'll just be running a Ruby application server from the command line without giving it any Ruby application to run.

Myst
  • 18,516
  • 2
  • 45
  • 67