Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
A

angelo

> 编程语言
Open source

Sinatra-like DSL for Reel that supports WebSockets and SSE

300 stars0 likes1 views
WebsiteGitHub

About

Sinatra-like DSL for Reel that supports WebSockets and SSE

Angelo

A Sinatra-like DSL for Reel.

tl;dr

  • websocket support via websocket('/path'){|s| ... } route builder
  • SSE support via eventsource('/path'){|s| ... } route builder
  • contextual websocket/sse stashing via websockets and sses helpers
  • task handling via async and future helpers
  • no rack
  • erb, haml, and markdown support
  • mustermann support

What is Angelo?

Just like Sinatra, Angelo gives you an expressive DSL for creating web applications. There are some notable differences, but the basics remain the same: you can either create a "classic" application by requiring 'angelo/main' and using the DSL at the top level of your script, or a "modular" application by requiring 'angelo', subclassing Angelo::Base, and calling .run! on that class for the service to start. In addition, and perhaps more importantly, Angelo is built on Reel, which is built on Celluloid::IO and gives you a reactor with evented IO in Ruby!

Things will feel very familiar to anyone experienced with Sinatra. You can define route handlers denoted by HTTP verb and path with parameters set from path matching (using Mustermann), the query string, and post body. A route block may return:

  • The body of the response in full as a String.
  • A Hash (or anything that respond_to? :to_json) if the content type is set to :json.
  • Any object that responds to #each(&block) if the transfer encoding is set to :chunked. There is also a chunked_response helper that will take a block, set the transfer encoding, and return an appropriate object.

Angelo also features before and after filter blocks, just like Sinatra. Filters are ordered as defined, and called in that order. When defined without a path, they run for all matched requests. With a path, the path is interpreted as a Mustermann pattern and params are merged. before filters can set instance variables which can be used in the route block and the after filter. For more info on the difference in how after blocks are handled, see the Errors section below for more info.

Websockets!

One of the main motivations for Angelo was the ability to define websocket handlers with ease. Through the addition of a websocket route builder and a websockets helper, Angelo attempts to make it easy for you to build real-time web applications.

Route Builder

The websocket route builder accepts a path and a block, and passes the actual websocket to the block as the only argument. This socket is an instance of Reel's WebSocket class, and, as such, responds to methods like on_message and on_close. A service-wide on_pong handler may be defined to customize the behavior when a pong frame comes back from a connected websocket client.

websockets helper

Angelo includes a "stash" helper for connected websockets. One can `{sses(false).remove_socket es}

sses  0

'hi' end


##### `future` helper

Just like `async`, this comes from Celluloid as well. It behaves exactly like `async`, with the
notable exception of returning a "future" object that you can call `#value` on later to retrieve
the return value of the task. Calling `#value` will "block" until the task is finished, while the
reactor continues to process requests.

