onyx-http

REST API framework with type-safe params and separate business and rendering layers, based on onyx-http web framework router websockets modular http-handler onyxframework
0.4.0-beta.2 released
onyxframework/http
141 11 7
Onyx Framework

Prism

Built with Crystal Build status Docs Releases Awesome vladfaust.com

Prism is an expressive web framework with a speed of light. Features:

  • ⚡️ Efficiency based on Crystal performance
  • Expressiveness with powerful DSL and lesser code
  • 💼 Safety with strictly typed params

Installation

Add this to your application's shard.yml:

dependencies:
  prism:
    github: vladfaust/prism
    version: ~> 0.4.0

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
  include Prism::Action
  include Prism::Action::Params

  params do
    type who : String
    type how_many_times : Int32, validate: {lte: 10}
  end

  def call
    params[:how_many_times].times do
      text("Knock-knock #{params[:who]}\n")
    end
  end
end

router = Prism::Router.new do
  get "/:who", KnockKnock
end

logger = Logger.new(STDOUT, Logger::DEBUG)
log_handler = Prism::LogHandler.new(logger)
handlers = [log_handler, router]

server = Prism::Server.new(handlers, logger)
server.bind_tcp(5000)
server.listen

#  INFO -- :   Prism::Server is listening on http://127.0.0.1:5000...
# DEBUG -- :     GET /me 200 177μs
curl -X GET -d "howManyTimes=2" http://127.0.0.1:5000/me
Knock-knock me
Knock-knock me

Websockets example

We call them Channels for convenience.

require "prism"

class Notifications
  include 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

  1. Fork it ( https://github.com/vladfaust/prism/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

onyx-http:
  github: onyxframework/http
  version: ~> 0.4.0-beta.2
License MIT
Crystal 0.26.0

Authors

Dependencies 3

  • callbacks ~> 0.1.0
    {'github' => 'vladfaust/callbacks.cr', 'version' => '~> 0.1.0'}
  • radix ~> 0.3.8
    {'github' => 'luislavena/radix', 'version' => '~> 0.3.8'}
  • time_format ~> 0.1.0
    {'github' => 'vladfaust/time_format.cr', 'version' => '~> 0.1.0'}

Development Dependencies 0

Dependents 2

Last synced .
search fire star recently