Elegant HTTP DSL
Go to file
Yessiest f1f13faddf fixed endpoint matching when indexing directory 2023-05-05 00:28:34 +04:00
test added stuff to the test webpage 2023-04-20 21:34:05 +04:00
COPYING.txt License and README 2023-05-05 00:00:47 +04:00
README.md License and README 2023-05-05 00:00:47 +04:00
hyde.rb fixed endpoint matching when indexing directory 2023-05-05 00:28:34 +04:00
index.html removed the crippling cocaine addiction from regexp pattern matching 2023-04-19 11:30:24 +04:00
test_calculator.rb testing this little express js task one of my friends got 2023-04-27 22:27:53 +04:00
test_combined.rb made the matching system work like sinatra's 2023-04-30 03:57:33 +04:00
test_hyde.rb adding webrick functionality 2023-04-16 20:30:00 +04:00
test_webrick.rb adding webrick functionality 2023-04-16 20:30:00 +04:00

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/>.