|
||
---|---|---|
test | ||
COPYING.txt | ||
README.md | ||
hyde.rb | ||
index.html | ||
test_calculator.rb | ||
test_combined.rb | ||
test_hyde.rb | ||
test_webrick.rb |
README.md
The strange case of Dr. WEBrick and Mr. Hyde
Hyde is a library that provides a DSL for creating HTTP servers. It wraps around WEBrick (this decision is subject to change), and allows for creating complex HTTP path pattern matching scenarios.
Hyde was initially designed to try the DSL features Ruby offered. When the potential of Ruby's DSL tools was realized, Hyde became an experiment in an attempt to create a backend API tool that would speed up the process of creating HTTP paths by grouping them together into PathSpec
objects.
Since then, the goal of the project has remained largely the same - to create a tool that could be used to speed up the process of API development.
Examples
A simple "Hello, World!" HTTP API using 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
end
server.start
A push/pull stack as an HTTP API
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
end
server.start
Several push/pull buckets
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
end
end
server.start
Static file serving (Note: index applies only to /var/www (to the path its defined in))
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
end
server.start
Logging on a particular path
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
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
end
server.start
And a lot more to come (hopefully)
Documentation
Someday it's gonna be there somewhere
License
Hyde - an HTTP request pattern matching system
Copyright (C) 2022 yessiest (yessiest@memeware.net)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.