Normalize / and /{id} responses.

This commit is contained in:
Thomas E Lackey 2024-02-19 16:40:41 -06:00
parent 4f0fb236bd
commit ecc21cb935
2 changed files with 36 additions and 15 deletions

View File

@ -104,17 +104,30 @@ export class RegHelper {
return [...records];
}
async deploymentRequestStatus() {
const requests = await this.queryRecords({
type: 'ApplicationDeploymentRequest',
});
const deployments = await this.queryRecords({
type: 'ApplicationDeploymentRecord',
});
async deploymentRequestStatus(requestId?: string) {
const requests: any[] = [];
const deployments: any[] = [];
const removalRequests = await this.queryRecords({
type: 'ApplicationDeploymentRemovalRequest',
});
if (requestId) {
const request = await this.getRecordById(requestId);
if (request) {
requests.push(request);
}
deployments.push(...await this.queryRecords({
type: 'ApplicationDeploymentRecord', request: requestId
}));
} else {
requests.push(...await this.queryRecords({
type: 'ApplicationDeploymentRequest',
}));
deployments.push(...await this.queryRecords({
type: 'ApplicationDeploymentRecord',
}));
}
requests.sort((a, b) => a.createTime === b.createTime ? 0 : a.createTime > b.createTime ? 1 : -1,);
requests.reverse();

View File

@ -1,7 +1,7 @@
import express from 'express';
import {existsSync, readdirSync, readFileSync} from 'fs';
import {Config, getRegistry} from './config.js';
import {Config} from './config.js';
import {RegHelper} from './deployments.js';
@ -26,9 +26,7 @@ const logForRequest = (req_id) => {
return ret;
};
app.get('/', async (_req, res) => {
const reg = new RegHelper();
const regStatus = await reg.deploymentRequestStatus();
const adjustFromState = (regStatus: any[]) => {
let deployerState = {};
if (Config.DEPLOYER_STATE && existsSync(Config.DEPLOYER_STATE)) {
deployerState = JSON.parse(readFileSync(Config.DEPLOYER_STATE).toString());
@ -53,19 +51,29 @@ app.get('/', async (_req, res) => {
}
}
}
return regStatus;
}
app.get('/', async (_req, res) => {
const reg = new RegHelper();
const regStatus = adjustFromState(await reg.deploymentRequestStatus());
res.send(regStatus);
});
app.get('/:id', async (req, res) => {
const registry = getRegistry();
const records = await registry.getRecordsByIds([req.params.id]);
res.send(records);
const reg = new RegHelper();
const records = adjustFromState(await reg.deploymentRequestStatus(req.params.id));
if (records.length) {
res.send(records[0]);
} else {
res.sendStatus(410);
}
});
app.get('/log/:id', async (req, res) => {
const logFile = logForRequest(req.params.id);
if (!logFile) {
res.sendStatus(404);
res.sendStatus(410);
return;
}