routers.js 1.73 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';
import pageCache from  './actions/page_cache';
import cdnCache from  './actions/cdn_cache';
import productCache from  './actions/product_cache';

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('/page_cache', pageCache.routes(), pageCache.allowedMethods());
    base.use('/cdn_cache', cdnCache.routes(), cdnCache.allowedMethods());
    base.use('/product_cache', productCache.routes(), productCache.allowedMethods());

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

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

}