More examples, +fixes to response not resetting +fixes to missing request body
This commit is contained in:
parent
9c9ceaf4f8
commit
041a9844d9
115
README.md
115
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
|
A simple "Hello, World!" HTTP API using Hyde
|
||||||
```ruby
|
```ruby
|
||||||
require 'Hyde'
|
require 'hyde'
|
||||||
|
|
||||||
server = Hyde::Server.new Port: 8000 do
|
app = Hyde::Server.new do
|
||||||
get "/hello" do |ctx|
|
get "/hello" do
|
||||||
ctx.response.body = "Hello, World!"
|
header "content-type", "text/plain"
|
||||||
ctx.response['Content-Type'] = "text/plain"
|
"Hello world!"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
server.start
|
run app
|
||||||
```
|
```
|
||||||
|
|
||||||
A push/pull stack as an HTTP API
|
A push/pull stack as an HTTP API
|
||||||
|
|
||||||
```ruby
|
```ruby
|
||||||
require 'hyde'
|
require 'hyde'
|
||||||
|
|
||||||
Stack = []
|
stack = []
|
||||||
|
|
||||||
server = Hyde::Server.new Port: 8000 do
|
app = Hyde::Server.new do
|
||||||
get "pull" do |ctx|
|
get "/pop" do
|
||||||
ctx.response.body = "#{Stack.pop}"
|
header 'content-type', 'text/plain'
|
||||||
ctx.response["Content-Type"] = "text/plain"
|
stack.pop.to_s
|
||||||
end
|
end
|
||||||
post "push" do |ctx|
|
post "/push" do
|
||||||
Stack.push ctx.request.body
|
header 'content-type', 'text/plain'
|
||||||
ctx.response.body = "#{ctx.request.body}"
|
stack.push(request.body)
|
||||||
ctx.response["Content-Type"] = "text/plain"
|
request.body
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
server.start
|
run app
|
||||||
```
|
```
|
||||||
|
|
||||||
Several push/pull buckets
|
Several push/pull buckets
|
||||||
|
@ -47,25 +47,23 @@ Several push/pull buckets
|
||||||
```ruby
|
```ruby
|
||||||
require 'hyde'
|
require 'hyde'
|
||||||
|
|
||||||
Stack = {"bucket_1" => [], "bucket_2" => [], "bucket_3" => []}
|
stack = { "1" => [], "2" => [], "3" => [] }
|
||||||
|
|
||||||
server = Hyde::Server.new Port: 8000 do
|
app = Hyde::Server.new do
|
||||||
path ["bucket_1","bucket_2","bucket_3"] do
|
path "bucket_(1|2|3)" do
|
||||||
get "pull" do |ctx|
|
get "pop" do |bucket|
|
||||||
bucket_name = (ctx.filepath.match /bucket_[^\/]*/)[0]
|
header "content-type", "text/plain"
|
||||||
ctx.response.body = "#{Stack[bucket_name].pop}"
|
stack[bucket].pop.to_s
|
||||||
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
|
|
||||||
end
|
end
|
||||||
|
post "push" do |bucket|
|
||||||
|
header "content-type", "text/plain"
|
||||||
|
stack[bucket].push(request.body)
|
||||||
|
request.body
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
server.start
|
run app
|
||||||
```
|
```
|
||||||
|
|
||||||
Static file serving
|
Static file serving
|
||||||
|
@ -74,16 +72,13 @@ Static file serving
|
||||||
```ruby
|
```ruby
|
||||||
require 'hyde'
|
require 'hyde'
|
||||||
|
|
||||||
server = Hyde::Server.new Port:8000 do
|
app = Hyde::Server.new do
|
||||||
path "static" do
|
root "/var/www"
|
||||||
root "/var/www"
|
index ["index.html","index.htm"]
|
||||||
index ["index.html","index.htm"]
|
serve "**/*.(html|htm)"
|
||||||
serve "*/*.html" safe_regexp: false
|
|
||||||
serve "*.html"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
server.start
|
run app
|
||||||
```
|
```
|
||||||
|
|
||||||
Logging on a particular path
|
Logging on a particular path
|
||||||
|
@ -91,29 +86,29 @@ Logging on a particular path
|
||||||
```ruby
|
```ruby
|
||||||
require 'hyde'
|
require 'hyde'
|
||||||
|
|
||||||
server = Hyde::Server.new Port:8000 do
|
app = Hyde::Server.new do
|
||||||
path "unimportant" do
|
path "unimportant" do
|
||||||
get "version" do |ctx|
|
get "version" do
|
||||||
ctx.response.body = '{"version": "the good one"}'
|
header "content-type", "text/plain"
|
||||||
ctx.response['Content-Type'] = "application/json"
|
"1337 (the best one)"
|
||||||
end
|
|
||||||
end
|
end
|
||||||
path "important" do
|
end
|
||||||
preprocess do |ctx|
|
path "important" do
|
||||||
# Implement logging logic here
|
preprocess do |req|
|
||||||
puts "Client at #{ctx.request.remote_ip} wanted to access something /important!"
|
# Implement logging logic here
|
||||||
end
|
puts "Client at #{req.headers['REMOTE_ADDR']} wanted to access something /important!"
|
||||||
get "answer" do |ctx|
|
|
||||||
ctx.response.body = '{"answer":42}'
|
|
||||||
ctx.response['Content-Type'] = "application/json"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
get "answer" do
|
||||||
|
header "content-type", "application/json"
|
||||||
|
'{"answer":42, "desc":"something important!"}'
|
||||||
|
end
|
||||||
|
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
|
# 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
|
# @return [Boolean] true if further navigation is possible
|
||||||
# @raise [UncaughtThrowError] may raise if die() is called.
|
# @raise [UncaughtThrowError] may raise if die() is called.
|
||||||
def process(request)
|
def process(request)
|
||||||
|
@response = nil
|
||||||
return reject(request) unless request.path.match?(/^\/?$/)
|
return reject(request) unless request.path.match?(/^\/?$/)
|
||||||
|
|
||||||
@request = request
|
@request = request
|
||||||
|
|
|
@ -37,7 +37,7 @@ module Hyde
|
||||||
# Returns request body (if POST data exists)
|
# Returns request body (if POST data exists)
|
||||||
# @return [nil, String]
|
# @return [nil, String]
|
||||||
def body
|
def body
|
||||||
@rack.input&.gets
|
@body ||= @rack.input&.gets
|
||||||
end
|
end
|
||||||
|
|
||||||
# Push current navigation state (path, splat, param) onto state stack
|
# 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_")
|
[name.delete_prefix("HTTP_"), value] if name.start_with?("HTTP_")
|
||||||
end.to_h
|
end.to_h
|
||||||
headers.merge!({ "CONTENT_TYPE" => env["CONTENT_TYPE"],
|
headers.merge!({ "CONTENT_TYPE" => env["CONTENT_TYPE"],
|
||||||
"CONTENT_LENGTH" => env["CONTENT_LENGTH"] })
|
"CONTENT_LENGTH" => env["CONTENT_LENGTH"],
|
||||||
|
"REMOTE_ADDR" => env["REMOTE_ADDR"] })
|
||||||
headers.freeze
|
headers.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue