Last active
January 21, 2020 10:31
-
-
Save ivanvmoreno/bd74ad1ddf75d4a3cf3b103490b9502c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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