Skip to content

Commit

Permalink
Merge pull request #185 from sasjs/issue183
Browse files Browse the repository at this point in the history
fix(appstream): should serve only new files for same app stream name …
  • Loading branch information
allanbowe authored Jun 2, 2022
2 parents 7a93238 + e6d1989 commit d2e9456
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 15 deletions.
68 changes: 57 additions & 11 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@
"mongoose-sequence": "^5.3.1",
"morgan": "^1.10.0",
"multer": "^1.4.3",
"swagger-ui-express": "4.3.0"
"swagger-ui-express": "4.3.0",
"url": "^0.10.3"
},
"devDependencies": {
"@types/bcryptjs": "^2.4.2",
Expand Down
32 changes: 29 additions & 3 deletions api/src/routes/appStream/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import path from 'path'
import express from 'express'
import express, { Request } from 'express'
import { folderExists } from '@sasjs/utils'

import { addEntryToAppStreamConfig, getFilesFolder } from '../../utils'
import {
addEntryToAppStreamConfig,
getFilesFolder,
getFullUrl
} from '../../utils'
import { appStreamHtml } from './appStreamHtml'

const appStreams: { [key: string]: string } = {}

const router = express.Router()

router.get('/', async (req, res) => {
Expand Down Expand Up @@ -44,7 +50,7 @@ export const publishAppStream = async (
streamServiceName = `AppStreamName${appCount + 1}`
}

router.use(`/${streamServiceName}`, express.static(pathToDeployment))
appStreams[streamServiceName] = pathToDeployment

addEntryToAppStreamConfig(
streamServiceName,
Expand All @@ -64,4 +70,24 @@ export const publishAppStream = async (
return {}
}

router.get(`/*`, function (req: Request, res, next) {
const reqPath = req.path.replace(/^\//, '')

// Redirecting to url with trailing slash for base appStream URL only
if (reqPath.split('/').length === 1 && !reqPath.endsWith('/'))
return res.redirect(301, `${getFullUrl(req)}/`)

const appStream = reqPath.split('/')[0]
const appStreamFilesPath = appStreams[appStream]
if (appStreamFilesPath) {
const resourcePath = reqPath.split('/')[1] || 'index.html'

req.url = resourcePath

return express.static(appStreamFilesPath)(req, res, next)
}

return res.send("There's no App Stream available here.")
})

export default router
15 changes: 15 additions & 0 deletions api/src/utils/getServerUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import express from 'express'
import url from 'url'

export const getFullUrl = (req: express.Request) =>
url.format({
protocol: req.protocol,
host: req.get('host'),
pathname: req.originalUrl
})

export const getServerUrl = (req: express.Request) =>
url.format({
protocol: req.protocol,
host: req.get('x-forwarded-host') || req.get('host')
})
1 change: 1 addition & 0 deletions api/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from './generateRefreshToken'
export * from './getCertificates'
export * from './getDesktopFields'
export * from './getPreProgramVariables'
export * from './getServerUrl'
export * from './instantiateLogger'
export * from './isDebugOn'
export * from './parseLogToArray'
Expand Down

0 comments on commit d2e9456

Please sign in to comment.