Class: ThriftRack

Inherits:
Object
  • Object
show all
Defined in:
lib/thrift_rack.rb,
lib/thrift_rack/atom.rb,
lib/thrift_rack/ping.rb,
lib/thrift_rack/client.rb,
lib/thrift_rack/logger.rb,
lib/thrift_rack/sentry.rb,
lib/thrift_rack/server.rb,
lib/thrift_rack/version.rb,
lib/thrift_rack/downgrade.rb,
lib/thrift_rack/format_check.rb,
lib/thrift_rack/active_record.rb,
lib/thrift_rack/server_metric.rb,
lib/thrift_rack/launch_timestamp.rb,
lib/thrift_rack/http_client_transport.rb

Defined Under Namespace

Classes: ActiveRecord, Atom, Client, Downgrade, FormatCheck, HttpClientTransport, LaunchTimestamp, Logger, Ping, Sentry, Server, ServerMetric

Constant Summary collapse

THRIFT_HEADER =
"application/x-thrift".freeze
VERSION =
"0.2.6"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(servers = nil) ⇒ ThriftRack

Returns a new instance of ThriftRack.



22
23
24
25
26
27
28
# File 'lib/thrift_rack.rb', line 22

def initialize(servers = nil)
  servers ||= ThriftRack::Server.children
  @maps = {}
  servers.each do |server|
    @maps[server.mount_path] = server
  end
end

Class Attribute Details

.redisObject



65
66
67
# File 'lib/thrift_rack.rb', line 65

def redis
  @redis ||= Redis.new
end

Class Method Details

.app(servers = nil) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/thrift_rack.rb', line 50

def app(servers = nil)
  Rack::Builder.new(ThriftRack.new(servers)) do
    use ThriftRack::LaunchTimestamp
    use ThriftRack::ActiveRecord if defined? ::ActiveRecord::Base
    # use(ActionDispatch::Executor, ::Rails.application.executor) if defined? ::Rails
    use ThriftRack::Downgrade
    use ThriftRack::Ping
    use ThriftRack::FormatCheck
    use ThriftRack::Atom
    use ThriftRack::Logger
    use ThriftRack::Sentry if defined? Raven
    use ThriftRack::ServerMetric
  end
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/thrift_rack.rb', line 30

def call(env)
  req = Rack::Request.new(env)
  Thread.current["request"] = req
  server_class = @maps[req.path]
  return 400, { 'Content-Type' => 'text/plain' }, ["No Thrift Server For #{req.path}"] unless server_class

  resp = Rack::Response.new([], 200, { 'Content-Type' => THRIFT_HEADER })

  transport = Thrift::IOStreamTransport.new req.body, resp
  protocol = server_class.protocol_factory.get_protocol transport
  server_class.processor_class.new(server_class.new(req)).process(protocol, protocol)

  resp_a = resp.to_a
  [resp_a[0], resp_a[1], [resp_a[2].join]]
ensure
  Thread.current["request"] = nil
end