A lightweight MVC web framework built from scratch in Ruby using raw TCP sockets. No external web framework dependencies - just pure Ruby and the standard library.
- Custom HTTP request parser
- Pattern-based routing with URL parameters
- MVC architecture with controllers
- Built on TCP sockets (no Rack, no Rails, no Sinatra)
- Ruby (any recent version)
ruby app.rb
The server will start on localhost:8080
.
Once the server is running, visit these URLs in your browser:
http://localhost:8080/
Displays a welcome message.
http://localhost:8080/hello/YourName
Replace YourName
with any name to see a personalized greeting. For example:
http://localhost:8080/hello/Alice
→ "Hello, Alice!"http://localhost:8080/hello/World
→ "Hello, World!"
.
├── app.rb # TCP server, request parser, and response handler
├── router.rb # Routing system with pattern matching
├── routes.rb # Route definitions
├── request.rb # Request object
└── controllers/
└── home_controller.rb # Example controller
- TCP Server - Listens on port 8080 for incoming connections
- Request Parser - Parses raw HTTP requests into structured data
- Router - Matches request paths against registered routes
- Controllers - Handle business logic and return HTML responses
- Response - Constructs and sends HTTP responses back to clients
Edit routes.rb
to add new routes:
# GET route
Router.get('/path', [ControllerClass, :method_name])
# POST route
Router.post('/path', [ControllerClass, :method_name])
# Route with parameters
Router.get('/users/{id}', [UserController, :show])
Create a new controller in the controllers/
directory:
class UserController
def show(request, params)
user_id = params['id']
"<h1>User ID: #{user_id}</h1>"
end
end
Remember to require your controller in routes.rb
.