Class: Upfluence::HTTP::Server

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

Constant Summary collapse

REQUEST_CONTEXT_KEY =
:uhtt_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,
  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.



42
43
44
45
46
47
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
# File 'lib/upfluence/http/server.rb', line 42

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 Prometheus::Middleware::Exporter if opts[:prometheus_endpoint]
    use Middleware::ApplicationHeaders, base_handler
    use Middleware::HandleException
    use Upfluence.error_logger.middleware
    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.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



106
107
108
# File 'lib/upfluence/http/server.rb', line 106

def request
  Thread.current[REQUEST_CONTEXT_KEY]
end

.request=(req) ⇒ Object



110
111
112
# File 'lib/upfluence/http/server.rb', line 110

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

Instance Method Details

#serveObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/upfluence/http/server.rb', line 89

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