Elegant HTTP DSL
Go to file
Yessiest fccc7ea9f0 header modifiers, status modifier, proper call rollbacks 2023-09-06 02:34:17 +04:00
.yardoc rewrite in progress 2023-08-29 22:41:50 +04:00
doc rewrite in progress 2023-08-29 22:41:50 +04:00
lib header modifiers, status modifier, proper call rollbacks 2023-09-06 02:34:17 +04:00
test Rewrite: Most of the structure is done, several pattern matching bugs fixed, config.ru is now working 2023-09-04 00:13:30 +04:00
COPYING.txt License and README 2023-05-05 00:00:47 +04:00
README.md rewrite in progress 2023-08-29 22:41:50 +04:00
config.ru header modifiers, status modifier, proper call rollbacks 2023-09-06 02:34:17 +04:00

README.md

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

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