Skip to content
Snippets Groups Projects
Commit d647697c authored by david.bauer's avatar david.bauer :fire:
Browse files

Fix: Faulty routing when running on different app root

parent 322a42c2
No related branches found
No related tags found
No related merge requests found
......@@ -39,8 +39,7 @@ describe('With different app root', function () {
}
}
})
process.env.APP_ROOT = '/appsuite/'
app = await injectApp()
app = await injectApp('/appsuite/')
})
afterEach(async function () {
......
......@@ -65,8 +65,9 @@ export function mockRedis (data = {}, isEnabled = true) {
return mock
}
export async function injectApp () {
export async function injectApp (appRoot = '/') {
register.clear()
process.env.APP_ROOT = appRoot
const { configMap } = await import('../src/config_map.js')
const { getLatestVersion } = await import('../src/version.js')
......@@ -76,7 +77,9 @@ export async function injectApp () {
app.register(sensible)
app.register(urlData)
app.register(formbody)
app.register(autoLoad, { dir: join(__dirname, '../src/routes'), prefix: process.env.APP_ROOT, autoHooks: true })
const autoLoadOptions = { dir: join(__dirname, '../src/routes'), autoHooks: true }
if (appRoot) autoLoadOptions.options = { prefix: String(appRoot).replace(/\/$/, '') }
app.register(autoLoad, autoLoadOptions)
return app
}
......
......@@ -45,7 +45,9 @@ app.register(autoLoad, { dir: join(__dirname, 'plugins') })
// Register routes
// Note: routes are loaded in alphabetical order
app.register(autoLoad, { dir: join(__dirname, 'routes'), prefix: process.env.APP_ROOT, autoHooks: true })
const autoLoadOptions = { dir: join(__dirname, 'routes'), autoHooks: true }
if (process.env.APP_ROOT !== '/') autoLoadOptions.options = { prefix: String(process.env.APP_ROOT).replace(/\/$/, '') }
app.register(autoLoad, autoLoadOptions)
app.addHook('onReady', () => {
// don't block the onReady hook
......
......@@ -18,11 +18,12 @@ export default async function (app, opts) {
done()
})
const slowRequestThreshold = parseInt(process.env.SLOW_REQUEST_THRESHOLD) || 4000
// Logs the request with the 'debug' level and also logs headers with the 'trace' level
app.addHook('onResponse', (req, reply, done) => {
const responseTime = reply.getResponseTime()
const loggingOptions = { url: req.raw.url, res: reply, method: req.method, responseTime }
const slowRequestThreshold = parseInt(process.env.SLOW_REQUEST_THRESHOLD) || 4000
/* c8 ignore next */
if (process.env.LOG_LEVEL === 'trace') loggingOptions.headers = req.headers
reply.log.debug(loggingOptions, 'request completed')
......
......@@ -9,7 +9,7 @@ export default async function redirectsPlugin (fastify) {
})
if (process.env.APP_ROOT.length > 1) {
fastify.get(process.env.APP_ROOT.slice(0, -1), async (req, res) => {
fastify.get('', async (req, res) => {
res.redirect(process.env.APP_ROOT)
})
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment