This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
104 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# encoding: utf-8 | ||
require 'sinatra/base' | ||
require 'signature' | ||
require 'json' | ||
require 'active_support/core_ext/hash' | ||
require 'eventmachine' | ||
require 'em-hiredis' | ||
require 'rack' | ||
require 'fiber' | ||
require 'rack/fiber_pool' | ||
|
||
module Slanger | ||
module Api | ||
class Server < Sinatra::Base | ||
use Rack::FiberPool | ||
set :raise_errors, lambda { false } | ||
set :show_exceptions, false | ||
|
||
# Respond with HTTP 401 Unauthorized if request cannot be authenticated. | ||
error(Signature::AuthenticationError) { |e| halt 401, "401 UNAUTHORIZED: #{e}" } | ||
|
||
before do | ||
validate_request! | ||
end | ||
|
||
post '/apps/:app_id/events' do | ||
socket_id = validated_request.socket_id | ||
data = validated_request.data | ||
|
||
# Event and channel data are now serialized in the JSON data | ||
# So, extract and use it | ||
# Send event to each channel | ||
data["channels"].each { |channel| publish(channel, data['name'], data['data'], socket_id) } | ||
|
||
status 202 | ||
return {}.to_json | ||
end | ||
|
||
post '/apps/:app_id/channels/:channel_id/events' do | ||
params = validated_request.params | ||
|
||
publish(params[:channel_id], params['name'], body) | ||
|
||
status 202 | ||
return {}.to_json | ||
end | ||
|
||
def body | ||
@body ||= request.body.read.tap{|s| s.force_encoding("utf-8")} | ||
end | ||
|
||
def validate_request! | ||
validated_request | ||
end | ||
|
||
def validated_request | ||
@validated_reqest ||= RequestValidation.new(body, params, env["PATH_INFO"]) | ||
end | ||
|
||
def payload(channel, event, data, socket_id) | ||
{ | ||
event: event, | ||
data: data, | ||
channel: channel, | ||
socket_id: socket_id | ||
}.select { |_,v| v }.to_json | ||
end | ||
|
||
|
||
def publish(channel, event, data, socket_id) | ||
Slanger::Redis.publish(channel, payload(channel, event, data, socket_id)) | ||
end | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.