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

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