Skip to content

Commit

Permalink
Merge pull request #15 from abetomo/modify_log_of_server
Browse files Browse the repository at this point in the history
Modify log of server
  • Loading branch information
abetomo authored Sep 15, 2017
2 parents ae80557 + eb12c52 commit 2bb25c0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 6 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
2 changes: 1 addition & 1 deletion bin/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

'use strict'
const server = new (require('..').Server)()
server.run()
server.run({logging: true})
5 changes: 4 additions & 1 deletion examples/example_webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down
14 changes: 11 additions & 3 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down Expand Up @@ -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 = ''
Expand All @@ -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()
}
Expand Down

0 comments on commit 2bb25c0

Please sign in to comment.