Skip to content

Commit

Permalink
fix: routes precedence #2820
Browse files Browse the repository at this point in the history
  • Loading branch information
dnmeid committed Mar 29, 2024
1 parent a0ad601 commit 52a3557
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 42 deletions.
3 changes: 1 addition & 2 deletions companion/lib/Service/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ class ServiceController {
* @param {import('../Registry.js').default} registry - the application core
*/
constructor(registry) {
this.httpApi = new ServiceHttpApi(registry, registry.ui.express.legacyApiRouter)
this.httpApi.bindToApp(registry.ui.express.app)
this.httpApi = new ServiceHttpApi(registry, registry.ui.express)
this.https = new ServiceHttps(registry, registry.ui.express)
this.oscSender = new ServiceOscSender(registry)
this.oscListener = new ServiceOscListener(registry)
Expand Down
71 changes: 34 additions & 37 deletions companion/lib/Service/HttpApi.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import CoreBase from '../Core/Base.js'
import { ParseAlignment, parseColorToNumber, rgb } from '../Resources/Util.js'
import express from 'express'
import cors from 'cors'
import { formatLocation } from '@companion-app/shared/ControlId.js'
import Express from 'express'

/**
* Class providing the HTTP API.
Expand All @@ -27,41 +27,32 @@ import { formatLocation } from '@companion-app/shared/ControlId.js'
*/
export class ServiceHttpApi extends CoreBase {
/**
* Root express router
* new Api express router
* @type {import('express').Router}
* @access private
*/
#legacyRouter
#apiRouter

/**
* Api router
* @type {import('express').Router}
* @access private
* The web application framework
* @type {import('../UI/Express.js').default}
* @access protected
* @readonly
*/
#apiRouter
#express

/**
* @param {import('../Registry.js').default} registry - the application core
* @param {import('express').Router} router - the http router
* @param {import('../UI/Express.js').default} express - express
*/
constructor(registry, router) {
constructor(registry, express) {
super(registry, 'Service/HttpApi')

this.#legacyRouter = router
this.#apiRouter = express.Router()
this.#apiRouter.use(cors())
this.#express = express
// @ts-ignore
this.#apiRouter = new Express.Router()

this.#setupLegacyHttpRoutes()
this.#setupNewHttpRoutes()
}

/**
*
* @param {import('express').Application} app
*/
bindToApp(app) {
app.use(
'/api',
this.#apiRouter.use(
(_req, res, next) => {
// Check that the API is enabled
if (this.userconfig.getKey('http_api_enabled')) {
Expand All @@ -71,17 +62,26 @@ export class ServiceHttpApi extends CoreBase {
// Disabled
res.status(403).send()
}
},
this.#apiRouter
}
)

this.#setupNewHttpRoutes()
this.#setupLegacyHttpRoutes()

// Finally, default all unhandled to 404
this.#apiRouter.use('*', (_req, res) => {
res.status(404).send('')
})

this.#express.apiRouter = this.#apiRouter
}

#isLegacyRouteAllowed() {
return !!(this.userconfig.getKey('http_api_enabled') && this.userconfig.getKey('http_legacy_api_enabled'))
}

#setupLegacyHttpRoutes() {
this.#legacyRouter.options('/press/bank/*', (_req, res, _next) => {
this.#apiRouter.options('/press/bank/*', (_req, res, _next) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand All @@ -90,7 +90,7 @@ export class ServiceHttpApi extends CoreBase {
return res.send(200)
})

this.#legacyRouter.get('^/press/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})', (req, res) => {
this.#apiRouter.get('/press/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})', (req, res) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand All @@ -116,7 +116,7 @@ export class ServiceHttpApi extends CoreBase {
return res.send('ok')
})

this.#legacyRouter.get('^/press/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})/:direction(down|up)', (req, res) => {
this.#apiRouter.get('/press/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})/:direction(down|up)', (req, res) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ServiceHttpApi extends CoreBase {
return res.send('ok')
})

this.#legacyRouter.get('^/rescan', (_req, res) => {
this.#apiRouter.get('/rescan', (_req, res) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand All @@ -174,7 +174,7 @@ export class ServiceHttpApi extends CoreBase {
)
})

this.#legacyRouter.get('^/style/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})', (req, res) => {
this.#apiRouter.get('/style/bank/:page([0-9]{1,2})/:bank([0-9]{1,2})', (req, res) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand Down Expand Up @@ -262,7 +262,7 @@ export class ServiceHttpApi extends CoreBase {
return res.send('ok')
})

this.#legacyRouter.get('^/set/custom-variable/:name', (req, res) => {
this.#apiRouter.get('/set/custom-variable/:name', (req, res) => {
if (!this.#isLegacyRouteAllowed()) return res.status(403).send()

res.header('Access-Control-Allow-Origin', '*')
Expand Down Expand Up @@ -296,16 +296,13 @@ export class ServiceHttpApi extends CoreBase {
this.#apiRouter.post('/location/:page([0-9]{1,2})/:row(-?[0-9]+)/:column(-?[0-9]+)/style', this.#locationStyle)

// custom variables
this.#apiRouter.post('/custom-variable/:name/value', this.#customVariableSetValue)
this.#apiRouter.get('/custom-variable/:name/value', this.#customVariableGetValue)
this.#apiRouter.route('/custom-variable/:name/value')
.post(this.#customVariableSetValue)
.get(this.#customVariableGetValue)

// surfaces
this.#apiRouter.post('/surfaces/rescan', this.#surfacesRescan)

// Finally, default all unhandled to 404
this.#apiRouter.use('*', (_req, res) => {
res.status(404).send('')
})
}

/**
Expand Down
14 changes: 11 additions & 3 deletions companion/lib/UI/Express.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,14 @@ class UIExpress {
logger = LogController.createLogger('UI/Express')

app = Express()
#apiRouter = Express.Router()

/**
* @param {import('../Registry.js').default} registry
*/
constructor(registry) {
this.registry = registry

this.legacyApiRouter = Express.Router()

this.app.use(cors())

this.app.use((_req, res, next) => {
Expand Down Expand Up @@ -105,7 +104,8 @@ class UIExpress {
}
})

this.app.use(this.legacyApiRouter)
// Use the router #apiRouter to add API routes dynamically, this router can be redefined at runtime with setter
this.app.use('/api', (r,s,n) => this.#apiRouter(r,s,n))

/**
* @param {string} subpath
Expand Down Expand Up @@ -141,6 +141,14 @@ class UIExpress {
return webuiServer(req, res, next)
})
}

/**
* Set a new router as the ApiRouter
* @param {*} router
*/
set apiRouter (router) {
this.#apiRouter = router
}
}

export default UIExpress

0 comments on commit 52a3557

Please sign in to comment.