-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.js
88 lines (84 loc) · 2.24 KB
/
tasks.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
const express = require('express')
/**
* Initializes the router for handling tasks requests.
* @param {Object} node - The node object.
* @returns {Object} - The router object.
*/
const init = (node, db) => {
/**
* Handles the save task request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*/
const saveTask = async (req, res) => {
console.log(req);
db.put(req.params.name, JSON.stringify(req.body));d
res.write("{success:true}");
res.status(200).end();
};
/**
* Handles the load task request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*/
const loadTask = async (req, res) => {
console.log("loading task");
try {
res.write(db.get(req.params.name));
res.status(200).end();
} catch (e) {
console.error(e);
res.write(JSON.stringify({
error: e
}));
res.status(500).end();
}
};
/**
* Handles the run task request.
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*/
const runTask = async (req, res) => {
console.log("run called");
var params = req.params;
let body = req.body,
pk = req.params.pk;
const args = {
...req.params
};
console.log({
body: body,
args: args
});
delete args.actionname;
delete args.pk;
try {
console.log("running", pk, params);
const kp = {
publicKey: Buffer.from(pk, "hex")
};
const lbkey = await node.lbfind(kp, "task");
const output = await node.runKey(Buffer.from(lbkey[0], "hex"), {
name: req.params.actionname,
pk: req.params.pk,
params: body || args
});
console.log("output:", output);
if (typeof output == "object")
res.write(JSON.stringify(output));
else if (typeof output == "string")
res.write(output);
res.status(200).end();
} catch (err) {
res.write(JSON.stringify(err));
res.status(500).end();
}
};
const router = express.Router();
router.get("/load/:name", loadTask);
router.post("/run/:pk/:actionname", runTask);
router.post("/save/:name", saveTask);
return router;
};
module.exports = init;