routers.js 1.36 KB
import Router from 'koa-router';

import common from './actions/common';
import index from './actions/index';
import projects from './actions/projects';
import servers from './actions/servers';
import login from './actions/login';
import monitor from './actions/monitor';
import users from './actions/users';
import hotfix from  './actions/hotfix';
import operationLog from './actions/operation_log';

const noAuth = new Router();
const base = new Router();

export default function (app) {

    noAuth.use('', login.routes(), login.allowedMethods());
    noAuth.use('', common.routes(), common.allowedMethods());

    app.use(noAuth.routes(), noAuth.allowedMethods());
    app.use(async (ctx, next) => {

        if (ctx.session && ctx.session.user) {
            await next();
        } else {
            ctx.redirect('/login');
        }
    });

    base.use('/projects', projects.routes(), projects.allowedMethods());
    base.use('/servers', servers.routes(), servers.allowedMethods());
    base.use('/monitor', monitor.routes(), monitor.allowedMethods());
    base.use('/users', users.routes(), users.allowedMethods());
    base.use('/hotfix', hotfix.routes(), hotfix.allowedMethods());
    base.use('/operation', operationLog.routes(), operationLog.allowedMethods());

    base.use('', index.routes(), index.allowedMethods());

    app.use(base.routes(), base.allowedMethods());

}