1
0
mirror of https://github.com/sasjs/server.git synced 2026-01-09 07:20:05 +00:00

feat: serve deployed streaming apps

This commit is contained in:
Saad Jutt
2022-03-15 03:54:19 +05:00
parent 940f705f5d
commit d6fa877941
7 changed files with 106 additions and 79 deletions

View File

@@ -1,7 +1,7 @@
import path from 'path'
import { MemberType, FolderMember, ServiceMember, FileTree } from '../../types'
import { getTmpFilesFolderPath } from '../../utils/file'
import { createFolder, createFile, asyncForEach } from '@sasjs/utils'
import path from 'path'
// REFACTOR: export FileTreeCpntroller
export const createFileTree = async (
@@ -27,9 +27,13 @@ export const createFileTree = async (
(err) => Promise.reject({ error: err, failedToCreate: name })
)
} else {
await createFile(path.join(destinationPath, name), member.code).catch(
(err) => Promise.reject({ error: err, failedToCreate: name })
)
const encoding = member.type === MemberType.file ? 'base64' : undefined
await createFile(
path.join(destinationPath, name),
member.code,
encoding
).catch((err) => Promise.reject({ error: err, failedToCreate: name }))
}
})

View File

@@ -1,6 +1,8 @@
import express from 'express'
import { deleteFile } from '@sasjs/utils'
import { publishAppStream } from '../appStream'
import { multerSingle } from '../../middlewares/multer'
import { DriveController } from '../../controllers/'
import { fileBodyValidation, fileParamValidation } from '../../utils'
@@ -12,6 +14,12 @@ const driveRouter = express.Router()
driveRouter.post('/deploy', async (req, res) => {
try {
const response = await controller.deploy(req.body)
const data = req.body
const appLoc = data.appLoc ? data.appLoc.replace(/^\//, '').split('/') : []
publishAppStream(appLoc)
res.send(response)
} catch (err: any) {
const statusCode = err.code

View File

@@ -0,0 +1,22 @@
import path from 'path'
import express from 'express'
import { getTmpFilesFolderPath } from '../../utils'
const router = express.Router()
export const publishAppStream = (appLoc: string[]) => {
const appLocUrl = encodeURI(appLoc.join('/'))
const appLocPath = appLoc.join(path.sep)
const pathToDeployment = path.join(
getTmpFilesFolderPath(),
appLocPath,
'services',
'webv'
)
router.use(`/${appLocUrl}`, express.static(pathToDeployment))
}
export default router

View File

@@ -2,8 +2,15 @@ import { Express } from 'express'
import webRouter from './web'
import apiRouter from './api'
import appStreamRouter from './appStream'
export const setupRoutes = (app: Express) => {
app.use('/', webRouter)
app.use('/SASjsApi', apiRouter)
app.use('/AppStream', function (req, res, next) {
// this needs to be a function to hook on
// whatever the current router is
appStreamRouter(req, res, next)
})
}

View File

@@ -4,7 +4,8 @@ export interface FileTree {
export enum MemberType {
folder = 'folder',
service = 'service'
service = 'service',
file = 'file'
}
export interface FolderMember {
@@ -15,7 +16,7 @@ export interface FolderMember {
export interface ServiceMember {
name: string
type: MemberType.service
type: MemberType.service | MemberType.file
code: string
}
@@ -36,7 +37,9 @@ const isFolderMember = (arg: any): arg is FolderMember =>
Array.isArray(arg.members) &&
arg.members.filter(
(member: FolderMember | ServiceMember) =>
!isFolderMember(member) && !isServiceMember(member)
!isFolderMember(member) &&
!isServiceMember(member) &&
!isFileMember(member)
).length === 0
const isServiceMember = (arg: any): arg is ServiceMember =>
@@ -45,3 +48,10 @@ const isServiceMember = (arg: any): arg is ServiceMember =>
arg.type === MemberType.service &&
arg.code &&
typeof arg.code === 'string'
const isFileMember = (arg: any): arg is ServiceMember =>
arg &&
typeof arg.name === 'string' &&
arg.type === MemberType.file &&
arg.code &&
typeof arg.code === 'string'