More examples, +fixes to response not resetting +fixes to missing request body
This commit is contained in:
parent
9c9ceaf4f8
commit
041a9844d9
113
README.md
113
README.md
|
@ -8,16 +8,16 @@ 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
|
||||
|
@ -25,21 +25,21 @@ A push/pull stack as an HTTP API
|
|||
```ruby
|
||||
require 'hyde'
|
||||
|
||||
Stack = []
|
||||
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
|
||||
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" => []}
|
||||
stack = { "1" => [], "2" => [], "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
|
||||
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
|
||||
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
|
||||
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
|
||||
|
||||
server.start
|
||||
run app
|
||||
```
|
||||
|
||||
And a lot more to come (hopefully)
|
||||
And a lot more to be found in /examples in this repo.
|
||||
|
||||
# Documentation
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
|
@ -0,0 +1 @@
|
|||
../lib
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -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…
Reference in New Issue