# The strange case of Dr. Rack and Mr. Hyde Hyde is a library that provides a DSL for creating HTTP servers. As of now it is using Rack as the webserver adapter, but ideally it shouldn't take much work to make it run on top of any webserver. Hyde was made mostly for fun. Ideally it will become something more, but as of yet it's just an experiment revolving around Ruby Metaprogramming and its DSL capabilities. # Examples A simple "Hello, World!" HTTP API using Hyde ```ruby 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 ```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 end server.start ``` 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 end end server.start ``` Static file serving (Note: index applies *only* to /var/www (to the path its defined in)) ```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 end server.start ``` 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 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 . ```