degrade.js
3.74 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
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
'use strict';
const Router = require('koa-router');
const moment = require('moment');
const _ = require('lodash');
const process = require('process');
const {Degrade, DegradeServer} = require('../../models');
const getter = require('../../zookeeper/getter');
const setter = require('../../zookeeper/setter');
const tester = require('../../zookeeper/tester');
const router = new Router();
const ctl = {
async index (ctx) {
let qcloudServer = await DegradeServer.findOne({
type: 'qcloud'
});
if (!qcloudServer) {
qcloudServer = {};
}
let awsServer = await DegradeServer.findOne({
type: 'aws'
});
if (!awsServer) {
awsServer = {};
}
await ctx.render('action/degrade', {
qCloudConfig: {
ip: qcloudServer.ip,
port: qcloudServer.port
},
awsConfig: {
ip: awsServer.ip,
port: awsServer.port
}
});
},
async connect(ctx) {
let {ip, port} = ctx.query;
let server = `${ip}:${port}`;
let connected = await tester(server);
// connecting test
if (!connected) {
ctx.body = `<p class="connect-err">
<i class="fa fa-wheelchair" aria-hidden="true"></i>
Sorry, I can not connect to <span class="server-name">${server}</span>.Please check whether your ip/port is correct or the zookeeper server is running
</p>`;
return;
}
let degrades = await Degrade.findAll();
for(let i of degrades) {
// 从zookeeper读取配置信息,memcached只做PHP读取使用
i.checked = await getter(server, i.path);
}
let pc = _.filter(degrades, o => _.startsWith(o.path, '/pc'));
let wap = _.filter(degrades, o => _.startsWith(o.path, '/wap'));
await ctx.render('action/degrade_list', {
layout: false,
pc: pc,
wap: wap
});
},
async server(ctx) {
let {ip, port, type} = ctx.request.body;
let serverCount = await DegradeServer.count({
type: type
});
// keep one server
if (serverCount) {
let serverConfig = await DegradeServer.findOne({
type: type
});
let id = serverConfig._id; // get the latest item
await DegradeServer.update({
_id: id
}, {
$set: {
ip: ip,
port: port
}
});
} else {
await DegradeServer.insert({
ip: ip,
port: port,
type: type
});
}
ctx.body = {
code: 200,
message: `${serverCount ? 'update' : 'new'} ${type} server success`
};
},
async setter(ctx) {
let {checked, id, type} = ctx.query;
let theDegrade = await Degrade.findById(id);
let path = theDegrade.path;
let server = await await DegradeServer.findOne({
type: type
});
let result = await setter(`${server.ip}:${server.port}`, path, checked.toString());
// result结果以zookeeper写为准
if (result) {
ctx.body = {
code: 200,
message: 'update success'
};
} else {
ctx.body = {
code: 500,
message: 'update fail,Please retry'
}
}
}
};
router.get('/', ctl.index);
router.post('/server', ctl.server);
router.get('/connect', ctl.connect);
router.get('/setter', ctl.setter);
module.exports = router;