egg-at-plugin
TypeScript icon, indicating that this package has built-in type declarations

0.4.8 • Public • Published

凹凸租车前端egg服务插件

介绍

插件整合了error log, access log, sentry, validator,并会注册健康接口/health

如何使用

首先

$ yarn add egg-at-plugin

然后进行配置

// config/config.{env}.js
 
config.sentry = {
  dsn: 'xxx'
}  // 添加sentry配置, 如果配置不存在则不进行注册
 
// config/plugin.js
 
exports.atPlugin = {
  enable: true,
  package: 'egg-at-plugin'
}

最后就得到了所有的功能啦~

接入

当前validator功能是需要服务自行接入的, 默认约定实现Validator Service, 实现validate方法, 方法签名为(path:string, method: string, data: any)

path为当前controller注册的路由, method为当前controller使用的http方法, data为当前参数所在区域(requestBody为body, requestQuery为不含数组的http query, requestQueries为含复数的http query)

举个🌰

// app/lib/service/validator.js
module.exports = class Validator {
  validate: (path, method, data) => {
    // some valiation here
  }
}
 
// app/controller/home.js
 
class Home extends Controller {
  // 如果当前controller 传入参数为 { light: 'blue' }, 调用方法为POST, 参数存在于HTTP body中
  async livingRoom () {
    const value = this.ctx.validator.requestBody()
    const { light } = value;
    // some bll here
  }
 
  // 如果当前controller 传入参数为 { knife: 'blue' }, 调用方法为GET, 参数存在于HTTP query
  async kitchen () {
    const value = this.ctx.validator.requestQuery()
    const { knife } = value;
    // some bll here
  }
 
  // 如果当前controller 传入参数为 { clothes: 'blue' }, 调用方法为GET, 参数存在于HTTP query 但是存在数组形式的参数
  async balcony () {
    const value = this.ctx.validator.requestQueries()
    const { clothes } = value;
    // some bll here
  }
}
 

sentry接入:

// config/config.{env}.js
 
exports.sentry = {
  dsn: 'https://819e74a6e948468b9740680cfa87986b:38aaa0c0d51e463597493c250ff11f83@sentry.io/246025',
  errCallback: (err) => { // 可选
    // return 为 true 上发sentry记录, 其余忽略不进行上传, 默认只上传http code 500以上的错误及无http code的错误
    return !err.status || err.status >= 500
  }
};
 
// 主动上发sentry error ()
 
ctx.logSentryError(err) // 会自动集成当前环境上下文
 
// 或者
 
app.Raven.setContext({
  // sonme context
})
app.Raven.captureException(err, {
  // some tags
})
 

eureka接入:

// config/config.{env}.js
exports.eureka = {
  instance: {
    app: 'auto-node-eureka-test',
    instanceId: 'egg-eureka-pro',
    hostName: 'localhost',
    ipAddr: '127.0.0.1',
    port: {
      $: 8080,
      '@enabled': 'true',
    },
    homePageUrl: '/health',
    statusPageUrl: '/health',
    healthCheckUrl: '/health',
    secureVipAddress: '127.0.0.1',
    vipAddress: '127.0.0.1',
    dataCenterInfo: {
      '@class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
      name: 'MyOwn',
    },
  },
  eureka: {
    servicePath: '/eureka/apps/',
    host: '121.199.4.107',
    port: 1246,
  },
}
 

TODO

  • 完善的单元测试
  • 接入apollo(egg的启动顺序坑爹啊)
  • 继续完善

Package Sidebar

Install

npm i egg-at-plugin

Weekly Downloads

10

Version

0.4.8

License

MIT

Unpacked Size

26 kB

Total Files

16

Last publish

Collaborators

  • chunbo54813