config.default.js 3.69 KB
/*
 * @Author: limin
 * @Date: 2019-11-16 09:26:53
 * @Last Modified by: huyue
 * @Last Modified time: 2020-09-07 15:10:57
 */

/* eslint valid-jsdoc: "off" */

'use strict'

/**
 * @param {Egg.EggAppInfo} app app info
 */
module.exports = app => {
  /**
   * built-in config
   * @type {Egg.EggAppConfig}
   **/
  const config = (exports = {})

  // use for cookie sign key, should change to your own and keep security
  config.keys = app.name + '_1573615133248_6253'

  // add your middleware config here
  // config.middleware = [ 'errorHandler']

  config.onerror = {
    // 全局异常捕获
    all(err, ctx) {
      // 在此处定义针对所有响应类型的错误处理方法
      // 注意,定义了 config.all 之后,其他错误处理方法不会再生效

      // 所有的异常都在 app 上触发一个 error 事件,框架会记录一条错误日志
      ctx.app.emit('error', err, ctx)
      const status = err.status || 500
      // 生产环境时 500 错误的详细错误内容不返回给客户端,因为可能包含敏感信息
      const error =
      status === 500 && ctx.app.config.env === 'prod'
        ? 'Internal Server Error'
        : err.message
      // 从 error 对象上读出各个属性,设置到响应中
      ctx.body = JSON.stringify(
        ctx.response.ServerResponse.Error({ message: error })
      )
      if (status === 422) {
        ctx.body.detail = err.errors
      }
      ctx.status = status
    }
  // html(err, ctx) {
  //   // html hander
  //   ctx.body = '<h3>error</h3>'
  //   ctx.status = 500
  // },
  // json(err, ctx) {
  //   // json hander
  //   ctx.body = { message: 'error' }
  //   ctx.status = 500
  // },
  // jsonp(err, ctx) {
  //   // 一般来说,不需要特殊针对 jsonp 进行错误定义,jsonp 的错误处理会自动调用 json 错误处理,并包装成 jsonp 的响应格式
  // },
  }

  // add your user config here
  // const userConfig = {
  config.jwt = {
    secret: 'glsx-insurence-egg-server' // 自定义 token 的加密条件字符串
  }
  config.cors = {
    // origin: "http://192.168.2.222:8085",
    credentials: true,
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
  }
  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true
    },
    // domainWhiteList: ["http://192.168.2.222:8085"] //允许访问接口的白名单
    domainWhiteList: ['http://localhost:8080', 'http://192.168.2.201:8085', 'http://192.168.2.27:8085'] // 允许访问接口的白名单
  }
  config.redis = {
    client: {
      port: 6379, // Redis port
      host: '127.0.0.1', // Redis host
      password: '',
      db: 0
    }
  }
  config.cluster = {
    listen: {
      path: '',
      port: 7002,
      hostname: '0.0.0.0'
    }
  }
  config.validate = {
    // convert: false,
    // validateRoot: false,
  }
  config.sequelize = {
    timezone: '+08:00',
    define: {
      freezeTableName: true, // 禁止修改表名,默认情况下,sequelize将自动将所有传递的模型名称(define的第一个参数)转换为复数
      // 但是为了安全着想,复数的转换可能会发生变化,所以禁止该行为
      timestamps: false // 不自动维护时间戳 [ created_at、updated_at ]
    },
    dialectOptions: {
      dateStrings: true, // 格式化datetime
      typeCast: true
    }
  }

  config.swaggerdoc = {
    dirScanner: './app/controller',
    apiInfo: {
      title: '广联运营',
      description: 'swagger-ui for egg',
      version: '1.0.0'
    },
    schemes: ['http', 'https'],
    consumes: ['application/json'],
    produces: ['application/json'],
    securityDefinitions: {},
    enableSecurity: false,
    // enableValidate: true,
    routerMap: true,
    enable: true
  }
  return config
}