GOAL: To launch our web application!
To launch the web server, run the puma
command from the web
directory. Alternatively, you can also run rackup
and it will work the same.
The benefit of the rackup command is that it's app-server agnostic. So it will work even if you're using a different app-server like Thin instead of Puma.
$ puma
Puma starting in single mode...
* Puma version: 6.4.0 (ruby 3.2.2-p53) ("The Eagle of Durango")
* Min threads: 0
* Max threads: 5
* Environment: development
* PID: 10910
* Listening on http://0.0.0.0:9292
Use Ctrl-C to stop
Now our web server is running and serving our application. Open your browser and go to http://0.0.0.0:9292 URL.
Congratulations! You have the simplest web application written in Ruby.
To summarize what we've done so far,
- We installed an application server (Puma) that accepts HTTP request and forwards it to our application
- We created the glue (the
rackup
file) between the application server and our application, so Puma knows where to send the incoming request. - We wrote an entire web application in a single Ruby script
app.rb
that takes the incoming HTTP request in theenv
parameter and returns the response containing the status, headers, and the response body.
The cool thing is, this is exactly what Rails does behind the scenes!
If you don't believe me, open any Rails app and notice the config.ru
file. It runs the Rails.application
as your application.
require_relative "config/environment"
run Rails.application
Rails.application.load_server
The Rails application class inherits from the Engine class. It contains the entry point of your application. Open the engine.rb file and see for yourself.
def call(env)
req = build_request env
app.call req.env
end
Let's fix one minor annoyance we have in our application: having to restart the application whenever we change the code.