Allow for others to plug-over us

Stephen Paul Weber created

Defer executing the event loop until exit in case someone `require`s us
and wants to install extra stuff before we run.

Some small tweaks to the APIs to make the coming fwdcalls plugover
easier.

Also, normalise to use REDIS_URL environment variable instead of passing
host/port in args.

Change summary

.rubocop.yml    |   4 +
sgx-catapult.rb | 104 +++++++++++++++++++++++++++-----------------------
2 files changed, 61 insertions(+), 47 deletions(-)

Detailed changes

.rubocop.yml 🔗

@@ -81,6 +81,10 @@ Style/Next:
 Style/Not:
   Enabled: false
 
+Style/NumericLiterals:
+  MinDigits: 20
+  Strict: true
+
 Style/NumericPredicate:
   Enabled: false
 

sgx-catapult.rb 🔗

@@ -33,22 +33,6 @@ require 'log4r'
 
 require_relative 'em_promise'
 
-$stdout.sync = true
-
-puts "Soprani.ca/SMS Gateway for XMPP - Catapult\n"\
-	"==>> last commit of this version is " + `git rev-parse HEAD` + "\n"
-
-if ARGV.size != 9
-	puts "Usage: sgx-catapult.rb <component_jid> <component_password> "\
-		"<server_hostname> <server_port> "\
-		"<redis_hostname> <redis_port> <delivery_receipt_url> "\
-		"<http_listen_port> <mms_proxy_prefix_url>"
-	exit 0
-end
-
-t = Time.now
-puts "LOG %d.%09d: starting...\n\n" % [t.to_i, t.nsec]
-
 def panic(e)
 	puts "Shutting down gateway due to exception: #{e.message}"
 	puts e.backtrace
@@ -143,7 +127,8 @@ module SGXcatapult
 	end
 
 	def self.call_catapult(
-		token, secret, m, pth, body=nil, head={}, code=[200]
+		token, secret, m, pth, body=nil,
+		head={}, code=[200], respond_with=:body
 	)
 		EM::HttpRequest.new(
 			"https://api.catapult.inetwork.com/#{pth}"
@@ -158,7 +143,14 @@ module SGXcatapult
 				" response.code #{http.response_header.status}"
 
 			if code.include?(http.response_header.status)
-				http.response
+				case respond_with
+				when :body
+					http.response
+				when :headers
+					http.response_header
+				else
+					http
+				end
 			else
 				EMPromise.reject(http.response_header.status)
 			end
@@ -173,7 +165,7 @@ module SGXcatapult
 		else
 			{
 				receiptRequested: 'all',
-				callbackUrl:      ARGV[6]
+				callbackUrl:      ARGV[4]
 			}
 		end
 
@@ -790,16 +782,6 @@ module SGXcatapult
 	end
 end
 
-[:INT, :TERM].each do |sig|
-	trap(sig) {
-		puts 'Shutting down gateway...'
-		SGXcatapult.shutdown
-		puts 'Gateway has terminated.'
-
-		EM.stop
-	}
-end
-
 class ReceiptMessage < Blather::Stanza
 	def self.new(to=nil)
 		node = super :message
@@ -809,12 +791,14 @@ class ReceiptMessage < Blather::Stanza
 end
 
 class WebhookHandler < Goliath::API
+	use Goliath::Rack::Params
+
 	def send_media(from, to, media_url)
 		# we assume media_url is of the form (always the case so far):
 		#  https://api.catapult.inetwork.com/v1/users/[uid]/media/[file]
 
 		# the caller must guarantee that 'to' is a bare JID
-		proxy_url = ARGV[8] + to + '/' + media_url.split('/', 8)[7]
+		proxy_url = ARGV[6] + to + '/' + media_url.split('/', 8)[7]
 
 		puts 'ORIG_URL: ' + media_url
 		puts 'PROX_URL: ' + proxy_url
@@ -846,8 +830,6 @@ class WebhookHandler < Goliath::API
 
 	def response(env)
 		puts 'ENV: ' + env.to_s
-		body = Rack::Request.new(env).body.read
-		params = JSON.parse body
 
 		users_num = ''
 		others_num = ''
@@ -1016,23 +998,51 @@ class WebhookHandler < Goliath::API
 	end
 end
 
-EM.run do
-	REDIS = EM::Hiredis.connect("redis://#{ARGV[4]}:#{ARGV[5]}/0")
+at_exit do
+	$stdout.sync = true
 
-	SGXcatapult.run
+	puts "Soprani.ca/SMS Gateway for XMPP - Catapult\n"\
+		"==>> last commit of this version is " + `git rev-parse HEAD` + "\n"
 
-	# required when using Prosody otherwise disconnects on 6-hour inactivity
-	EM.add_periodic_timer(3600) do
-		msg = Blather::Stanza::Iq::Ping.new(:get, 'localhost')
-		msg.from = ARGV[0]
-		SGXcatapult.write(msg)
+	if ARGV.size != 7
+		puts "Usage: sgx-catapult.rb <component_jid> "\
+			"<component_password> <server_hostname> "\
+			"<server_port> <delivery_receipt_url> "\
+			"<http_listen_port> <mms_proxy_prefix_url>"
+		exit 0
 	end
 
-	server = Goliath::Server.new('0.0.0.0', ARGV[7].to_i)
-	server.api = WebhookHandler.new
-	server.app = Goliath::Rack::Builder.build(server.api.class, server.api)
-	server.logger = Log4r::Logger.new('goliath')
-	server.logger.add(Log4r::StdoutOutputter.new('console'))
-	server.logger.level = Log4r::INFO
-	server.start
+	t = Time.now
+	puts "LOG %d.%09d: starting...\n\n" % [t.to_i, t.nsec]
+
+	EM.run do
+		REDIS = EM::Hiredis.connect
+
+		SGXcatapult.run
+
+		# required when using Prosody otherwise disconnects on 6-hour inactivity
+		EM.add_periodic_timer(3600) do
+			msg = Blather::Stanza::Iq::Ping.new(:get, 'localhost')
+			msg.from = ARGV[0]
+			SGXcatapult.write(msg)
+		end
+
+		server = Goliath::Server.new('0.0.0.0', ARGV[5].to_i)
+		server.api = WebhookHandler.new
+		server.app = Goliath::Rack::Builder.build(server.api.class, server.api)
+		server.logger = Log4r::Logger.new('goliath')
+		server.logger.add(Log4r::StdoutOutputter.new('console'))
+		server.logger.level = Log4r::INFO
+		server.start do
+			["INT", "TERM"].each do |sig|
+				trap(sig) do
+					puts 'Shutting down gateway...'
+					SGXcatapult.shutdown
+
+					puts 'Gateway has terminated.'
+					EM.stop
+				end
+			end
+		end
+	end
 end