From 0e9b9b990e0c7f8f218573ab2db63f1517f82ce3 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 15 Sep 2017 12:07:33 +0900 Subject: [PATCH 1/3] Enable log on / off --- lib/server.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/server.js b/lib/server.js index 2488fb2..7f6211b 100644 --- a/lib/server.js +++ b/lib/server.js @@ -8,13 +8,14 @@ class SimplyImitatedSQSHttpServer { constructor () { this.server = null this.sqs = new SimplyImitatedSQS() + this.logging = false } signalHandle () { const signals = ['SIGUSR1', 'SIGUSR2', 'SIGTERM', 'SIGINT', 'SIGHUP'] signals.forEach((signal) => { process.on(signal, () => { - console.log('exit') + this.log_('exit') this.sqs.clear() process.exit(signal) }) @@ -76,7 +77,14 @@ class SimplyImitatedSQSHttpServer { return `http://${host}:${port}` } + log_ (message) { + if (!this.logging) return + console.log(message) + } + run (options) { + if (options != null) this.logging = !!options.logging + this.signalHandle() this.server = http.createServer((request, response) => { let postData = '' @@ -86,12 +94,12 @@ class SimplyImitatedSQSHttpServer { request.on('end', () => { const params = querystring.parse(postData) - console.log('access:', JSON.stringify(params)) + this.log_('access:', JSON.stringify(params)) response.writeHead(200, {'Content-Type': 'text/xml'}) try { response.end(this[params.Action](params)) } catch (e) { - console.log(e) + this.log_(e) response.end('error') this.close() } From 5748ff269e4805f4cabe8bd7d9744e4ad57f5cc8 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 15 Sep 2017 12:08:02 +0900 Subject: [PATCH 2/3] Modify to server log output setting --- bin/webserver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/webserver.js b/bin/webserver.js index 6569056..398c4be 100755 --- a/bin/webserver.js +++ b/bin/webserver.js @@ -2,4 +2,4 @@ 'use strict' const server = new (require('..').Server)() -server.run() +server.run({logging: true}) From eb12c52f53cdbab05cbc60f51aacb086919b233c Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 15 Sep 2017 12:10:07 +0900 Subject: [PATCH 3/3] Add example that port and host can be set by run --- README.md | 5 ++++- examples/example_webserver.js | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0514830..65b60f0 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,10 @@ const server = new (require('@abetomo/simply-imitated-sqs').Server)() var queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test' if (process.env.LOCAL_TEST === '1') { /// !!! Server start and `queueUrl` reassignment - queueUrl = server.run() + queueUrl = server.run({ + port: '1234', + host: 'localhost' + }) } Promise.resolve().then(() => { diff --git a/examples/example_webserver.js b/examples/example_webserver.js index a9fa496..16dfe57 100644 --- a/examples/example_webserver.js +++ b/examples/example_webserver.js @@ -16,7 +16,10 @@ const server = new (require('@abetomo/simply-imitated-sqs').Server)() var queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test' if (process.env.LOCAL_TEST === '1') { /// !!! Server start and `queueUrl` reassignment - queueUrl = server.run() + queueUrl = server.run({ + port: '1234', + host: 'localhost' + }) } Promise.resolve().then(() => {