basic-web

Learn how to set up a basic web server using Ruby

Ruby WEBrick Server Setup

Introduction to Basic Web Server with Ruby

This guide demonstrates how to create a fundamental web server using Ruby's built-in WEBrick library. WEBrick is a simple HTTP server toolkit that is part of the Ruby standard library, making it an excellent choice for quick prototyping, local development, and simple web applications without requiring external dependencies.

Core WEBrick Server Code

The following Ruby code snippet initializes and starts a basic HTTP server. It's configured to listen on port 80, serve content from the /var/www/app directory (though this example overrides it with a simple text response), and respond to requests with "Hello, World!".

require 'webrick'

# Initialize the WEBrick HTTP server
server = WEBrick::HTTPServer.new(
    :Port => 80, # The port the server will listen on
    :SSLEnable => false, # Disable SSL for this basic example
    :DocumentRoot => '/var/www/app', # Default directory for serving files
    :ServerAlias => 'localhost' # Alias for the server
)

# Mount a handler for the root path '/'
server.mount_proc '/' do |request, response|
  response.status = 200 # Set HTTP status to OK
  response.content_type = 'text/html; charset=utf-8' # Set content type to HTML with UTF-8 encoding
  response.body = 'Hello, World!' # The response body
end

# Trap the INT signal (Ctrl+C) to shut down the server gracefully
trap 'INT' do server.shutdown end

# Start the server
server.start

Understanding the Code

WEBrick Initialization

The WEBrick::HTTPServer.new method creates an instance of the HTTP server. Key options include:

  • :Port: Specifies the network port the server will listen on. Port 80 is the standard for HTTP.
  • :SSLEnable: Set to false for simplicity in this example. For secure connections, this would be true and require SSL certificates.
  • :DocumentRoot: Defines the directory from which static files will be served if no specific handler is found.
  • :ServerAlias: Allows the server to respond to requests for specific hostnames.

Request Handling with mount_proc

The server.mount_proc '/' method registers a block of code to handle requests for the root path (/). Inside the block:

  • request: An object representing the incoming HTTP request.
  • response: An object used to construct the HTTP response.
  • response.status = 200: Sets the HTTP status code to 200 (OK).
  • response.content_type = 'text/html; charset=utf-8': Informs the client that the response is an HTML document encoded in UTF-8.
  • response.body = 'Hello, World!': Sets the actual content that will be sent back to the client.

Graceful Shutdown

The trap 'INT' do server.shutdown end line ensures that when you press Ctrl+C in the terminal, the server shuts down cleanly, releasing resources.

Starting the Server

Finally, server.start begins the server's main loop, listening for and processing incoming HTTP requests.

Further Learning

For more advanced configurations and features of WEBrick, refer to the official Ruby documentation: