Elegant HTTP DSL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Yessiest dd791ca5d1 bumped version in gemspect 3 hours ago
examples Websockets (experimental) 3 hours ago
lib Websockets (experimental) 3 hours ago
test Rebranding & better cookie DSL 7 months ago
.gitignore bumped version 6 months ago
.yardopts Module restructuring, minor bugfixes, first template engine (incomplete), template engine example 8 months ago
COPYING.txt License and README 12 months ago
HACKING.md Modified guidelines, bumped version in library 6 months ago
LICENSE.md Hardened header parsing, basic cookie functionality, restructuring of Util files, proper encoding handling in formdata parser, other minor changes 7 months ago
README.md Websockets (experimental) 3 hours ago
landline.gemspec bumped version in gemspect 3 hours ago

README.md

Landline - an HTTP DSL

Landline is a library that provides a minimalistic DSL for creating web services. It doesn't include patterns, middleware, or anything that could be considered application logic, besides the basic primitives It does a few things, and hopefully it does them well.

Landline 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. Since then, it has somewhat matured, and while it's still not entirely advisable to use in production due to the experimental nature of the framework, you're free to test it, see if it works, and if it doesn't, supply (hopefully constructive) criticism and/or suggestions.

Examples

A simple "Hello, World!" app using Landline

require 'landline'

app = Landline::Server.new do
  get "/hello" do
    header "content-type", "text/plain"
    "Hello world!"
  end
end

run app

A push/pull stack as an app

require 'landline'

stack = []

app = Landline::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

Several push/pull buckets

require 'landline'

stack = { "1" => [], "2" => [], "3" => [] }

app = Landline::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

Static file serving with nginx-like syntax and file globbing (Note: index applies only to /var/www (to the path its defined in))

require 'landline'

app = Landline::Server.new do
  root "/var/www"
  index ["index.html","index.htm"]
  serve "**/*.(html|htm)"
end

run app

Preprocessing requests on a subset of handlers

require 'landline'

app = Landline::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

Class interface with middleware support

require 'landline'

class TimerMiddleware
  def initialize(app)
    @app = app
  end

  def call(*data)
    puts("Request accepted")
    before = Time.now
    output = @app.call(*data)
    puts("Time elapsed: #{(Time.now - before) * 1000}ms")
    output
  end
end

class Application < Landline::App
  use TimerMiddleware

  setup do
    get "/hello" do
      "Hello world!"
    end
  end
end

run Application.new

And a lot more to be found in /examples in this repo.

Design goals

Out of the box, Landline is designed to do the following:

  • Routing HTTP requests to handlers
  • Processing HTTP requests (cookies, headers, etc.)
  • Filtering, preprocessing, postprocessing requests
  • Deferring block execution until after the request gets processed
  • Creating responses from templates using various template engines
  • Parsing and handling forms and queries
  • Connecting multiple Landline Rack applications together
  • Sending files using nginx-style configuration system
  • A lot of basic Rack things such as plugging in middleware, BEING middleware, or sending Rack-compatible environment to another Rack application

As such, the library is pretty thin and can be used to build more complex applications. This is intentional, and hopefully will remain that way.

Additionally, there are a few extra things landline can do, which can be used by requireing from the landline/extensions directory. Please note, however, that some of that functionality may require additional dependencies, which would otherwise be optional. This functionality includes:

  • PHP-like Session handling (via landline/extensions/session)
  • Websockets (via landline/extensions/websockets) (available for testing)
  • (Probably something else eventually)

Landline is built entirely on Rack webserver interface, while being agnostic to any and all underlying webservers (such as Puma, Thin, Unicorn and such). For the foreseeable future, this design decision will not change.

Name

The name is, quite literally, a metaphor for request routing.

Documentation

Documentation can be generated using yard doc. For things to render correctly, please install the redcarpet gem.

License

    Landline - an HTTP request pattern matching system 
    Copyright (C) 2022 yessiest (yessiest@text.512mb.org)

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