-
Notifications
You must be signed in to change notification settings - Fork 0
/
stated-workflow-api.js
executable file
·155 lines (132 loc) · 5.1 KB
/
stated-workflow-api.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/env node --experimental-vm-modules
import express from 'express';
import bodyParser from 'body-parser';
import { WorkflowManager } from "./src/workflow/WorkflowManager.js";
import StatedREPL from "stated-js/dist/src/StatedREPL.js";
import minimist from 'minimist';
const app = express();
app.use(bodyParser.json());
const args = minimist(process.argv.slice(2));
let snapshotOptions = undefined;
if (args.storage) {
snapshotOptions = {snapshot: {storage: args.storage}};
};
const workflowManager = new WorkflowManager(snapshotOptions);
await workflowManager.initialize();
app.get('/', (req, res) => {
res.json({ status: 'OK' });
});
app.post('/', async (req, res) => {
console.log(`Received POST /event with data: ${StatedREPL.stringify(req.body)}`);
if (req.body !== undefined && req.body.type === 'workflow') {
try {
const workflowId = await workflowManager.createWorkflow(req.body.workflow);
res.json({ workflowId, status: 'Started' });
} catch (error) {
console.error('Error in POST /workflow:', error);
res.status(500).send({'error': error.toString()});
}
return;
}
try {
res.json(await workflowManager.sendCloudEvent([req.body]));
} catch (error) {
console.error(`Error in POST /event`, error);
res.status(500).send({'error': error.toString()});
}
});
app.post('/workflow', async (req/**/, res) => {
try {
const workflowId = await workflowManager.createWorkflow(req.body);
res.json({ workflowId, status: 'Started' });
} catch (error) {
console.error('Error in POST /workflow:', error);
res.status(500).send({'error': error.toString()});
}
});
app.get('/workflow', (req, res ) => {
res.json(workflowManager.getWorkflowIds());
});
app.get('/workflow/:workflowId', (req, res) => {
const workflowId = req.params.workflowId;/**/
const workflow = workflowManager.getWorkflow(workflowId);
if (workflow) {
res.json(workflow.templateProcessor.output);
} else {
console.log(`Workflow ${workflowId} not found`);
res.status(404).send({'error': 'Workflow not found'});
}
});
app.post('/workflow/:workflowId/:type/:subscriberId', async (req, res) => {
const workflowId = req.params.workflowId;
const type = req.params.type;
const subscriberId = req.params.subscriberId;
if (!Array.isArray(req.body)) {
console.log(`data must be an array of events, but received ${req.body}`);
res.status(400).send({'error': 'data must be an array of events'});
return;
};
const workflow = workflowManager.getWorkflow(workflowId);
if (!workflow) {
console.log(`Workflow ${workflowId} not found`);
res.status(404).send({'error': 'Workflow not found'});
return;
}
try {
res.json(await workflowManager.sendEvent(workflowId, type, subscriberId, req.body));
} catch (error) {
console.error(`Error in POST /workflow/${workflowId}/${type}/${subscriberId}`, error);
res.status(500).send({'error': error.toString()});
}
});
app.delete('/workflow/:workflowId', (req, res) => {
const workflowId = req.params.workflowId;
console.log(`Received DELETE /workflow/${workflowId}`);
if (workflowManager.getWorkflow(workflowId)) {
workflowManager.deleteWorkflow(workflowId)
console.log(`Workflow ${workflowId} deleted`);
res.send({ workflowId, status: 'deleted'});
} else {
console.log(`Workflow ${workflowId} not found for deletion`);
res.status(404).send({'error': 'Workflow not found'});
}
});
app.get('/restore/:workflowId', (req, res) => {
const workflowId = req.params.workflowId;
console.log(`Received GET /restore/${workflowId}`);
try {
res.json(workflowManager.getWorkflowSnapshot(workflowId, storage));
} catch (error) {
console.error(`Error in GET /restore/${workflowId}`, error);
res.status(500).send({'error': error.toString()});
}
});
app.post('/restore/:workflowId', async (req, res) => {
const workflowId = req.params.workflowId;
console.log(`Received POST /restore/${workflowId} with data:`, req.body);
try {
await workflowManager.restoreWorkflowFromFile(workflowId, req.body);
res.json({ workflowId, status: 'restored' });
} catch (error) {
console.error(`Error in POST /workflow/${workflowId}`, error);
res.status(500).json({error: error.toString()});
}
});
app.listen(8080, () => {
console.log('Server running on port 8080');
}).on('error', (error) => {
console.error('Error starting server:', error);
});
app.post('/event', async (req, res) => {
if (!Array.isArray(req.body)) {
console.log(`data must be an array of events, but received ${StatedREPL.stringify(req.body)}`);
res.status(400).send({'error': 'data must be an array of events'});
return;
};
try {
res.json(await workflowManager.sendCloudEvent(req.body));
} catch (error) {
console.error(`Error in POST /event`, error);
res.status(500).send({'error': error.toString()});
}
});