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:           [],
  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.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/upfluence/http/server.rb', line 48

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

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

  @builder = Builder.new do
    use Middleware::RequestStapler
    use Middleware::Logger
    use Middleware::Prometheus
    use Middleware::ApplicationHeaders, base_handler
    use Middleware::HandleException

    if opts[:request_timeout]
      use Rack::Timeout, service_timeout: opts[:request_timeout]
    end

    use Upfluence.error_logger.middleware
    use Prometheus::Middleware::Exporter if opts[:prometheus_endpoint]

    use Rack::ContentLength
    use Rack::Chunked
    use Rack::Lint if Upfluence.env.development?
    use Rack::TempfileReaper
    use Rack::ETag
    use Middleware::CORS if Upfluence.env.development?

    (DEFAULT_MIDDLEWARES + opts[:middlewares]).each do |m|
      m = [m] unless m.is_a?(Array)
      use(*m)
    end

    map '/healthcheck' do
      run(opts[:healthcheck_endpoint] || Endpoint::Healthcheck.new)
    end

    if opts[:base_processor_klass] && base_handler
      map '/base' do
        run_thrift(opts[:base_processor_klass], base_handler)
      end
    end

    map('/debug') { run(Endpoint::Profiler.new) } if opts[:debug]

    instance_eval(&block)
  end

  @handler = Rack::Handler.get(@options[:server])
end

Class Method Details

.requestObject



118
119
120
# File 'lib/upfluence/http/server.rb', line 118

def request
  Thread.current[REQUEST_CONTEXT_KEY]
end

.request=(req) ⇒ Object



122
123
124
# File 'lib/upfluence/http/server.rb', line 122

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

Instance Method Details

#serveObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/upfluence/http/server.rb', line 101

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

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

  @handler.run(@builder, **@options) do |server|
    server.threaded = @options[:threaded] if server.respond_to? :threaded=

    # Thin does not recognize the max_thread argument, howerver it has a
    # threadpool_size setter. Puma on the other hand recognize max_thread.
    if server.respond_to?(:threadpool_size=) && @options[:max_threads]
      server.threadpool_size = @options[:max_threads]
    end
  end
end