summaryrefslogtreecommitdiff
path: root/lib/nickserver/em_server.rb
diff options
context:
space:
mode:
authorazul <azul@riseup.net>2016-06-21 11:54:32 +0200
committerGitHub <noreply@github.com>2016-06-21 11:54:32 +0200
commit27196b87e8d0ce5325381ea96ccd68ced8ee2e4d (patch)
tree331cfb3d07f49f1e12e5cbf50f63e80692e0868f /lib/nickserver/em_server.rb
parent3c6dc3d7b902b46b82a3d8cd38bd3fa896024beb (diff)
parentb185742d959a0a3854c5e628354a538a15bdea15 (diff)
Merge pull request #4 from azul/refactor/separate-em-server
refactor: separate em specifics from generic server
Diffstat (limited to 'lib/nickserver/em_server.rb')
-rw-r--r--lib/nickserver/em_server.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/nickserver/em_server.rb b/lib/nickserver/em_server.rb
new file mode 100644
index 0000000..bcec4cd
--- /dev/null
+++ b/lib/nickserver/em_server.rb
@@ -0,0 +1,51 @@
+require 'eventmachine'
+silence_warnings do
+ require 'evma_httpserver'
+end
+require 'nickserver/request_handler'
+
+module Nickserver
+ class EmServer < EM::Connection
+ include EM::HttpServer
+
+ def self.start(options = {})
+ EventMachine.run do
+ EM.start_server options[:host], options[:port], self
+ end
+ end
+
+ def post_init
+ super
+ no_environment_strings
+ end
+
+ def process_http_request
+ handler.respond_to params, @http_headers
+ end
+
+ def send_response(options = {})
+ response = EM::DelegatedHttpResponse.new(self)
+ response.status = options[:status]
+ response.content_type options[:content_type]
+ response.content = options[:content]
+ silence_warnings do
+ response.send_response
+ end
+ end
+
+ private
+
+ def handler
+ @handler ||= RequestHandler.new(self, Nickserver::Adapters::EmHttp.new)
+ end
+
+ def params
+ if @http_query_string
+ CGI.parse(@http_query_string)
+ elsif @http_post_content
+ CGI.parse(@http_post_content)
+ end
+ end
+
+ end
+end