1
0
mirror of https://github.com/sasjs/server.git synced 2026-04-09 15:13:13 +00:00

chore: default is desktop mode with prompts

This commit is contained in:
Saad Jutt
2021-11-12 23:59:55 +05:00
parent 46f2648a95
commit cd738aa4b8
17 changed files with 368 additions and 40 deletions

View File

@@ -1,10 +1,12 @@
import path from 'path'
import express from 'express'
import morgan from 'morgan'
import dotenv from 'dotenv'
import webRouter from './routes/web'
import apiRouter from './routes/api'
import { getWebBuildFolderPath } from './utils'
import { connectDB } from './routes/api/auth'
const app = express()
@@ -18,4 +20,6 @@ app.use(express.json({ limit: '50mb' }))
app.use(express.static(getWebBuildFolderPath()))
export default app
dotenv.config()
export default connectDB().then(() => app)

View File

@@ -84,7 +84,8 @@ ${program}`
let additionalArgs: string[] = []
if (autoExec) additionalArgs = ['-AUTOEXEC', autoExec]
const { stdout, stderr } = await execFilePromise(configuration.sasPath, [
const sasLoc = process.sasLoc ?? configuration.sasPath
const { stdout, stderr } = await execFilePromise(sasLoc, [
'-SYSIN',
code,
'-LOG',

View File

@@ -29,7 +29,7 @@ const authenticateToken = (
tokenType: 'accessToken' | 'refreshToken' = 'accessToken'
) => {
const { MODE } = process.env
if (MODE?.trim() === 'desktop') return next()
if (MODE?.trim() !== 'server') return next()
const authHeader = req.headers['authorization']
const token = authHeader?.split(' ')[1]

View File

@@ -1,6 +1,6 @@
export const desktopRestrict = (req: any, res: any, next: any) => {
const { MODE } = process.env
if (MODE?.trim() === 'desktop')
if (MODE?.trim() !== 'server')
return res.status(403).send('Not Allowed while in Desktop Mode.')
next()

View File

@@ -1,6 +1,6 @@
export const verifyAdmin = (req: any, res: any, next: any) => {
const { MODE } = process.env
if (MODE?.trim() === 'desktop') return next()
if (MODE?.trim() !== 'server') return next()
const { user } = req
if (!user?.isAdmin) return res.status(401).send('Admin account required')

View File

@@ -2,7 +2,7 @@ import path from 'path'
import { readFileSync } from 'fs'
import * as https from 'https'
import { configuration } from '../package.json'
import app from './app'
import appPromise from './app'
const keyPath = path.join('..', 'certificates', 'privkey.pem')
const certPath = path.join('..', 'certificates', 'fullchain.pem')
@@ -10,10 +10,12 @@ const certPath = path.join('..', 'certificates', 'fullchain.pem')
const key = readFileSync(keyPath)
const cert = readFileSync(certPath)
const httpsServer = https.createServer({ key, cert }, app)
appPromise.then((app) => {
const httpsServer = https.createServer({ key, cert }, app)
httpsServer.listen(configuration.sasJsPort, () => {
console.log(
`⚡️[server]: Server is running at https://localhost:${configuration.sasJsPort}`
)
httpsServer.listen(configuration.sasJsPort, () => {
console.log(
`⚡️[server]: Server is running at https://localhost:${configuration.sasJsPort}`
)
})
})

View File

