wip: prepare endpoints

This commit is contained in:
Carlos Valente
2023-11-22 22:30:31 +01:00
parent f39c8b46bb
commit cdca5eaad3
8 changed files with 73 additions and 58 deletions
+39 -33
View File
@@ -1,4 +1,5 @@
import { Alias, DatabaseModel, GetInfo, LogOrigin, ProjectData } from 'ontime-types';
import { LogOrigin } from 'ontime-types';
import type { Alias, DatabaseModel, GetInfo, HTTPSettings, ProjectData } from 'ontime-types';
import { RequestHandler, Request, Response } from 'express';
import fs from 'fs';
@@ -284,11 +285,25 @@ export const getOSC = async (req, res) => {
res.status(200).send(osc);
};
// Create controller for GET request to '/ontime/http'
// Returns -
export const getHTTP = async (req, res) => {
const http = DataProvider.getHttp();
res.status(200).send(http);
// Create controller for POST request to '/ontime/osc'
// Returns ACK message
export const postOSC = async (req, res) => {
if (failEmptyObjects(req.body, res)) {
return;
}
try {
const oscSettings = req.body;
await DataProvider.setOsc(oscSettings);
// TODO: this update could be more granular, checking that relevant data was changed
const { message } = oscIntegration.init(oscSettings);
logger.info(LogOrigin.Tx, message);
res.send(oscSettings).status(200);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
};
export const postOscSubscriptions = async (req, res) => {
@@ -312,42 +327,33 @@ export const postOscSubscriptions = async (req, res) => {
}
};
export const postHttpSubscriptions = async (req, res) => {
if (failEmptyObjects(req.body, res)) {
return;
}
try {
const subscriptions = req.body;
const httpSettings = DataProvider.getHttp();
httpSettings.subscriptions = subscriptions;
await DataProvider.setHttp(httpSettings);
const { message } = httpIntegration.init(httpSettings);
logger.info(LogOrigin.Tx, message);
res.send(httpSettings).status(200);
} catch (error) {
res.status(400).send(error);
}
// Create controller for GET request to '/ontime/http'
export const getHTTP = async (req, res: Response<HTTPSettings>) => {
const http = DataProvider.getHttp();
res.status(200).send(http);
};
// Create controller for POST request to '/ontime/osc'
// Returns ACK message
export const postOSC = async (req, res) => {
// Create controller for POST request to '/ontime/http'
export const postHTTP = async (req: Request<any, any, HTTPSettings>, res: Response) => {
if (failEmptyObjects(req.body, res)) {
return;
}
try {
const oscSettings = req.body;
await DataProvider.setOsc(oscSettings);
const settings = req.body;
const httpSettings = DataProvider.getHttp();
const hasEnabledChanged = httpSettings.enabledOut !== settings.enabledOut;
// TODO: this update could be more granular, checking that relevant data was changed
const { message } = oscIntegration.init(oscSettings);
logger.info(LogOrigin.Tx, message);
httpSettings.subscriptions = settings.subscriptions;
httpSettings.enabledOut = settings.enabledOut;
await DataProvider.setHttp(httpSettings);
res.send(oscSettings).status(200);
if (hasEnabledChanged) {
const { message } = httpIntegration.init(httpSettings);
logger.info(LogOrigin.Tx, message);
}
res.send(httpSettings).status(200);
} catch (error) {
res.status(400).send({ message: error.toString() });
}
@@ -90,6 +90,21 @@ export const validateOSC = [
},
];
/**
* @description Validates object for POST /ontime/http
*/
export const validateHTTP = [
body('enabledOut').exists().isBoolean(),
body('subscriptions')
.isObject()
.custom((value) => validateOscObject(value)),
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return res.status(422).json({ errors: errors.array() });
next();
},
];
/**
* @description Validates object for POST /ontime/osc-subscriptions
*/
+4 -3
View File
@@ -16,11 +16,11 @@ import {
postNew,
postOSC,
postOscSubscriptions,
postHttpSubscriptions,
postSettings,
postUserFields,
postViewSettings,
previewExcel,
postHTTP,
} from '../controllers/ontimeController.js';
import {
@@ -31,6 +31,7 @@ import {
validateSettings,
validateUserFields,
viewValidator,
validateHTTP,
} from '../controllers/ontimeController.validate.js';
import { projectSanitiser } from '../controllers/projectController.validate.js';
@@ -90,8 +91,8 @@ router.post('/osc-subscriptions', validateSubscription, postOscSubscriptions);
// create route between controller and '/ontime/http' endpoint
router.get('/http', getHTTP);
// create route between controller and '/ontime/osc-subscriptions' endpoint
router.post('/http-subscriptions', validateSubscription, postHttpSubscriptions);
// create route between controller and '/ontime/http' endpoint
router.post('/http', validateHTTP, postHTTP);
// create route between controller and '/ontime/new' endpoint
router.post('/new', projectSanitiser, postNew);
@@ -115,7 +115,6 @@ export class HttpIntegration implements IIntegration {
}
shutdown() {
console.log('Shutting down HTTP integration');
if (this.httpAgent) {
this.httpAgent?.destroy();
this.httpAgent = null;