api_cache.js
2.03 KB
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
'use strict';
const Router = require('koa-router');
const _ = require('lodash');
const {
MemcachedHost
} = require('../../models');
const ApiCache = require('../../ci/api_cache');
let r = new Router();
const api_cache = {
async index(ctx) {
let hosts = await MemcachedHost.findAll();
hosts.forEach(host => {
host.hostFm = host.host.replace(/\./g, '-').replace(/:/g, '-');
});
await ctx.render('action/memcached', {hosts: hosts});
},
async addHost(ctx) {
let q = ctx.request.body;
await MemcachedHost.insert({host: q.host});
return ctx.body = {
code: 200
};
},
async delHost(ctx) {
let q = ctx.request.body;
await MemcachedHost.removeById(q.id);
return ctx.body = {
code: 200
};
},
async cleanKey(ctx) {
let q = ctx.request.body;
let key = q.key;
let hosts = await MemcachedHost.findAll();
_.each(hosts, (h) => {
(new ApiCache(h.host)).clean(key);
});
return ctx.body = {
code: 200
};
},
async flushAll(ctx) {
let hosts = await MemcachedHost.findAll();
_.each(hosts, (h) => {
(new ApiCache(h.host)).flushAll();
});
return ctx.body = {
code: 200
};
},
async monit(ctx) {
let hosts = await MemcachedHost.findAll();
_.each(hosts, (h) => {
(new ApiCache(h.host)).stats();
});
return ctx.body = {
code: 200
};
},
async restart(ctx) {
let host = ctx.request.body.host;
(new ApiCache(host)).restart();
return ctx.body = {
code: 200
};
}
};
r.get('/', api_cache.index);
r.get('/monit', api_cache.monit);
r.post('/host/add', api_cache.addHost);
r.post('/host/del', api_cache.delHost);
r.post('/clean', api_cache.cleanKey);
r.post('/flush', api_cache.flushAll);
r.post('/restart', api_cache.restart);
module.exports = r;