routers.js 2.9 KB
'use strict';
const Router = require('koa-router');

const common = require('./actions/common');
const index = require('./actions/index');
const projects = require('./actions/projects');
const servers = require('./actions/servers');
const login = require('./actions/login');
const monitor = require('./actions/monitor');
const users = require('./actions/users');
const hotfix = require('./actions/hotfix');
const operationLog = require('./actions/operation_log');
const pageCache = require('./actions/page_cache');
const cdnCache = require('./actions/cdn_cache');
const productCache = require('./actions/product_cache');
const apiCache = require('./actions/api_cache');
const degrade = require('./actions/degrade');
const deploy = require('./actions/deploy');
const api = require('./actions/api');
const abuseProtection = require('./actions/abuse_protection');
const crawler = require('./actions/crawler');
const checkcode = require('./actions/checkcode').router;
const noAuth = new Router();
const base = new Router();

module.exports = function(app) {

    noAuth.use('', login.routes(), login.allowedMethods());
    noAuth.use('', common.routes(), common.allowedMethods());
    noAuth.use('/api', api.routes(), api.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('/check', checkcode.routes(), checkcode.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('/api_cache', apiCache.routes(), apiCache.allowedMethods());
    base.use('/degrade', degrade.routes(), degrade.allowedMethods());
    base.use('/deploys', deploy.routes(), deploy.allowedMethods());

    const white = crawler('/crawler/ip_whitelists', '/crawler/ua_whitelists', '白名单', false);
    const black = crawler('/crawler/ip_blacklists', '/crawler/ua_blacklists', '黑名单', true);

    base.use('/crawler_white', white.routes(), white.allowedMethods());
    base.use('/crawler_black', black.routes(), black.allowedMethods());

    base.use('/abuse_protection', abuseProtection.routes(), degrade.allowedMethods());

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

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

}