Skip to content

Instantly share code, notes, and snippets.

@luislavena
Created May 3, 2017 22:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luislavena/1445fa990eea7f2607994674540d72c0 to your computer and use it in GitHub Desktop.
Save luislavena/1445fa990eea7f2607994674540d72c0 to your computer and use it in GitHub Desktop.
require "../src/beryl"
# This sample showcases how to use a separate action
class HelloRouter < Beryl::Router
routing do
get "/", HelloAction
end
end
class HelloAction
include Beryl::Action
# You have access to the following:
# - The original context (`HTTP::Server::Context`) as `context`
# - The request as `request`
# - The response as `response`
def call
response.headers["Content-Type"] = "text/plain"
response.puts "Hello World!"
end
end
# HTTP::Server requires an instance of our Router
server = HTTP::Server.new(9292, HelloRouter.new)
Signal::INT.trap {
server.close
exit
}
puts "Server ready at http://localhost:#{server.port}"
server.listen
require "../src/beryl"
# This sample showcases how to use an inline action within a Router
class HelloRouter < Beryl::Router
routing do
get "/" do |ctx|
# here you have access to `HTTP::Server::Context` to both `request` and
# `response`:
ctx.response.headers["Content-Type"] = "text/plain"
ctx.response.puts "Hello World!"
# access to Beryl's specific details (like `params`) is done through
# `beryl`:
if id = ctx.beryl.params.query["id"]?
end
end
end
end
# HTTP::Server requires an instance of our Router
server = HTTP::Server.new(9292, HelloRouter.new)
Signal::INT.trap {
server.close
exit
}
puts "Server ready at http://localhost:#{server.port}"
server.listen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment