Service implement api or sdk of 3rd party.
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.
 

56 lines
1.4 KiB

'use strict'
/**
* Save log to file for Loki
**/
const express = require('express');
const { resolve } = require('node:path');
const {default: common} = require('./common.js');
const {default: defaultConfig, getCustomConfigs: getCustomConfigs} = require('./conf/config');
const router = express.Router();
let customConfig = null;
//写入文件接口
//@group - 分组,log文件名
//其它所有参数均会写入log
router.post('/save', async (req, res) => {
let out = {code: 1};
//使用默认配置
let myConfig = defaultConfig;
//加载自定义配置
if (!customConfig) {
customConfig = await getCustomConfigs('./conf/custom_config.json');
}
//使用自定义配置
myConfig = customConfig;
const group = req.body.group ? req.body.group : 'test';
const token = req.body.token ? req.body.token : '';
//参数检查
if (!token || token != myConfig.secret) {
return res.status(403).json({code:0, error: '403 Forbidden'});
}
const logDir = '/var/log/loki/'; //日志保存目录
const logFile = resolve(logDir, `${group}.log`);
const ignoreKeys = ['group', 'timestamp', 'token'];
let params = common.copyBodyParams(req.body, ignoreKeys);
params.timestamp = common.getTimestampInSeconds();
const json = JSON.stringify(params);
common.saveLog(logFile, `${json}\n`);
return res.status(200).json(out);
});
exports.default = router;