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.2.0-pre.4 released
onyxframework/http
142 11 7
Onyx Framework

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

This shard follows Semantic Versioning v2.0.0.

Basic example

Please refer to the documentation available online at 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 |r|
  r.get "/:who" do |env|
    KnockKnock.call(env)
  end
end

logger = Logger.new(STDOUT)
log_handler = Prism::Logger.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 |r|
  r.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.2.0-pre.4
License MIT
Crystal 0.23.1

Authors

Dependencies 3

  • radix ~> 0.3.8
    {'github' => 'luislavena/radix', 'version' => '~> 0.3.8'}
  • shard ~> 0.2.0
    {'github' => 'maiha/shard.cr', 'version' => '~> 0.2.0'}
  • 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