|
| 1 | +# Ruby Hello World Web Server |
| 2 | + |
| 3 | +A simple Ruby web server that demonstrates basic HTTP server concepts with a Hello World response. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Lightweight HTTP server built with pure Ruby |
| 8 | +- Responds with "Hello World" on the root endpoint |
| 9 | +- Request logging and error handling |
| 10 | +- Graceful shutdown with Ctrl+C |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +- Ruby 2.7.0 or higher |
| 15 | +- Bundler (for managing dependencies) |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +1. **Clone or download this repository** |
| 20 | + ```bash |
| 21 | + git clone <repository-url> |
| 22 | + cd featurevisor-example-ruby |
| 23 | + ``` |
| 24 | + |
| 25 | +2. **Install dependencies** |
| 26 | + ```bash |
| 27 | + bundle install |
| 28 | + ``` |
| 29 | + |
| 30 | + Or use the rake task: |
| 31 | + ```bash |
| 32 | + rake install |
| 33 | + ``` |
| 34 | + |
| 35 | +## Running the Web Server |
| 36 | + |
| 37 | +### Method 1: Using Rake (recommended) |
| 38 | +```bash |
| 39 | +rake run |
| 40 | +``` |
| 41 | + |
| 42 | +### Method 2: Direct Ruby execution |
| 43 | +```bash |
| 44 | +ruby app.rb |
| 45 | +``` |
| 46 | + |
| 47 | +### Method 3: Start in background |
| 48 | +```bash |
| 49 | +rake start |
| 50 | +``` |
| 51 | + |
| 52 | +### Method 4: Make the file executable and run |
| 53 | +```bash |
| 54 | +chmod +x app.rb |
| 55 | +./app.rb |
| 56 | +``` |
| 57 | + |
| 58 | +## Accessing the Server |
| 59 | + |
| 60 | +Once the server is running, you can access it at: |
| 61 | + |
| 62 | +- **Main endpoint**: http://localhost:3000 |
| 63 | + |
| 64 | +## API Endpoints |
| 65 | + |
| 66 | +### GET / |
| 67 | +Returns a simple "Hello World" text response. |
| 68 | + |
| 69 | +**Response:** |
| 70 | +``` |
| 71 | +HTTP/1.1 200 OK |
| 72 | +Content-Type: text/plain |
| 73 | +Content-Length: 11 |
| 74 | +
|
| 75 | +Hello World |
| 76 | +``` |
| 77 | + |
| 78 | +## Stopping the Server |
| 79 | + |
| 80 | +- **If running in foreground**: Press `Ctrl+C` |
| 81 | +- **If running in background**: Use `rake stop` |
| 82 | + |
| 83 | +## Available Rake Tasks |
| 84 | + |
| 85 | +- `rake run` - Start the web server in foreground |
| 86 | +- `rake start` - Start the web server in background |
| 87 | +- `rake stop` - Stop the background web server |
| 88 | +- `rake check_port` - Check if port 3000 is available |
| 89 | +- `rake install` - Install gem dependencies |
| 90 | +- `rake lint` - Run RuboCop for code style checking |
| 91 | +- `rake clean` - Clean temporary files |
| 92 | + |
| 93 | +## Project Structure |
| 94 | + |
| 95 | +``` |
| 96 | +featurevisor-example-ruby/ |
| 97 | +├── app.rb # Main web server application |
| 98 | +├── Gemfile # Ruby dependencies |
| 99 | +├── Rakefile # Build tasks |
| 100 | +├── .gitignore # Git ignore patterns |
| 101 | +├── .editorconfig # Editor configuration |
| 102 | +├── .ruby-version # Ruby version specification |
| 103 | +├── .rubocop.yml # RuboCop configuration |
| 104 | +└── README.md # This file |
| 105 | +``` |
| 106 | + |
| 107 | +## Development |
| 108 | + |
| 109 | +### Code Style |
| 110 | +This project uses RuboCop for code style enforcement. Run it with: |
| 111 | +```bash |
| 112 | +bundle exec rubocop |
| 113 | +``` |
| 114 | + |
| 115 | +### Port Configuration |
| 116 | +The server runs on port 3000 by default. You can change this by modifying the port number in `app.rb` or by creating a new instance with a different port. |
| 117 | + |
| 118 | +## Troubleshooting |
| 119 | + |
| 120 | +### Port already in use |
| 121 | +If you get an "Address already in use" error, check if port 3000 is available: |
| 122 | +```bash |
| 123 | +rake check_port |
| 124 | +``` |
| 125 | + |
| 126 | +### Ruby not found |
| 127 | +If you get a "ruby: command not found" error, make sure Ruby is installed and in your PATH. |
| 128 | + |
| 129 | +### Bundler not found |
| 130 | +Install Bundler with: |
| 131 | +```bash |
| 132 | +gem install bundler |
| 133 | +``` |
| 134 | + |
| 135 | +### Permission denied |
| 136 | +If you get a permission error when running `./app.rb`, make sure the file is executable: |
| 137 | +```bash |
| 138 | +chmod +x app.rb |
| 139 | +``` |
| 140 | + |
| 141 | +## Example Usage |
| 142 | + |
| 143 | +### Starting the server: |
| 144 | +```bash |
| 145 | +$ rake run |
| 146 | +Starting Ruby Hello World Web Server... |
| 147 | +Starting Hello World Web Server on port 3000... |
| 148 | +Visit http://localhost:3000 in your browser |
| 149 | +Press Ctrl+C to stop the server |
| 150 | + |
| 151 | +2024-01-15 10:30:00 - GET / |
| 152 | +2024-01-15 10:30:05 - GET / |
| 153 | +``` |
| 154 | + |
| 155 | +### Testing with curl: |
| 156 | +```bash |
| 157 | +# Test main endpoint |
| 158 | +curl http://localhost:3000/ |
| 159 | +# Output: Hello World |
| 160 | +``` |
| 161 | + |
| 162 | +## Contributing |
| 163 | + |
| 164 | +Feel free to modify this basic template to suit your needs. This is a starting point for learning Ruby web development or building more complex web applications. |
| 165 | + |
| 166 | +## License |
| 167 | + |
| 168 | +This project is open source and available under the [MIT License](LICENSE). |
0 commit comments