summaryrefslogtreecommitdiff
path: root/lib/nickserver
diff options
context:
space:
mode:
Diffstat (limited to 'lib/nickserver')
-rw-r--r--lib/nickserver/adapters/http.rb21
-rw-r--r--lib/nickserver/daemon.rb2
-rw-r--r--lib/nickserver/dispatcher.rb11
-rw-r--r--lib/nickserver/logging_responder.rb18
-rw-r--r--lib/nickserver/reel_server.rb24
-rw-r--r--lib/nickserver/source.rb4
6 files changed, 69 insertions, 11 deletions
diff --git a/lib/nickserver/adapters/http.rb b/lib/nickserver/adapters/http.rb
new file mode 100644
index 0000000..b0ba728
--- /dev/null
+++ b/lib/nickserver/adapters/http.rb
@@ -0,0 +1,21 @@
+require 'nickserver/adapters'
+require 'nickserver/config'
+require 'http'
+
+module Nickserver::Adapters
+ class Http
+
+ def get(url, options = {})
+ response = HTTP.get url,
+ params: options[:query],
+ ssl_context: ctx
+ return response.code, response.to_s
+ end
+
+ def ctx
+ OpenSSL::SSL::SSLContext.new.tap do |ctx|
+ ctx.ca_file = Nickserver::Config.hkp_ca_file
+ end
+ end
+ end
+end
diff --git a/lib/nickserver/daemon.rb b/lib/nickserver/daemon.rb
index 9cc1af7..12ed30e 100644
--- a/lib/nickserver/daemon.rb
+++ b/lib/nickserver/daemon.rb
@@ -176,6 +176,7 @@ module Nickserver
$stdout.reopen(log_path, 'a')
$stderr.reopen $stdout
$stdout.sync = true
+ $stderr.sync = true
else
# redirect to /dev/null
$stdin.reopen '/dev/null'
@@ -255,6 +256,7 @@ module Nickserver
puts "\nShutting down..."
exit(0)
end
+ Config.log_file = STDOUT
yield
exit(0)
end
diff --git a/lib/nickserver/dispatcher.rb b/lib/nickserver/dispatcher.rb
index 869f721..71e71cf 100644
--- a/lib/nickserver/dispatcher.rb
+++ b/lib/nickserver/dispatcher.rb
@@ -39,10 +39,6 @@ module Nickserver
def handle(request)
handler_chain.handle request
- rescue RuntimeError => exc
- puts "Error: #{exc}"
- puts exc.backtrace
- ErrorResponse.new(exc.to_s)
end
def handler_chain
@@ -62,9 +58,10 @@ module Nickserver
end
def proxy_error_response
- exception = handler_chain.rescued_exceptions.first
- if exception
- Nickserver::Response.new(502, exception.to_s)
+ exc = handler_chain.rescued_exceptions.first
+ if exc
+ Nickserver::Response.new 502,
+ JSON.dump(error: exc.to_s)
end
end
diff --git a/lib/nickserver/logging_responder.rb b/lib/nickserver/logging_responder.rb
new file mode 100644
index 0000000..6eb756f
--- /dev/null
+++ b/lib/nickserver/logging_responder.rb
@@ -0,0 +1,18 @@
+module Nickserver
+ class LoggingResponder
+
+ def initialize(responder, logger)
+ @responder = responder
+ @logger = logger
+ end
+
+ def respond(status, body)
+ logger.info " -> #{status}"
+ responder.respond(status, body)
+ end
+
+ protected
+
+ attr_reader :responder, :logger
+ end
+end
diff --git a/lib/nickserver/reel_server.rb b/lib/nickserver/reel_server.rb
index d2a95ba..c378aca 100644
--- a/lib/nickserver/reel_server.rb
+++ b/lib/nickserver/reel_server.rb
@@ -1,8 +1,11 @@
silence_warnings do
require 'reel'
end
+require 'logger'
+require 'nickserver/config'
require 'nickserver/adapters/celluloid_http'
require 'nickserver/dispatcher'
+require 'nickserver/logging_responder'
module Nickserver
class ReelServer < Reel::Server::HTTP
@@ -12,6 +15,7 @@ module Nickserver
end
def initialize(host = "127.0.0.1", port = 3000)
+ Celluloid.logger = logger
super(host, port, &method(:on_connection))
end
@@ -23,17 +27,28 @@ module Nickserver
def on_connection(connection)
connection.each_request do |request|
- handler = handler_for(request)
- handler.respond_to params(request), request.headers
+ handle_request(request)
end
end
protected
+ def handle_request(request)
+ logger.info "#{request.method} #{request.uri}"
+ logger.debug " #{params(request)}"
+ handler = handler_for(request)
+ handler.respond_to params(request), request.headers
+ rescue StandardError => e
+ logger.error e
+ logger.error e.backtrace.join "\n "
+ request.respond 500, "{}"
+ end
+
def handler_for(request)
# with reel the request is the responder
- Dispatcher.new(request)
+ responder = LoggingResponder.new(request, logger)
+ Dispatcher.new(responder)
end
def params(request)
@@ -44,5 +59,8 @@ module Nickserver
end
end
+ def logger
+ @logger ||= ::Logger.new Config.log_file
+ end
end
end
diff --git a/lib/nickserver/source.rb b/lib/nickserver/source.rb
index edc57e9..dc0669a 100644
--- a/lib/nickserver/source.rb
+++ b/lib/nickserver/source.rb
@@ -3,7 +3,9 @@ require 'nickserver/adapters/celluloid_http'
module Nickserver
class Source
- def initialize(adapter = Nickserver::Adapters::CelluloidHttp.new)
+ DEFAULT_ADAPTER_CLASS = Nickserver::Adapters::CelluloidHttp
+
+ def initialize(adapter = DEFAULT_ADAPTER_CLASS.new)
@adapter = adapter
end