2023-10-31 16:02:52 +00:00
|
|
|
import { Router } from 'express';
|
2023-11-07 08:56:47 +00:00
|
|
|
import { body, header, param, validate } from '../middleware/validate';
|
2023-10-31 16:02:52 +00:00
|
|
|
import { recordServerStatus } from '../model/serverStatus';
|
2023-11-07 08:56:47 +00:00
|
|
|
import fs from 'fs-extra';
|
2024-01-28 18:17:10 +00:00
|
|
|
import { libraryPath } from '../utils/lib';
|
2023-10-31 16:02:52 +00:00
|
|
|
|
|
|
|
export const serverStatusRouter = Router();
|
|
|
|
|
2024-01-28 18:17:10 +00:00
|
|
|
const installScript = fs.readFileSync(libraryPath.installScript);
|
2023-11-07 08:56:47 +00:00
|
|
|
|
2023-10-31 16:02:52 +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;
|
2024-01-03 16:19:23 +00:00
|
|
|
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)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|