| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- import express from "express";
- import internalStream from "../../internal/stream.js";
- import jwtdecode from "../../lib/express/jwt-decode.js";
- import apiValidator from "../../lib/validator/api.js";
- import validator from "../../lib/validator/index.js";
- import { express as logger } from "../../logger.js";
- import { getValidationSchema } from "../../schema/index.js";
- const router = express.Router({
- caseSensitive: true,
- strict: true,
- mergeParams: true,
- });
- /**
- * /api/nginx/streams
- */
- router
- .route("/")
- .options((_, res) => {
- res.sendStatus(204);
- })
- .all(jwtdecode()) // preferred so it doesn't apply to nonexistent routes
- /**
- * GET /api/nginx/streams
- *
- * Retrieve all streams
- */
- .get(async (req, res, next) => {
- try {
- const data = await validator(
- {
- additionalProperties: false,
- properties: {
- expand: {
- $ref: "common#/properties/expand",
- },
- query: {
- $ref: "common#/properties/query",
- },
- },
- },
- {
- expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
- query: typeof req.query.query === "string" ? req.query.query : null,
- },
- );
- const rows = await internalStream.getAll(res.locals.access, data.expand, data.query);
- res.status(200).send(rows);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- })
- /**
- * POST /api/nginx/streams
- *
- * Create a new stream
- */
- .post(async (req, res, next) => {
- try {
- const payload = await apiValidator(getValidationSchema("/nginx/streams", "post"), req.body);
- const result = await internalStream.create(res.locals.access, payload);
- res.status(201).send(result);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- });
- /**
- * Specific stream
- *
- * /api/nginx/streams/123
- */
- router
- .route("/:stream_id")
- .options((_, res) => {
- res.sendStatus(204);
- })
- .all(jwtdecode()) // preferred so it doesn't apply to nonexistent routes
- /**
- * GET /api/nginx/streams/123
- *
- * Retrieve a specific stream
- */
- .get(async (req, res, next) => {
- try {
- const data = await validator(
- {
- required: ["stream_id"],
- additionalProperties: false,
- properties: {
- stream_id: {
- $ref: "common#/properties/id",
- },
- expand: {
- $ref: "common#/properties/expand",
- },
- },
- },
- {
- stream_id: req.params.stream_id,
- expand: typeof req.query.expand === "string" ? req.query.expand.split(",") : null,
- },
- );
- const row = await internalStream.get(res.locals.access, {
- id: Number.parseInt(data.stream_id, 10),
- expand: data.expand,
- });
- res.status(200).send(row);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- })
- /**
- * PUT /api/nginx/streams/123
- *
- * Update and existing stream
- */
- .put(async (req, res, next) => {
- try {
- const payload = await apiValidator(getValidationSchema("/nginx/streams/{streamID}", "put"), req.body);
- payload.id = Number.parseInt(req.params.stream_id, 10);
- const result = await internalStream.update(res.locals.access, payload);
- res.status(200).send(result);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- })
- /**
- * DELETE /api/nginx/streams/123
- *
- * Update and existing stream
- */
- .delete(async (req, res, next) => {
- try {
- const result = await internalStream.delete(res.locals.access, {
- id: Number.parseInt(req.params.stream_id, 10),
- });
- res.status(200).send(result);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- });
- /**
- * Enable stream
- *
- * /api/nginx/streams/123/enable
- */
- router
- .route("/:host_id/enable")
- .options((_, res) => {
- res.sendStatus(204);
- })
- .all(jwtdecode())
- /**
- * POST /api/nginx/streams/123/enable
- */
- .post(async (req, res, next) => {
- try {
- const result = await internalStream.enable(res.locals.access, {
- id: Number.parseInt(req.params.host_id, 10),
- });
- res.status(200).send(result);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- });
- /**
- * Disable stream
- *
- * /api/nginx/streams/123/disable
- */
- router
- .route("/:host_id/disable")
- .options((_, res) => {
- res.sendStatus(204);
- })
- .all(jwtdecode())
- /**
- * POST /api/nginx/streams/123/disable
- */
- .post(async (req, res, next) => {
- try {
- const result = await internalStream.disable(res.locals.access, {
- id: Number.parseInt(req.params.host_id, 10),
- });
- res.status(200).send(result);
- } catch (err) {
- logger.debug(`${req.method.toUpperCase()} ${req.path}: ${err}`);
- next(err);
- }
- });
- export default router;
|