mirror of
https://github.com/sasjs/server.git
synced 2025-12-11 03:34:35 +00:00
feat(api): set up endpoint for sas code execution
This commit is contained in:
46
src/controllers/index.ts
Normal file
46
src/controllers/index.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { execFile } from 'child_process'
|
||||
import { readFile, generateTimestamp, deleteFile } from '@sasjs/utils'
|
||||
import path from 'path'
|
||||
import { ExecutionResult, RequestQuery } from '../types'
|
||||
|
||||
// FIXME
|
||||
const sasExePath = `C:\\Program Files\\SASHome\\SASFoundation\\9.4\\sas.exe`
|
||||
const baseSasLogPath = 'C:\\Users\\YuryShkoda\\projects\\server\\sas\\logs'
|
||||
const baseSasCodePath = `sas`
|
||||
|
||||
// TODO: create utils isSasFile
|
||||
|
||||
export const processSas = async (
|
||||
query: RequestQuery
|
||||
): Promise<ExecutionResult> =>
|
||||
new Promise((resolve, reject) => {
|
||||
let sasCodePath = query._program
|
||||
sasCodePath = path.join(baseSasCodePath, `${sasCodePath}.sas`)
|
||||
sasCodePath = sasCodePath.replace(new RegExp('/', 'g'), path.sep)
|
||||
|
||||
const sasFile: string = sasCodePath.split(path.sep).pop() || 'default'
|
||||
|
||||
const sasLogPath = [
|
||||
baseSasLogPath,
|
||||
path.sep,
|
||||
sasFile.replace(/\.sas/g, ''),
|
||||
'-',
|
||||
generateTimestamp(),
|
||||
'.log'
|
||||
].join('')
|
||||
|
||||
execFile(
|
||||
sasExePath,
|
||||
['-SYSIN', sasCodePath, '-log', sasLogPath, '-nosplash'],
|
||||
async (err, _, stderr) => {
|
||||
if (err) reject(err)
|
||||
if (stderr) reject(stderr)
|
||||
|
||||
const log = await readFile(sasLogPath)
|
||||
|
||||
deleteFile(sasLogPath)
|
||||
|
||||
resolve({ log: log, logPath: sasLogPath })
|
||||
}
|
||||
)
|
||||
})
|
||||
12
src/index.ts
12
src/index.ts
@@ -1,17 +1,11 @@
|
||||
// Express App Setup
|
||||
import express from 'express'
|
||||
import bodyParser from 'body-parser'
|
||||
import indexRouter from './routes'
|
||||
|
||||
const app = express()
|
||||
|
||||
app.use(bodyParser.json())
|
||||
|
||||
// Express route handlers
|
||||
app.get('/', (req, res) => {
|
||||
res.send('Hey from @sasjs/server API!')
|
||||
})
|
||||
app.use('/', indexRouter)
|
||||
|
||||
const port = 5000
|
||||
app.listen(port, () => {
|
||||
console.log(`⚡️[server]: Server is running at https://localhost:${port}`)
|
||||
console.log(`⚡️[server]: Server is running at http://localhost:${port}`)
|
||||
})
|
||||
|
||||
23
src/routes/index.ts
Normal file
23
src/routes/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import express from 'express'
|
||||
import { processSas } from '../controllers'
|
||||
import { ExecutionResult, RequestQuery, isRequestQuery } from '../types'
|
||||
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const query = req.query
|
||||
|
||||
if (!isRequestQuery(query)) {
|
||||
res.send('Welcome to @sasjs/server API')
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
const result: ExecutionResult = await processSas(query)
|
||||
|
||||
res.send(`<b>Executed!</b><br>
|
||||
<p>Log is located:</p> ${result.logPath}<br>
|
||||
<p>Log:</p> <textarea style="width: 100%; height: 100%">${result.log}</textarea>`)
|
||||
})
|
||||
|
||||
export default router
|
||||
2
src/types/index.ts
Normal file
2
src/types/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './sas'
|
||||
export * from './request'
|
||||
6
src/types/request.ts
Normal file
6
src/types/request.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
export interface RequestQuery {
|
||||
_program: string
|
||||
}
|
||||
|
||||
export const isRequestQuery = (arg: any): arg is RequestQuery =>
|
||||
arg && !Array.isArray(arg) && typeof arg._program === 'string'
|
||||
4
src/types/sas.ts
Normal file
4
src/types/sas.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface ExecutionResult {
|
||||
log: string
|
||||
logPath: string
|
||||
}
|
||||
Reference in New Issue
Block a user