Code files
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
'use strict'
|
||||
|
||||
const wiring = require('./wiring')
|
||||
const service = require('./service')()
|
||||
|
||||
wiring(service)
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "auditservice",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"keywords": [],
|
||||
"author": "Peter Elger",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"concordant": "^0.2.1",
|
||||
"mongo": "^0.1.0",
|
||||
"restify": "^4.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
'use strict'
|
||||
|
||||
const { MongoClient } = require('mongodb')
|
||||
const { dns } = require('concordant')()
|
||||
|
||||
module.exports = service
|
||||
|
||||
function service () {
|
||||
|
||||
var db
|
||||
|
||||
setup()
|
||||
|
||||
function setup () {
|
||||
const mongo = '_main._tcp.mongo.micro.svc.cluster.local'
|
||||
|
||||
dns.resolve(mongo, (err, locs) => {
|
||||
if (err) {
|
||||
console.error(err)
|
||||
return
|
||||
}
|
||||
const { host, port } = locs[0]
|
||||
const url = `mongodb://${host}:${port}/audit`
|
||||
MongoClient.connect(url, (err, client) => {
|
||||
if (err) {
|
||||
console.log('failed to connect to MongoDB retrying in 100ms')
|
||||
setTimeout(setup, 100)
|
||||
return
|
||||
}
|
||||
db = client
|
||||
db.on('close', () => db = null)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function append (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
const data = {
|
||||
ts: Date.now(),
|
||||
calc: args.calc,
|
||||
result: args.calcResult
|
||||
}
|
||||
|
||||
audit.insert(data, (err, result) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {result: result.toString()})
|
||||
})
|
||||
}
|
||||
|
||||
function list (args, cb) {
|
||||
if (!db) {
|
||||
cb(Error('No database connection'))
|
||||
return
|
||||
}
|
||||
const audit = db.collection('audit')
|
||||
audit.find({}, {limit: 10}).toArray((err, docs) => {
|
||||
if (err) {
|
||||
cb(err)
|
||||
return
|
||||
}
|
||||
cb(null, {list: docs})
|
||||
})
|
||||
}
|
||||
|
||||
return { append, list }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const restify = require('restify')
|
||||
const { AUDITSERVICE_SERVICE_PORT } = process.env
|
||||
|
||||
module.exports = wiring
|
||||
|
||||
function wiring (service) {
|
||||
const server = restify.createServer()
|
||||
|
||||
server.use(restify.bodyParser())
|
||||
|
||||
server.post('/append', (req, res, next) => {
|
||||
service.append(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.get('/list', (req, res, next) => {
|
||||
service.list(req.params, (err, result) => {
|
||||
if (err) {
|
||||
res.send(err)
|
||||
return
|
||||
}
|
||||
res.send(200, result)
|
||||
next()
|
||||
})
|
||||
})
|
||||
|
||||
server.listen(AUDITSERVICE_SERVICE_PORT, '0.0.0.0', () => {
|
||||
console.log('%s listening at %s', server.name, server.url)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user