You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
1.8 KiB
77 lines
1.8 KiB
// logan.js
|
|
// Copyright (C) 2023 DTP Technologies, LLC
|
|
// License: Apache-2.0
|
|
|
|
'use strict';
|
|
|
|
const os = require('os');
|
|
|
|
const { SiteService, SiteError } = require('../../lib/site-lib');
|
|
|
|
class LoganService extends SiteService {
|
|
|
|
constructor (dtp) {
|
|
super(dtp, module.exports);
|
|
}
|
|
|
|
async start ( ) {
|
|
await super.start();
|
|
}
|
|
|
|
async sendRequestEvent (component, req, event) {
|
|
if (req.user) {
|
|
event.data = event.data || { };
|
|
event.data.user = {
|
|
_id: req.user._id,
|
|
username: req.user.username,
|
|
};
|
|
}
|
|
event.ip = req.ip;
|
|
return this.sendEvent(component, event);
|
|
}
|
|
|
|
async sendEvent (component, event) {
|
|
try {
|
|
event.host = os.hostname();
|
|
event['component.slug'] = component.slug;
|
|
event['component.name'] = component.className || component.name;
|
|
|
|
this.log[event.level]('application event', { event });
|
|
|
|
if (process.env.DTP_LOGAN !== 'enabled') {
|
|
return;
|
|
}
|
|
|
|
const loganScheme = process.env.DTP_LOGAN_SCHEME || 'http';
|
|
const loganUrl = `${loganScheme}://${process.env.DTP_LOGAN_HOST}/api/event`;
|
|
const payload = JSON.stringify(event);
|
|
|
|
const response = await fetch(loganUrl, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Content-Length': payload.length,
|
|
'X-LogAn-Auth': process.env.DTP_LOGAN_API_KEY,
|
|
},
|
|
body: payload,
|
|
});
|
|
|
|
const json = await response.json();
|
|
if (!json.success) {
|
|
throw new SiteError(500, json.message);
|
|
}
|
|
|
|
return json;
|
|
} catch (error) {
|
|
this.log.error('failed to send LOGAN event', { event, error });
|
|
// fall through
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
slug: 'logan',
|
|
name: 'logan',
|
|
className: 'LoganService',
|
|
create: (dtp) => { return new LoganService(dtp); },
|
|
};
|