degrade.js 3.74 KB
'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;