Elegant HTTP DSL
Go to file
Yessiest f2e995dcc0 fixed double socket close in rescue 2024-07-09 17:26:08 +04:00
examples Added ability to IO.select websockets 2024-07-08 18:26:45 +04:00
lib fixed double socket close in rescue 2024-07-09 17:26:08 +04:00
test Rebranding & better cookie DSL 2023-09-19 22:58:15 +04:00
.gitignore bumped version 2023-11-11 20:42:48 +04:00
.yardopts Module restructuring, minor bugfixes, first template engine (incomplete), template engine example 2023-09-13 02:23:23 +04:00
COPYING.txt License and README 2023-05-05 00:00:47 +04:00
HACKING.md Modified guidelines, bumped version in library 2023-10-22 01:44:33 +04:00
LICENSE.md Hardened header parsing, basic cookie functionality, restructuring of Util files, proper encoding handling in formdata parser, other minor changes 2023-09-17 01:32:55 +04:00
README.md Fixed copyright dates, added 2024 as the year of latest update 2024-05-07 22:21:01 +04:00
STRUCTURE.md added some clarification docs 2024-07-08 16:34:04 +04:00
landline.gemspec version bump 2024-05-11 23:26:47 +04:00

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

  get "/hello" do
    "Hello world!"
  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) 2023-2024 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/>.