-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·88 lines (72 loc) · 2.03 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
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 koa = require("koa");
const auth = require("koa-basic-auth");
const router = require("koa-router")();
const serve = require("koa-static");
const supervisord = require("./supervisord");
const config = require("./config.json");
const app = koa();
const client = supervisord.connect(config.supervisord);
app.use(function *(next){
try {
yield next;
} catch (err) {
if (401 == err.status) {
this.status = 401;
this.set('WWW-Authenticate', 'Basic');
this.body = 'cant haz that';
} else {
throw err;
}
}
});
app.use(auth(config.nodevisor.users[0]));
app.use(serve('./static/'));
router.get('/api/readlog', function *(next) {
this.body = {
result: yield client.readLog(-1000, 0)
};
yield next;
});
const instanceMethods = {
"clearlog": "clearLog",
"shutdown": "shutdown",
"restart": "restart",
"pid": "getPID",
"state": "getState",
"version": "getSupervisorVersion",
"apiVersion": "getAPIVersion",
"identification": "getIdentification",
"processinfo": "getAllProcessInfo",
"startall": "startAllProcesses",
"stopall": "stopAllProcesses",
"reload": "reloadConfig"
}
Object.keys(instanceMethods).forEach((method) => {
const serverMethod = instanceMethods[method];
router.get("/api/" + method, function *(next) {
this.body = {
result: yield client[serverMethod](),
status: true
};
yield next;
})
})
const processMethods = {
"start": "startProcess",
"stop": "stopProcess",
"clearlog": "clearProcessLogs",
"processinfo": "getProcessInfo"
}
Object.keys(processMethods).forEach((method) => {
const serverMethod = processMethods[method];
router.get("/api/:process/" + method, function *(next) {
this.body = {
result: yield client[serverMethod](this.params.process),
status: true
};
yield next;
})
})
app.use(router.routes())
.use(router.allowedMethods());
app.listen(config.nodevisor.port);