servers.js 1.68 KB
'use strict';

import Router from 'koa-router';

import {
    Server
} from '../../models';

const r = new Router;

const envs = {
    production: '线上环境',
    preview: '灰度环境',
    test: '测试环境'
};

const servers = {
    config_page: async(ctx, next) => {
        let servers = await Server.findAll();
        servers.forEach(s => {
            s.envName = envs[s.env];
        });
        await ctx.render('action/servers', {
            servers: servers
        });
    },
    new_page: async(ctx, next) => {
        await ctx.render('action/server_form');
    },
    edit_page: async(ctx, next) => {
        let id = ctx.query.id;
        let server = await Server.findById(id);
        await ctx.render('action/server_form', server);
    },
    save: async(ctx, next) => {
        let {_id, host,username,password,port,env,deployDir } = ctx.request.body;
        let server = {
            host: host,
            username: username,
            password: password,
            port: port,
            env: env,
            deployDir: deployDir
        };
        if (_id) {
            await Server.update({
                _id: _id
            }, {
                $set: server
            });
        } else {
            await Server.insert(server);
        }
        ctx.redirect('/servers/setting');
        ctx.status = 301;
    },
    del: async(ctx, next) => {
        let id = ctx.request.body.id;
        await Server.removeById(id);
        ctx.body = {
            code: 200
        };
    }
};

r.get('/setting', servers.config_page);
r.get('/new', servers.new_page);
r.get('/edit', servers.edit_page);
r.post('/save', servers.save);
r.post('/del', servers.del);

export default r;