Reworking the Puma server integration

This commit is contained in:
Chris Cowan 2014-08-28 17:16:06 -07:00
parent 156b597010
commit 3bc216e27e
8 changed files with 142 additions and 19 deletions

1
.ruby-version Normal file
View file

@ -0,0 +1 @@
1.9.3-p547

View file

@ -24,6 +24,9 @@ parser = OptionParser.new do |opts|
opts.on('-p', '--port PORT', 'Kibana port') do |arg|
options[:port] = arg
end
opts.on('-H', '--host HOST', 'Kibana host') do |arg|
options[:host] = arg
end
opts.on('-v', '--version', 'Display version') do |arg|
puts ENV['KIBANA_VERSION'] || 'dev-build'
exit
@ -46,6 +49,9 @@ config = YAML.load(IO.read(options[:config]))
# Set the override for the port
port = (options[:port] || config['port'])
# Set the override for the host
host = (options[:host] || config['host'])
# Set the override for Elasticsaerch
elasticsearch = (options[:elasticsearch] || config['elasticsearch'])
@ -55,14 +61,26 @@ ENV["KIBANA_CONFIG_FILE"] = options[:config]
ENV["KIBANA_ELASTICSEARCH"] = elasticsearch
# Clone argv and then clear it and set up the defaults for the Puma CLI
puma_args = ARGV.clone()
puma_args.clear
puma_args << '--port'
puma_args << port.to_s
puma_args << "#{HERE}/../config/web.ru"
# puma_args = ARGV.clone()
# puma_args.clear
# puma_args << '--tag'
# puma_args << 'kibana'
# puma_args << '-d'
# puma_args << '--port'
# puma_args << port.to_s
# puma_args << "#{HERE}/../config/web.ru"
# gem 'puma', version
# load Gem.bin_path('puma', 'puma', version)
#
cli = Puma::CLI.new puma_args
cli.run
# cli = Puma::CLI.new puma_args
# cli.run
require "#{HERE}/../lib/server"
server_opts = {
:port => port || 5601,
:host => host || '0.0.0.0'
}
Kibana::Server.run(server_opts)

View file

@ -1,3 +1,6 @@
require "rubygems"
require "bundler/setup"
ROOT = File.expand_path("#{File.dirname(__FILE__)}/../")
if ENV['RACK_ENV'] == ('development')
@ -10,13 +13,8 @@ if ENV['RACK_ENV'] == ('production')
CONFIG_PATH = ENV["CONFIG_PATH"]
end
print ENV;
$LOAD_PATH.unshift(ROOT)
require "rubygems"
require "bundler/setup"
# Require the application
require "#{ROOT}/lib/app"

View file

@ -0,0 +1,32 @@
require "rack/commonlogger"
class JSONLogger < Rack::CommonLogger
def log(env, status, header, begin_at)
now = Time.now
length = extract_content_length(header)
data = {
"@timestamp" => now.iso8601,
:status => status.to_s[0..3],
:request_method => env["REQUEST_METHOD"],
:request => env["PATH_INFO"] + (env["QUERY_STRING"].empty? ? "" : "#{env['QUERY_STRING']}"),
:path => env["PATH_INFO"],
:query_string => env["QUERY_STRING"].empty? ? "" : "#{env['QUERY_STRING']}",
:remote_addr => env['HTTP_X_FORWARD_FOR'] || env["REMOTE_ADDR"],
:remote_user => env["REMOTE_USER"],
:http_version => env["HTTP_VERSION"],
:content_length => length,
:response_time => (now - begin_at) * 1000 # convert to milliseconds
}
logger = @logger || env['rack.errors']
msg = data.to_json+"\n"
if logger.respond_to?(:write)
logger.write(msg)
else
logger << msg
end
end
end

View file

@ -1,14 +1,23 @@
# Add the root of the project to the $LOAD_PATH, For some reason it seems
# to be getting lost when we use warble to make the jar. This fixes it :D
$LOAD_PATH.unshift(ROOT)
$LOAD_PATH.unshift(ENV['KIBANA_ROOT'])
require "rack/reverse_proxy"
require "logger"
require "routes/home"
require "routes/proxy"
require "json"
require "lib/JSONLogger"
class Logger
alias_method :write, :<<
end
module Kibana
class App < Sinatra::Base
configure do
logger = Logger.new(STDOUT)
use JSONLogger, logger
end
# Routes go here
use Routes::Home
use Routes::Proxy

65
src/server/lib/server.rb Normal file
View file

@ -0,0 +1,65 @@
require "rubygems"
require "bundler/setup"
require "puma"
require "json"
ENV['KIBANA_ROOT'] = File.expand_path("#{File.dirname(__FILE__)}/../")
if ENV['RACK_ENV'] == ('production')
ENV['PUBLIC_ROOT'] = File.expand_path("#{File.dirname(__FILE__)}/../public/")
end
if ENV['RACK_ENV'].nil? || ENV['RACK_ENV'] == ('development')
ENV['PUBLIC_ROOT'] = File.expand_path("#{File.dirname(__FILE__)}/../../kibana/")
ENV['CONFIG_PATH'] = File.expand_path("#{File.dirname(__FILE__)}/../config/kibana.yml")
end
$LOAD_PATH.unshift(ENV['KIBANA_ROOT'])
# Require the application
require "#{ENV['KIBANA_ROOT']}/lib/app"
module Kibana
module Server
DEFAULTS = {
:host => '0.0.0.0',
:port => 5601,
:threads => '0:16',
:verbose => false
}
def self.log(msg)
data = { "@timestamp" => Time.now.iso8601, :message => msg }
puts data.to_json
end
def self.run(options = {})
options = DEFAULTS.merge(options)
min, max = options[:threads].split(':', 2)
app = Kibana::App.new()
server = Puma::Server.new(app)
# Configure server
server.add_tcp_listener(options[:host], options[:port])
server.min_threads = min
server.max_threads = max
yield server if block_given?
begin
log("Kibana server started on tcp://#{options[:host]}:#{options[:port]} in #{ENV['RACK_ENV']} mode.")
server.run.join
rescue Interrupt
log("Kibana server gracefully stopping, waiting for requests to finish")
server.stop(true)
log("Kibana server stopped.")
end
end
end
end

View file

@ -9,11 +9,11 @@ module Kibana
helpers Sinatra::JSON
configure do
set :root, ROOT
set :public_folder, PUBLIC_ROOT
set :root, ENV['KIBANA_ROOT']
set :public_folder, ENV['PUBLIC_ROOT']
set :httponly, true
config = YAML.load(IO.read(CONFIG_PATH))
config = YAML.load(IO.read(ENV['CONFIG_PATH']))
set :config, config
config['elasticsearch'] = ENV['KIBANA_ELASTICSEARCH']
config['port'] = ENV['KIBANA_PORT'].to_i

View file

@ -5,7 +5,7 @@ module Kibana
class Home < Base
get "/" do
File.read(File.join(PUBLIC_ROOT, 'index.html'))
File.read(File.join(ENV['PUBLIC_ROOT'], 'index.html'))
end
get "/config" do