Browse Source

More examples, +fixes to response not resetting +fixes to missing request body

master
Yessiest 8 months ago
parent
commit
041a9844d9
  1. 119
      README.md
  2. 22
      examples/buckets.ru
  3. 13
      examples/helloworld.ru
  4. 1
      examples/lib
  5. 25
      examples/logging.ru
  6. 20
      examples/stack.ru
  7. 12
      examples/staticfiles.ru
  8. 1
      lib/hyde/probe/handler.rb
  9. 5
      lib/hyde/request.rb

119
README.md

@ -8,38 +8,38 @@ Hyde was made mostly for fun. Ideally it will become something more, but as of y
A simple "Hello, World!" HTTP API using Hyde
```ruby
require 'Hyde'
require 'hyde'
server = Hyde::Server.new Port: 8000 do
get "/hello" do |ctx|
ctx.response.body = "Hello, World!"
ctx.response['Content-Type'] = "text/plain"
end
app = Hyde::Server.new do
get "/hello" do
header "content-type", "text/plain"
"Hello world!"
end
end
server.start
run app
```
A push/pull stack as an HTTP API
```ruby
```ruby
require 'hyde'
Stack = []
server = Hyde::Server.new Port: 8000 do
get "pull" do |ctx|
ctx.response.body = "#{Stack.pop}"
ctx.response["Content-Type"] = "text/plain"
end
post "push" do |ctx|
Stack.push ctx.request.body
ctx.response.body = "#{ctx.request.body}"
ctx.response["Content-Type"] = "text/plain"
end
stack = []
app = Hyde::Server.new do
get "/pop" do
header 'content-type', 'text/plain'
stack.pop.to_s
end
post "/push" do
header 'content-type', 'text/plain'
stack.push(request.body)
request.body
end
end
server.start
run app
```
Several push/pull buckets
@ -47,25 +47,23 @@ Several push/pull buckets
```ruby
require 'hyde'
Stack = {"bucket_1" => [], "bucket_2" => [], "bucket_3" => []}
server = Hyde::Server.new Port: 8000 do
path ["bucket_1","bucket_2","bucket_3"] do
get "pull" do |ctx|
bucket_name = (ctx.filepath.match /bucket_[^\/]*/)[0]
ctx.response.body = "#{Stack[bucket_name].pop}"
ctx.response["Content-Type"] = "text/plain"
end
post "push" do |ctx|
bucket_name = (ctx.filepath.match /bucket_[^\/]*/)[0]
Stack[bucket_name].push ctx.request.body
ctx.response.body = "#{ctx.request.body}"
ctx.response["Content-Type"] = "text/plain"
end
stack = { "1" => [], "2" => [], "3" => [] }
app = Hyde::Server.new do
path "bucket_(1|2|3)" do
get "pop" do |bucket|
header "content-type", "text/plain"
stack[bucket].pop.to_s
end
post "push" do |bucket|
header "content-type", "text/plain"
stack[bucket].push(request.body)
request.body
end
end
end
server.start
run app
```
Static file serving
@ -74,16 +72,13 @@ Static file serving
```ruby
require 'hyde'
server = Hyde::Server.new Port:8000 do
path "static" do
root "/var/www"
index ["index.html","index.htm"]
serve "*/*.html" safe_regexp: false
serve "*.html"
end
app = Hyde::Server.new do
root "/var/www"
index ["index.html","index.htm"]
serve "**/*.(html|htm)"
end
server.start
run app
```
Logging on a particular path
@ -91,29 +86,29 @@ Logging on a particular path
```ruby
require 'hyde'
server = Hyde::Server.new Port:8000 do
path "unimportant" do
get "version" do |ctx|
ctx.response.body = '{"version": "the good one"}'
ctx.response['Content-Type'] = "application/json"
end
app = Hyde::Server.new do
path "unimportant" do
get "version" do
header "content-type", "text/plain"
"1337 (the best one)"
end
end
path "important" do
preprocess do |req|
# Implement logging logic here
puts "Client at #{req.headers['REMOTE_ADDR']} wanted to access something /important!"
end
path "important" do
preprocess do |ctx|
# Implement logging logic here
puts "Client at #{ctx.request.remote_ip} wanted to access something /important!"
end
get "answer" do |ctx|
ctx.response.body = '{"answer":42}'
ctx.response['Content-Type'] = "application/json"
end
get "answer" do
header "content-type", "application/json"
'{"answer":42, "desc":"something important!"}'
end
end
end
server.start
run app
```
And a lot more to come (hopefully)
And a lot more to be found in /examples in this repo.
# Documentation

22
examples/buckets.ru

@ -0,0 +1,22 @@
# frozen_string_literal: true
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'hyde'
stack = { "1" => [], "2" => [], "3" => [] }
app = Hyde::Server.new do
path "bucket_(1|2|3)" do
get "pop" do |bucket|
header "content-type", "text/plain"
stack[bucket].pop.to_s
end
post "push" do |bucket|
header "content-type", "text/plain"
stack[bucket].push(request.body)
request.body
end
end
end
run app

13
examples/helloworld.ru

@ -0,0 +1,13 @@
# frozen_string_literal: true
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'hyde'
app = Hyde::Server.new do
get "/hello" do
header "content-type", "text/plain"
"Hello World!"
end
end
run app

1
examples/lib

@ -0,0 +1 @@
../lib

25
examples/logging.ru

@ -0,0 +1,25 @@
# frozen_string_literal: true
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'hyde'
app = Hyde::Server.new do
path "unimportant" do
get "version" do
header "content-type", "text/plain"
"1337 (the best one)"
end
end
path "important" do
preprocess do |req|
# Implement logging logic here
puts "Client at #{req.headers['REMOTE_ADDR']} wanted to access something /important!"
end
get "answer" do
header "content-type", "application/json"
'{"answer":42, "desc":"something important!"}'
end
end
end
run app

20
examples/stack.ru

@ -0,0 +1,20 @@
# frozen_string_literal: true
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'hyde'
stack = []
app = Hyde::Server.new do
get "/pop" do
header 'content-type', 'text/plain'
stack.pop.to_s
end
post "/push" do
header 'content-type', 'text/plain'
stack.push(request.body)
request.body
end
end
run app

12
examples/staticfiles.ru

@ -0,0 +1,12 @@
# frozen_string_literal: true
$LOAD_PATH.unshift("#{File.dirname(__FILE__)}/lib")
require 'hyde'
app = Hyde::Server.new do
root "/var/www"
index ["index.html", "index.htm"]
serve "**/*.(html|htm)"
end
run app

1
lib/hyde/probe/handler.rb

@ -34,6 +34,7 @@ module Hyde
# @return [Boolean] true if further navigation is possible
# @raise [UncaughtThrowError] may raise if die() is called.
def process(request)
@response = nil
return reject(request) unless request.path.match?(/^\/?$/)
@request = request

5
lib/hyde/request.rb

@ -37,7 +37,7 @@ module Hyde
# Returns request body (if POST data exists)
# @return [nil, String]
def body
@rack.input&.gets
@body ||= @rack.input&.gets
end
# Push current navigation state (path, splat, param) onto state stack
@ -112,7 +112,8 @@ module Hyde
[name.delete_prefix("HTTP_"), value] if name.start_with?("HTTP_")
end.to_h
headers.merge!({ "CONTENT_TYPE" => env["CONTENT_TYPE"],
"CONTENT_LENGTH" => env["CONTENT_LENGTH"] })
"CONTENT_LENGTH" => env["CONTENT_LENGTH"],
"REMOTE_ADDR" => env["REMOTE_ADDR"] })
headers.freeze
end
end

Loading…
Cancel
Save