onyx-http
Prism is an expressive action-oriented modular web framework.
Features
- Modular approach avoiding singleton configurations.
- Safe params typecasting and validation.
- Expressive actions definition.
- ⚡️ Websockets built-in.
Installation
Add this to your application's shard.yml
:
dependencies:
prism:
github: vladfaust/prism
version: ~> 0.2.0 # See actual version in releases
This shard follows Semantic Versioning v2.0.0, so check releases and change the version
accordingly.
Basic example
Please refer to the API documentation available online at github.vladfaust.com/prism.
require "prism"
struct KnockKnock < Prism::Action
include Params
params do
param :who, String
param :times, Int32, validate: {max: 10}
end
def call
params[:times].times do
text("Knock-knock #{who}\n")
end
end
end
router = Prism::Router.new do
get "/:who", KnockKnock
end
logger = Logger.new(STDOUT)
log_handler = Prism::LogHandler.new(logger)
handlers = [log_handler, router]
server = Prism::Server.new("localhost", 5000, handlers, logger)
server.listen
# INFO -- : Prism server v0.1.0 is listening on http://localhost:5000...
Websockets example
We call them Channels for convenience.
require "prism"
class Notifications < Prism::Channel
@@subscriptions = Array(self).new
def self.notify(message)
@@subscriptions.each &.socket.send(message)
end
def on_open
socket.send("Hello")
@@subscriptions.push(self)
end
def on_close
@@subscriptions.delete(self)
end
end
router = Prism::Router.new do
ws "/notifications" do |socket, env|
Notifications.subscribe(socket, env)
end
end
# Later in the code...
Notifications.notify("Something happened!") # Will notify all subscribers binded to this particular Crystal process
Contributing
- Fork it ( https://github.com/vladfaust/prism/fork )
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create a new Pull Request
Contributors
- @vladfaust Vlad Faust - creator, maintainer