@tuia/tuia-egg

1.6.1 • Public • Published

tuia-egg

tuia-egg framework

QuickStart

  1. yarn global add tuia-cli
  2. tuia-cli init <项目名>
  3. cd <项目名>
  4. yarn
  5. yarn dev

OS Support

  • windows
  • linux
  • macOS

vscode插件(建议安装)

申请容器规范

目录、代码规范

祥见egg目录结构

Use Logger

// 日志级别分为debug info warn error
// app对象(常用于middleware, app生命周期内)
this.app.TuiaLogger.info('category', '日志内容');
// ctx对象(常用于controller, service)
this.ctx.TuiaLogger.info('category', '日志内容');
// 全局使用
global.TuiaLogger.info('category', '日志内容');

Use BiLogger

// 日志不分级别,统一使用info
// group: node日志一般固定10,type: 区分不同需求,json: 具体日志数据.
// app对象(常用于middleware, app生命周期内)
this.app.BiLogger.info({group: 10, type: 1, json: {key: value}});
// ctx对象(常用于controller, service)
this.ctx.BiLogger.info({group: 10, type: 1, json: {key: value}});
// 全局使用
global.TuiaLogger.info({group: 10, type: 1, json: {key: value}});

## Use ERRCODE

```js
// controller(建议在controller书写响应)
// 成功返回
this.ctx.success({
  desc: 'desc',
  data: {
    list: [],
    count: 0
  }
})
// 失败返回
this.ctx.fail({
  error: this.ctx.ERRCODE.SYSTEM_ERROR,
  desc: '重写错误描述'
})

// 如何自定义
// 只要满足error既定的数据结构即可,结构如下
exports.SYSTEM_ERROR = {
  code: 999999,
  desc: '系统错误',
};

Use Eureka

// How to config
// config.default.js

config.eureka = {
  services: ['TUIA-ADVERT-CENTER']
}

// How to use
// service(建议在service中书写具体业务逻辑)

this.app.Eureka.getHost('TUIA-ADVERT-CENTER')

OR

global.Eureka.getHost('TUIA-ADVERT-CENTER')

注意

如果本地启服务注册到开发环境,访问开发环境域名会有流量分流到本地来,请注意!!!

Use Proxy

// config/config.default.js

config.proxy = {
  filter (pathname, req) {
    return pathname.match('^/proxytest') && req.method === 'GET'
  },
  options: {
    // 默认target,实际没有用,因为使用router来做动态target
    target: 'http://www.baidu.com',
    router(req) {
      return global.Eureka.getHost('TUIA-ACTIVITY-WEB')
    },
    pathRewrite: {
      '^/proxytest': ''
    },
    changeOrigin: true
  }
}

Use SSO And JWT/Cookie-Session

config.sso = {
  // 项目系统id,查找地址是 http://sso.duiba.com.cn/ui#/permissionSystem/applications/appList,输入系统名称后查询前面的ID
  systemId: 214,
  // 项目名
  systemName: 'tuia-jssdk-manage-node',
  // 对应环境上的加密密钥,这里以开发环境为例
  // http://console.duibadev.com.cn/apollo/#/service/applications/tuia-worktable-node/accessKey
  accessKey: 'a6fa0e0307b84d90835fc600fa831c84',
  // 过滤接口,不走sso拦截器用
  filters: []
};

验证流程

使用JWT

  • 框架已将jwt的值存储在localStorage中
  • 请求头需要增加jwt请求头作用户会话校验。比如jwt: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c

使用Cookie

  • 服务端直接通过HTTP协议操作Cookie即可

注意

如果使用JWT方案,当用户信息失效(比如修改密码)或提前过期(比如修改用户名、生日等)时,需要前端代码配合清除缓存的jwt从而走重新签发jwt的流程以更新信息。如果使用Cookie-Session方案则清除服务端(或者redis)session即可。

Use Config Center

// https://console.duibadev.com.cn/apollo/#/service/applications/config/tuia-jimu-web-node
// 取对应环境(默认为dev)对应工程(一般对应package.json的name值)的所有配置
let allConfig = global.getFromConfigCenter();
// 取单个配置。返回明文(无论是否加密)
let configValue = global.getFromConfigCenter('key');

Use IP

// config/config.default.js
config.proxy = true;
config.maxProxyCount = 1;

// use ctx
this.ctx.ip         // 获取用户的 IP 地址
this.ctx.host       // 获取用户请求的域名
this.ctx.protocol   // 获取用户请求的协议

Use CORS

// config/plugin.js
exports.cors = {
  enable: true,
  package: 'egg-cors',
};

// config/config.default.js
// 支持跨域域名
exports.security = {
// domainWhiteList: [ 'http://localhost:4200' ],
};
// 支持跨域,若设置origin,则security中的白名单失效
exports.cors = {
// {string|Function} origin: '*',
// {string|Array} allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH'
};

Use Cat

class HomeController extends Controller {
  async index() {
    this.ctx.body = 'hi, egg';

    // 获取初始化好的cat对象
    let cat = this.ctx.app.catClient;
    threadCat = new cat.Cat(true);
    //创建一个transaction实例
    const trans = threadCat.newTransaction('TestTran', 'DDD');
    //添加数据
    trans.addData('key', 'value');
    trans.logEvent('EventTransaction', 'E1111');
    const subTrans = a.newTransaction('TestSubTran', 'GGGGG');
    subTrans.addData('subKey', 'subValue');
    trans.setStatus(cat.STATUS.SUCCESS);
    setTimeout(function() {
      trans.complete();
    }, 1000);

    // Event
    cat.logEvent('Event', 'E1');
    cat.logEvent('Event', 'E2', cat.STATUS.FAIL);
    cat.logEvent('Event', 'E3', 'failed');
    // Error
    cat.logError('ErrorInTransaction', new Error());
  }
}

module.exports = HomeController;

tuia-egg默认已统一监控所有接口请求,自动开启各环境cat,项目中无需配置。

注意事项:

  1. config.aliyunTest.js不会覆盖config.default.js,需手动添加。

  2. 容器镜像目前状态:

    node8:image-node < node10:cat < node10:dumb-init < node10:gifsicle

    node10:dumb-init 以后包含 dumb-init 版本的镜像,更新 Dockerfile 启动脚本的最后一行为 ENTRYPOINT [“/usr/local/bin/dumb-init”, “node”, “server”] 目的是解决 puppeteerdocker 中运行时的僵尸进程。

  3. cat装包问题,待解决,目前手动解决方案流程

  4. cpu核数根据部署的容器环境而定,假如 APP_CPU 环境变量缺失,默认1核。多核场景需注意定时任务的运用,详见egg定时任务

Readme

Keywords

Package Sidebar

Install

npm i @tuia/tuia-egg

Weekly Downloads

0

Version

1.6.1

License

MIT

Unpacked Size

29.4 kB

Total Files

42

Last publish

Collaborators

  • any-u
  • dinglh
  • candy_dd
  • wintersun97
  • tump
  • inuanfeng
  • zhoucheng
  • zhangmin173
  • aiduck
  • chao.zhou
  • babalk
  • leitingting
  • nbseven
  • linhao-vue
  • liusandy