```ruby
get '/' do
  # run the task defined above asynchronously, return immediately
  #
  f = future :in_sec, params[:sec], params[:msg]

  # now, block until the task is finished and return the task's value
  # as a response body
  #
  f.value
end

…

ruby
get '/' do
  raise RequestError.new '"foo" is a required parameter' unless params[:foo]
  params[:foo]
end

get '/json' do
  content_type :json
  raise RequestError.new foo: "required!"
  {foo: params[:foo]}
end

get '/not_found' do
  raise RequestError.new 'not found', 404
end

get '/halt' do
  halt 200, "everything's fine"
  raise RequestError.new "won't get here"
end
$ curl -i http://127.0.0.1:4567/
HTTP/1.1 400 Bad Request
Content-Type: text/html
Connection: Keep-Alive
Content-Length: 29

"foo" is a required parameter

$ curl -i http://127.0.0.1:4567/?foo=bar
HTTP/1.1 200 OK
Content-Type: text/html
Connection: Keep-Alive
Content-Length: 3

bar

$ curl -i http://127.0.0.1:4567/json
HTTP/1.1 400 Bad Request
Content-Type: application/json
Connection: Keep-Alive
Content-Length: 29

{"error":{"foo":"required!"}}

$ curl -i http://127.0.0.1:4567/not_found
HTTP/1.1 404 Not Found
Content-Type: text/html
Connection: Keep-Alive
Content-Length: 9

not found

$ curl -i http://127.0.0.1:4567/halt
HTTP/1.1 200 OK
Content-Type: text/html
Connection: Keep-Alive
Content-Length: 18

everything's fine

Tilt / ERB

ruby
class Foo  'some',
    #     'bar' => 'are_good'
    #   }

    @foo = params[:foo]
    @bar = params[:bar]
    erb :index
  end

  before '/:fu/things/*' do

    # `params` is merged with the Mustermann object#params hash, as
    # parsed with the current request path against this before block's
    # pattern. in the route handler, `params[:fu]` is no longer available.

    @fu = params[:fu]
  end

end

…

ruby
require 'angelo/main'

helpers do
  def say_hello
    "Hello"
  end
end

get "/hello" do
  "#{say_hello} to you, too."
end

And the same app in modular style:

ruby
require 'angelo'

class HelloApp < Angelo::Base
  def say_hello
    "Hello"
  end

  get "/hello" do
    "#{say_hello} to you, too."
  end
end

HelloApp.run!

…

ruby
require 'angelo'

class Foo < Angelo::Base

  # just some constants to use in routes later...
  #
  TEST = {foo: "bar", baz: 123, bat: false}.to_json
  HEART = '<3'

  # a flag to know if the :heart task is running
  #
  @@hearting = false

  # you can define instance methods, just like Sinatra!
  #
  def pong; 'pong'; end
  def foo; params[:foo]; end

  # standard HTTP GET handler
  #
  get '/ping' do
    pong
  end

  # standard HTTP POST handler
  #
  post '/foo' do
    foo
  end

  post '/bar' do
    params.to_json
  end

  # emit the TEST JSON value on all :emit_test websockets
  # return the params posted as JSON
  #
  post '/emit' do
    websockets[:emit_test].each {|ws| ws.write TEST}
    params.to_json
  end

  # handle websocket requests at '/ws'
  # stash them in the :emit_test context
  # write 6 messages to the websocket whenever a message is received
  #
  websocket '/ws' do |ws|
    websockets[:emit_test] << ws
    ws.on_message do |msg|
      5.times { ws.write TEST }
      ws.write foo.to_json
    end
  end

  # emit the TEST JSON value on all :other websockets
  #
  post '/other' do
    websockets[:other].each {|ws| ws.write TEST}
    ''
  end

  # stash '/other/ws' connected websockets in the :other context
  #
  websocket '/other/ws' do |ws|
    websockets[:other] << ws
  end

  websocket '/hearts' do |ws|

    # this is a call to Base#async, actually calling
    # the reactor to start the task
    #
    async :hearts unless @@hearting

    websockets[:hearts] << ws
  end

  # this is a call to Base.task, defining the task
  # to perform on the reactor
  #
  task :hearts do
    @@hearting = true
    every(10){ websockets[:hearts].each {|ws| ws.write HEART } }
  end

  post '/in/:sec/sec/:msg' do

    # this is a call to Base#future, telling the reactor
    # do this thing and we'll want the value eventually
    #
    f = future :in_sec, params[:sec], params[:msg]
    f.value
  end

  # define a task on the reactor that sleeps for the given number of
  # seconds and returns the given message
  #
  task :in_sec do |sec, msg|
    sleep sec.to_i
    msg
  end

  # return a chunked response of JSON for 5 seconds
  #
  get '/chunky_json' do
    content_type :json

    # this helper requires a block that takes one arg, the response
    # proc to call with each chunk (i.e. the block that is passed to
    # `#each`)
    #
    chunked_response do |response|
      5.times do
        response.call time: Time.now.to_i
        sleep 1
      end
    end
  end

end

Foo.run!

Contributing

Anyone is welcome to contribute. Conduct is guided by the Contributor Covenant. See code_of_conduct.md.

To contribute to Angelo, please:

  • fork the repository to your GitHub account
  • create a branch for the feature or fix
  • commit your changes to that branch, please include tests if applicable
  • submit a Pull Request back to the main repository's master branch

After review and acceptance, your changes will be merged and noted in CHANGLOG.md.

Testing

Unit tests are done with Minitest. Run them with :

bundle install
rake test

License

Apache 2.0

Name

Why the name "Angelo"? Since the project mimics Sinatra's DSL, I thought it best to keep a reference to The Chairman in the name. It turns out that Frank Sinatra won an Academy Award for his role 'Angelo Maggio' in 'From Here to Eternity'. I appropriated the name since this is like Sinatra on Reel (film).

Issues· 15 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Rubyrealtimerubyserver-sent-eventswebsockets

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言