diff --git a/src/routes/index.ts b/src/routes/index.ts index bd27d0a..1fc3bc9 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -1,23 +1,14 @@ import express from 'express' -import { processSas, createFileTree, getTreeExample } from '../controllers' +import { createFileTree, getTreeExample } from '../controllers' import { ExecutionResult, isRequestQuery, isFileTree } from '../types' +import path from 'path' +import { getTmpFilesFolderPath } from '../utils' +import { ExecutionController } from '../controllers' 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(`Executed!
-

Log is located:

${result.logPath}
-

Log:

`) +router.get('/', async (_, res) => { + res.status(200).send('Welcome to @sasjs/server API') }) router.post('/deploy', async (req, res) => { @@ -54,20 +45,21 @@ router.get('/SASjsExecutor', async (req, res) => { }) router.get('/SASjsExecutor/do', async (req, res) => { - const queryEntries = Object.keys(req.query).map((entry: string) => - entry.toLowerCase() - ) - if (isRequestQuery(req.query)) { - await processSas({ ...req.query }) - .then((result) => { + const sasCodePath = path + .join(getTmpFilesFolderPath(), req.query._program) + .replace(new RegExp('/', 'g'), path.sep) + + await new ExecutionController() + .execute(sasCodePath, undefined, undefined, { ...req.query }) + .then((result: {}) => { res.status(200).send(result) }) - .catch((err) => { + .catch((err: {} | string) => { res.status(400).send({ status: 'failure', message: 'Job execution failed.', - ...err + ...(typeof err === 'object' ? err : { details: err }) }) }) } else { diff --git a/src/utils/sleep.ts b/src/utils/sleep.ts new file mode 100644 index 0000000..61d7522 --- /dev/null +++ b/src/utils/sleep.ts @@ -0,0 +1,3 @@ +export const sleep = async (delay: number) => { + await new Promise((resolve) => setTimeout(resolve, delay)) +}