Class: Upfluence::HTTP::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/upfluence/http/server.rb

Constant Summary collapse

REQUEST_CONTEXT_KEY =
:uhttp_request_context
DEFAULT_MIDDLEWARES =
[]
DEFAULT_OPTIONS =
{
  server:                :puma,
  Port:                  ENV.fetch('PORT', 8080),
  Host:                  '0.0.0.0',
  threaded:              true,
  interfaces:            [],
  push_gateway_url:      ENV.fetch('PUSH_GATEWAY_URL', nil),
  push_gateway_interval: 15, # sec
  prometheus_endpoint:   ENV.fetch('PUSH_GATEWAY_URL', nil).eql?(nil),
  app_name:              ENV.fetch('APP_NAME', 'uhttp-rb-server'),
  unit_name:             ENV.fetch('UNIT_NAME','uhttp-rb-server-anonymous'),
  base_processor_klass:  nil,
  base_handler_klass:    nil,
  max_threads:           ENV.fetch('HTTP_SERVER_MAX_THREADS', 5).to_i,
  request_timeout:       ENV['HTTP_SERVER_REQUEST_TIMEOUT']&.to_i,
  middlewares:           [],
  instrumentations:      [
    Instrumentation::PumaInstrumenter.new,
    Instrumentation::GCInstrumenter.new,
    Instrumentation::ActiveRecordPoolInstrumenter.new
  ],
  admin_port:            ENV.fetch('ADMIN_PORT', nil),
  debug:                 ENV.fetch('DEBUG', nil)
}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ Server

Returns a new instance of Server.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/upfluence/http/server.rb', line 55

def initialize(options = {}, &block)
  @options = DEFAULT_OPTIONS.dup.merge(options)
  opts = @options

  if opts[:base_handler_klass]
    @base_handler = opts[:base_handler_klass].new(opts[:interfaces])
  end

  @admin_builder = admin_builder(opts) if opts[:admin_port]
  @production_builder = production_builder(opts, &block)
  @handler = Rack::Handler.get(opts[:server])
end

Class Method Details

.requestObject



91
92
93
# File 'lib/upfluence/http/server.rb', line 91

def request
  Thread.current[REQUEST_CONTEXT_KEY]
end

.request=(req) ⇒ Object



95
96
97
# File 'lib/upfluence/http/server.rb', line 95

def request=(req)
  Thread.current[REQUEST_CONTEXT_KEY] = req
end

Instance Method Details

#serveObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/upfluence/http/server.rb', line 68

def serve
  ENV['RACK_ENV'] = Upfluence.env.to_s

  Thread.new { run_prometheus_exporter } if @options[:push_gateway_url]

  @options[:instrumentations].each(&:start)

  if @admin_builder
    admin_port = @options[:admin_port].to_i

    Thread.new do
      run_builder(@admin_builder, Port: admin_port)
    end

    Upfluence.logger.info(
      "Admin server listening on #{@options[:Host]}:#{admin_port}"
    )
  end

  run_builder(@production_builder)
end