Skip to content

Instantly share code, notes, and snippets.

@ivanvmoreno
Last active January 21, 2020 10:31
Show Gist options
  • Save ivanvmoreno/bd74ad1ddf75d4a3cf3b103490b9502c to your computer and use it in GitHub Desktop.
Save ivanvmoreno/bd74ad1ddf75d4a3cf3b103490b9502c to your computer and use it in GitHub Desktop.
'use strict'
const https = require('https')
const { Client } = require('@elastic/elasticsearch')
const { ES_ENDPOINT, PATTERN = /^es_log/i, PORT = 8080 } = process.env
const client = new Client({ node: ES_ENDPOINT })
const processLog = input => {
const matchIndex = input.match(/index=/i)
if (Array.isArray(matchIndex)) {
const indexSlicePos = input.indexOf(matchIndex[0])
const index = input
.slice(indexSlicePos, input.indexOf(' ', indexSlicePos))
.slice(matchIndex[0].length - 1)
const body = input.slice(input.indexOf(index) + index.length)
return { index, body }
}
return
}
const handleLog = async log => {
if (log.test(PATTERN)) {
const processed = processLog(log)
if (processed) await client.index(processed)
}
}
const server = https.createServer(({ method }, res) => {
if (method === 'POST') {
let body = '';
req.on('data', chunk => body += chunk.toString())
req.on('end', () => {
handleLog(body)
res.writeHead(200)
res.end()
})
}
})
server.listen(PORT)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment