import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
const path = require('path');

export default (appInfo: EggAppInfo) => {
  const config = {} as PowerPartial<EggAppConfig>;

  // override config from framework / plugin
  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1600244957952_7142';

  // add your egg config in here
  config.middleware = ['log', 'proxy'];
  
  config.cluster = {
    listen: {
      port: 7500
    }
  };
  // add your special config in here
  const bizConfig = {
    sourceUrl: `https://github.com/eggjs/examples/tree/master/${appInfo.name}`,
  };

  config.mongoose = {
    url: 'mongodb://dbop:zyzdbopbantu@dds-8vbdb47c6fb58a541.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vbdb47c6fb58a542.mongodb.zhangbei.rds.aliyuncs.com:3717/zyz?replicaSet=mgset-500808098', // 内网
    options: { useNewUrlParser: true, useUnifiedTopology: true },
  };
  config.gmmongoose = {
    url: 'mongodb://dbop:zyzGm2021@dds-8vb9964bb4cc7f241.mongodb.zhangbei.rds.aliyuncs.com:3717,dds-8vb9964bb4cc7f242.mongodb.zhangbei.rds.aliyuncs.com:3717/zyzgm?replicaSet=mgset-507933150', // 内网
    options: { useNewUrlParser: true, useUnifiedTopology: true },
  };
  config.redis = {
    url: 'r-8vb4i2kgl91886fkxd.redis.zhangbei.rds.aliyuncs.com', // 内网
    pw: 'zyz_2020'
  };

  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true
    },
    domainWhiteList: ['http://localhost:9000']
  };

  // 配置上传
  config.multipart = {
    fileSize: '100mb',
    mode: 'stream',
    whitelist: [
      '.json', '.ts', '.zip'
    ],
    fileExtensions: ['.json', '.ts', '.zip', '.tar.gz'], // 扩展几种上传的文件格式
    autoFields: true
  };

  // config.alinode = {
  //   appid: '86043',
  //   secret: '54ef0364995b0c4f2ab42150e29ad30df8327a3a',
  //   error_log: [ '/root/logs/zyz/zyz-web.log', '/root/logs/zyz/common-error.log', '/root/logs/zyz/egg-agent.log' ],
  //   packages: [ '/root/zyz/web-server/package.json' ],
  // };

  config.customLogger = {
    linkLogger: {
      file: path.join(appInfo.root, 'logs/gm-server/link-log.log'),
      formatter(meta) {
        return `[${meta.level}] [${meta.date}] ${meta.message}`;
      },
    },
  };

  config.logrotator = {
    filesRotateBySize: [
      path.join(appInfo.root, 'logs/gm-server/link-log.log'),
    ],
    maxFileSize: 1024,
  };

  let regions = [    // 大区数据
    { env: 'stable', name: "常山少年", domain: 'http://zyz_gm.trgame.cn' },
    { env: 'alpha', name: "测试服", domain: 'http://pinus_gm_test.trgame.cn' },
    { env: 'dev', name: "开发服", domain: 'http://zyzdev_gm.trgame.cn' },
    { env: 'isbn', name: "办好福", domain: 'http://zyz_isbn_gm.trgame.cn' },
    // 37测试服组
    { env: 'sq1', name: "37测试1服", domain: 'http://gm-sgzzyz.yev242.com' },
    { env: 'sq3', name: "小范围对外测试", domain: 'http://sq3-web-sgzzyz.yev242.com' },
    { env: 'sq4', name: "渠道二测", domain: 'http://sq6-web-sgzzyz.yev242.com' },
    { env: 'sq7', name: "精英测试", domain: 'http://sq8-web-sgzzyz.yev242.com' },
    { env: 'sq9', name: "渠道吸量", domain: 'http://sq10-web-yjz.yev242.com' },
  ];
  config.regions = regions;
  
  let httpProxy: any = {};
  for(let { env, domain } of regions) {
    httpProxy[`/api/${env}/`] = {
      target: domain,
      changeOrigin: true,
      secure: true,
      pathRewrite: function(path) {
        console.log('proxy', path, path.replace(`/api/${env}/`, '/api/'))
        return path.replace(`/api/${env}/`, '/api/') 
      }
    }
  }
  config.proxy = httpProxy;

  // the return config will combines to EggAppConfig
  return {
    ...config,
    ...bizConfig,
  };
};
