-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
142 lines (125 loc) · 5.02 KB
/
app.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
// CONFIG /////////////////////////////////////////////////////////
var config = require('./config.json');
var currentstateConfig = 0;
// SUBPROCESSING /////////////////////////////////////////////////////////
// import child_process with CommonJS // TODO mrw change to EJS
const { spawn } = require('child_process');
// FASTIFY /////////////////////////////////////////////////////////
// import fastify with CommonJS // TODO mrw change to EJS
const fastify = require('fastify')({
logger: true
})
// import fastify auth plugin
fastify.register(require('fastify-auth'));
// satisfy external loads
fastify.register(require('fastify-cors'), { origin: '*' });
// import fastify redis plugin
fastify.register(require('fastify-redis'), { host: config.redis.host, port: config.redis.port })
// FASTIFY-AUTH /////////////////////////////////////////////////////////
const authenticate = {realm: 'Westeros'}
fastify.register(require('fastify-jwt'), {
secret: 'supersecret'
})
fastify.decorate("authenticate", async function(req, reply) {
try {
await req.jwtVerify()
} catch (err) {
reply.send(err)
}
})
fastify.post('/signup', (req, reply) => {
// some code
if ( typeof req.body !== 'undefined' && req.body ) {
// console.log(req.body);
if ( req.body.user !== 'undefined' && req.body.user && req.body.password !== 'undefined' && req.body.password ) {
if ( req.body.user === 'install' && req.body.password === 'serial' ) {
const userId = req.body.user;
const payload = { algorithm: 'HS256', expiresIn: 120, subject: userId }
const secret = 'supersecret';
const token = fastify.jwt.sign(payload, secret, {
algorithm: 'HS256',
expiresIn: '10m' // if ommited, the token will not expire
})
reply.send({"status": "ok","csrf": "csrf_hash","data": [{ token }],"errors": []})
} else {
reply.send({"status":"error","csrf":"csrf_hash","data":[],"errors":["User and/or Password does not match"]})
}
} else
{
reply.send({"status":"error","csrf":"csrf_hash","data":[],"errors":["Error in request",{"field":"body","message":"User and/or Password not given","significancy":"error"}]})
}
} else {
reply.send({"status":"error","csrf":"csrf_hash","data":[],"errors":["Error in request",{"field":"body","message":"Body was empty","significancy":"error"}]})
}
})
fastify.get('/pause', (req, reply) => {
setTimeout(() => {
reply.send({pause: '1500'})
}, 1500);
})
fastify.get('/version', (req, reply) => {
reply.send({version: 'VA01A'})
})
fastify.get('/versionui', (req, reply) => {
reply.send({version: '0.1.0'})
})
fastify.get('/status/config', (req, reply) => {
reply.send({config: currentstateConfig})
})
fastify.get('/status/config/0', { preValidation: [fastify.authenticate] }, (req, reply) => {
currentstateConfig = 0;
reply.send({config: '0'})
})
fastify.get('/status/config/1', { preValidation: [fastify.authenticate] }, (req, reply) => {
currentstateConfig = 1;
reply.send({config: '1'})
})
// start subprocess routine
fastify.get('/start', { preValidation: [fastify.authenticate] }, function (req, reply) {
console.log(JSON.stringify(req.headers));
if ( typeof req.query.extra_vars !== 'undefined' && req.query.extra_vars ) {
if ( typeof req.query.playbook !== 'undefined' && req.query.playbook ) {
const { redis } = fastify
let queuename = makeid(10)
console.log(queuename)
redis.xadd('work2do','*','listname',queuename,req.query.playbook,req.query.extra_vars);
redis.xread('BLOCK', 15000, 'STREAMS', queuename, '$', (err, str) => {
if (str === null) {
reply.send({status: 'timeout'})
} else {
str.forEach(message => {
console.log(message);
reply.send(message[1][0][1][1]);
});
}
})
} else {
reply.send({ module: 'start', result: 'error', errormsg: 'parameter playbook missing' })
}
} else {
reply.send({ module: 'start', result: 'error', errormsg: 'parameter extra_vars missing' })
}
})
// SCHEDULER /////////////////////////////////////////////////////////
// repetitive work
function intervalFunc() {
fastify.log.info('unstoppable task');
}
setInterval(intervalFunc, config.app.schedulerms);
// start the fastify server
fastify.listen(config.app.port, config.app.host, function (err, address) {
if (err) {
fastify.log.error(err)
process.exit(1)
}
})
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
return result;
}