Skip to content

Commit 84da486

Browse files
committed
initial commit
0 parents  commit 84da486

7 files changed

Lines changed: 473 additions & 0 deletions

File tree

.editorconfig

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
13+
# Ruby files
14+
[*.rb]
15+
indent_style = space
16+
indent_size = 2
17+
18+
# Ruby files with heredocs
19+
[*.{rb,erb}]
20+
block_ignore_case = false
21+
22+
# Markdown files
23+
[*.md]
24+
trim_trailing_whitespace = false
25+
26+
# YAML files
27+
[*.{yml,yaml}]
28+
indent_style = space
29+
indent_size = 2
30+
31+
# JSON files
32+
[*.json]
33+
indent_style = space
34+
indent_size = 2
35+
36+
# Shell scripts
37+
[*.sh]
38+
indent_style = space
39+
indent_size = 2

.gitignore

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Byte-compiled / optimized / DLL files
2+
*.o
3+
*.so
4+
*.dll
5+
6+
# Logs
7+
*.log
8+
9+
# Temporary files
10+
*.tmp
11+
*.temp
12+
13+
# OS generated files
14+
.DS_Store
15+
.DS_Store?
16+
._*
17+
.Spotlight-V100
18+
.Trashes
19+
ehthumbs.db
20+
Thumbs.db
21+
22+
# Ruby specific
23+
*.gem
24+
*.rbc
25+
/.config
26+
/coverage/
27+
/InstalledFiles
28+
/pkg/
29+
/spec/reports/
30+
/spec/examples.txt
31+
/test/tmp/
32+
/test/version_tmp/
33+
/tmp/
34+
35+
# Bundler
36+
/.bundle/
37+
/vendor/bundle
38+
/lib/bundler/man/
39+
40+
# RVM
41+
/.rvmrc
42+
43+
# rbenv
44+
/.ruby-version
45+
/.ruby-gemset
46+
47+
# IDE
48+
.vscode/
49+
.idea/
50+
*.swp
51+
*.swo
52+
*~
53+
54+
# Environment variables
55+
.env
56+
.env.local
57+
.env.*.local

.rubocop.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
AllCops:
2+
NewCops: enable
3+
TargetRubyVersion: 2.7
4+
Exclude:
5+
- "vendor/**/*"
6+
- "spec/**/*"
7+
8+
Style/Documentation:
9+
Enabled: false
10+
11+
Style/StringLiterals:
12+
EnforcedStyle: single_quotes
13+
14+
Style/FrozenStringLiteralComment:
15+
Enabled: false
16+
17+
Metrics/MethodLength:
18+
Max: 20
19+
20+
Metrics/AbcSize:
21+
Max: 30
22+
23+
Layout/LineLength:
24+
Max: 120
25+
26+
Style/IfUnlessModifier:
27+
Enabled: false
28+
29+
Style/GuardClause:
30+
Enabled: false

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
source 'https://rubygems.org'
2+
3+
ruby '>= 2.7.0'
4+
5+
# Add your gems here
6+
gem 'rake', '~> 13.0'
7+
8+
group :development do
9+
gem 'rubocop', '~> 1.50'
10+
end

README.md

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
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).

Rakefile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'rake'
2+
3+
# Default task
4+
task default: [:run]
5+
6+
# Run the web server
7+
desc "Run the Hello World web server"
8+
task :run do
9+
puts "Starting Ruby Hello World Web Server..."
10+
puts
11+
system("ruby app.rb")
12+
end
13+
14+
# Start server in background
15+
desc "Start the web server in background"
16+
task :start do
17+
puts "Starting Hello World web server in background..."
18+
puts "Server will be available at http://localhost:3000"
19+
puts "Use 'rake stop' to stop the server"
20+
system("ruby app.rb &")
21+
puts "Server started with PID: #{$?.pid}"
22+
end
23+
24+
# Stop background server
25+
desc "Stop the background web server"
26+
task :stop do
27+
puts "Stopping background web server..."
28+
system("pkill -f 'ruby app.rb'")
29+
puts "Server stopped."
30+
end
31+
32+
# Check if port is available
33+
desc "Check if port 3000 is available"
34+
task :check_port do
35+
require 'socket'
36+
begin
37+
server = TCPServer.new(3000)
38+
server.close
39+
puts "Port 3000 is available"
40+
rescue Errno::EADDRINUSE
41+
puts "Port 3000 is already in use"
42+
puts "You may need to stop another service or use a different port"
43+
end
44+
end
45+
46+
# Clean temporary files
47+
desc "Clean temporary files"
48+
task :clean do
49+
puts "Cleaning temporary files..."
50+
FileList["*.log", "*.tmp", "*.temp"].each do |file|
51+
File.delete(file) if File.exist?(file)
52+
end
53+
puts "Cleanup complete!"
54+
end
55+
56+
# Install dependencies
57+
desc "Install gem dependencies"
58+
task :install do
59+
puts "Installing gem dependencies..."
60+
system("bundle install")
61+
end
62+
63+
# Run RuboCop
64+
desc "Run RuboCop for code style checking"
65+
task :lint do
66+
puts "Running RuboCop..."
67+
system("bundle exec rubocop")
68+
end

0 commit comments

Comments
 (0)