@@ -9,7 +9,11 @@ import {
authenticateRefreshToken
} from '../../middlewares'
import { authorizeValidation, tokenValidation } from '../../utils'
import {
authorizeValidation,
getDesktopFields,
tokenValidation
} from '../../utils'
import { InfoJWT } from '../../types'
const authRouter = express.Router()
@@ -24,10 +28,16 @@ export const populateClients = async () => {
})
}
export const connectDB = () => {
export const connectDB = async () => {
const { MODE } = process.env
if (MODE?.trim() === 'desktop') {
if (MODE?.trim() !== 'server') {
console.log('Running in Destop Mode, no DB to connect.')
const { sasLoc, driveLoc } = await getDesktopFields()
process.sasLoc = sasLoc
process.driveLoc = driveLoc
return
}

View File

@@ -1,5 +1,5 @@
import express from 'express'
import dotenv from 'dotenv'
import swaggerUi from 'swagger-ui-express'
import {
@@ -13,10 +13,7 @@ import stpRouter from './stp'
import userRouter from './user'
import groupRouter from './group'
import clientRouter from './client'
import authRouter, { connectDB } from './auth'
dotenv.config()
connectDB()
import authRouter from './auth'
const router = express.Router()

View File

@@ -9,7 +9,7 @@ webRouter.get('/', async (_, res) => {
const indexHtmlPath = path.join(getWebBuildFolderPath(), 'index.html')
const { MODE } = process.env
if (MODE?.trim() === 'desktop') {
if (MODE?.trim() !== 'server') {
const content = await readFile(indexHtmlPath)
const codeToInject = `

View File

@@ -1,8 +1,10 @@
import app from './app'
import appPromise from './app'
import { configuration } from '../package.json'
app.listen(configuration.sasJsPort, () => {
console.log(
`⚡️[server]: Server is running at http://localhost:${configuration.sasJsPort}`
)
appPromise.then((app) => {
app.listen(configuration.sasJsPort, () => {
console.log(
`⚡️[server]: Server is running at http://localhost:${configuration.sasJsPort}`
)
})
})

View File

@@ -1,5 +1,7 @@
declare namespace NodeJS {
export interface Process {
sasLoc?: string
driveLoc?: string
sessionController?: import('../controllers/internal').SessionController
}
}

View File

@@ -5,7 +5,7 @@ export const getWebBuildFolderPath = () =>
getRealPath(path.join(__dirname, '..', '..', '..', 'web', 'build'))
export const getTmpFolderPath = () =>
getRealPath(path.join(process.cwd(), 'tmp'))
process.driveLoc ?? getRealPath(path.join(process.cwd(), 'tmp'))
export const getTmpFilesFolderPath = () =>
path.join(getTmpFolderPath(), 'files')

View File

@@ -0,0 +1,61 @@
import path from 'path'
import { getString } from '@sasjs/utils/input'
import { createFolder, fileExists, folderExists } from '@sasjs/utils'
const isWindows = () => process.platform === 'win32'
export const getDesktopFields = async () => {
const sasLoc = await getSASLocation()
const driveLoc = await getDriveLocation()
return { sasLoc, driveLoc }
}
const getDriveLocation = async (): Promise<string> => {
const validator = async (filePath: string) => {
if (!filePath) return 'Path to files/drive is required.'
const drivePath = path.join(process.cwd(), filePath)
if (!(await folderExists(drivePath))) {
await createFolder(drivePath)
await createFolder(path.join(drivePath, 'files'))
}
return true
}
const defaultLocation = isWindows() ? '.\\tmp\\' : './tmp/'
const targetName = await getString(
'Please enter path to file/drive (relative to executable): ',
validator,
defaultLocation
)
return targetName
}
const getSASLocation = async (): Promise<string> => {
const validator = async (filePath: string) => {
if (!filePath) return 'Path to SAS executable is required.'
if (!(await fileExists(filePath))) {
return 'No file found at provided path.'
}
return true
}
const defaultLocation = isWindows()
? 'C:\\Program Files\\SASHome\\SASFoundation\\9.4\\sas.exe'
: '/opt/sas/sas9/SASHome/SASFoundation/9.4/sasexe/sas'
const targetName = await getString(
'Please enter path to SAS executable (absolute path): ',
validator,
defaultLocation
)
return targetName
}

View File

@@ -2,6 +2,7 @@ export * from './file'
export * from './generateAccessToken'
export * from './generateAuthCode'
export * from './generateRefreshToken'
export * from './getDesktopFields'
export * from './removeTokensInDB'
export * from './saveTokensInDB'
export * from './sleep'