-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
60 lines (52 loc) · 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
const express = require('express')
const app = express()
const { startWorkflow, initTemporal } = require('./temporal.service')
const bodyParser = require('body-parser')
const crypto = require('crypto');
require('dotenv').config()
initTemporal()
const port = process.env.PORT
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.set('view engine', 'ejs');
app.set('views', __dirname + '/views');
app.get('/', (_, res) => res.redirect('/temporal'))
app.get('/temporal', (_, res) => res.render('index', { environments: global.environments}))
app.post('/temporal', async (req, res) => {
const { environment, workflowId, workflowType, taskqueue, input, startNew, timeout } = req.body;
const _id = crypto.randomBytes(20).toString('hex');
const inputs = []
const options = {
environment,
taskQueue: taskqueue,
workflowType,
timeout,
workflowId: workflowId + `${!!+startNew ? "-" + _id : ''}`
}
if(!Array.isArray(input)) {
inputs.push(normalizeObjectId(input))
} else {
inputs.push(...input.map( i => normalizeObjectId(i)))
}
const {cli, stderr, stdout} = await startWorkflow(options, ...inputs)
if(!stderr) {
const settedEnvironment = global.environments.find((env) => env.environment == options.environment)
return res.render("successful", {
data: stdout.trim(),
cli: cli,
temporal_client: settedEnvironment.ui,
namespace: settedEnvironment.namespace,
workflow_id: workflowId + `${!!+startNew ? "-" + _id : ''}`,
run_id: stdout.split('\n')?.[2]?.split("RunId")[1]?.trim() ?? ""
});
}
return res.render('error', {
data: stderr.trim(),
cli: cli,
})
})
app.listen(port, () => console.log("Server is running at port " + port))
function normalizeObjectId(jsonString) {
if(!jsonString) return
return jsonString.replace(/ObjectId\("([^"]+)"\)/g, '"$1"');
}