Skip to content

Commit

Permalink
add api endpoint for logs
Browse files Browse the repository at this point in the history
  • Loading branch information
ragibkl committed Aug 12, 2023
1 parent bcb7030 commit 1eccc79
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
18 changes: 14 additions & 4 deletions adblock_dnsdist_logs/src/getLogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ function extract (message) {
return [question, answers]
}

async function getLogs (ctx) {
const ip = getIp(ctx)
async function getQueries (ip) {
const text = await readFileAsync(logFile, 'utf8')
const data = yaml.loadAll(text)

const queries = data
return data
.filter(Boolean)
.filter(d => d.message.query_address === ip)
.map(d => {
Expand All @@ -52,10 +51,21 @@ async function getLogs (ctx) {
answers
}
})
}

async function getLogs (ctx) {
const ip = getIp(ctx)
const queries = await getQueries(ip)
await ctx.render('views/getLogs', { ip, queries })
}

async function getLogsApi (ctx) {
const ip = getIp(ctx)
const queries = await getQueries(ip)
ctx.body = { queries }
}

module.exports = {
getLogs
getLogs,
getLogsApi
}
3 changes: 2 additions & 1 deletion adblock_dnsdist_logs/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const Router = require('@koa/router')
const hbs = require('koa-views-handlebars')

const { port } = require('./config')
const { getLogs } = require('./getLogs')
const { getLogs, getLogsApi } = require('./getLogs')

const router = new Router()
router.get('/', getLogs)
router.get('/api/getLogs', getLogsApi)

const app = new Koa()

Expand Down

0 comments on commit 1eccc79

Please sign in to comment.