tianji/src/server/router/serverStatus.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

import { Router } from 'express';
2023-11-07 08:56:47 +00:00
import { body, header, param, validate } from '../middleware/validate';
import { recordServerStatus } from '../model/serverStatus';
2023-11-07 08:56:47 +00:00
import fs from 'fs-extra';
import path from 'path';
import { env } from '../utils/env';
export const serverStatusRouter = Router();
2023-11-07 08:56:47 +00:00
const installScript = fs.readFileSync(
env.isProd
? path.resolve(process.cwd(), './scripts/install.sh')
: path.resolve(process.cwd(), '../../scripts/install.sh')
2023-11-07 08:56:47 +00:00
);
serverStatusRouter.post(
'/report',
validate(
header('x-tianji-report-version').isSemVer(),
body('workspaceId').isString(),
body('name').isString(),
body('hostname').isString(),
body('timeout').optional().isInt(),
body('payload').isObject()
),
async (req, res) => {
const body = req.body;
recordServerStatus(body);
res.send('success');
}
);
2023-11-07 08:56:47 +00:00
serverStatusRouter.get(
'/:workspaceId/install.sh',
validate(param('workspaceId').isString()),
async (req, res) => {
const { workspaceId } = req.params;
const queryUrl = req.query.url ? String(req.query.url) : undefined;
const server = queryUrl || `${req.protocol}://${req.get('Host')}`;
2023-11-07 08:56:47 +00:00
res
.setHeader('Content-Type', 'text/plain')
.send(
String(installScript)
.replace('{{DEFAULT_SERVER}}', server)
.replace('{{DEFAULT_WORKSPACE}}', workspaceId)
);
}
);