Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Imfdj committed Nov 17, 2020
0 parents commit 40d183e
Show file tree
Hide file tree
Showing 111 changed files with 6,678 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .autod.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict';

module.exports = {
write: true,
prefix: '^',
plugin: 'autod-egg',
test: [
'test',
'benchmark',
],
dep: [
'egg',
'egg-scripts',
],
devdep: [
'egg-ci',
'egg-bin',
'egg-mock',
'autod',
'autod-egg',
'eslint',
'eslint-config-egg',
],
exclude: [
'./test/fixtures',
'./dist',
],
};

1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
coverage
8 changes: 8 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "eslint-config-egg",
"rules": {
"no-unused-vars": "off",
"template-curly-spacing": "off",
"array-bracket-spacing": "off"
}
}
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
logs/
npm-debug.log
yarn-error.log
node_modules/
package-lock.json
yarn.lock
coverage/
.idea/
run/
.DS_Store
*.sw*
*.un~
typings/
.nyc_output/
app/public/uploads/*
10 changes: 10 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';

const path = require('path');

module.exports = {
config: path.join(__dirname, 'database/config.json'),
'migrations-path': path.join(__dirname, 'database/migrations'),
'seeders-path': path.join(__dirname, 'database/seeders'),
'models-path': path.join(__dirname, 'app/model'),
};
12 changes: 12 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
sudo: false
language: node_js
node_js:
- '10'
before_install:
- npm i npminstall -g
install:
- npminstall
script:
- npm run ci
after_script:
- npminstall codecov && codecov
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# egg-beehive



## QuickStart

<!-- add docs here for user -->

see [egg docs][egg] for more detail.

### ToDo

```bash
1.
```

### Development

```bash
$ npm i
$ npm run dev
$ open http://localhost:7001/
```

### Sequelize

```bash
add dababases table:
$ sequelize model:generate --name bug --attributes name:string,password:string
migrate dababases table:
$ sequelize db:migrate
$ sequelize db:migrate --env test
```

### Deploy

```bash
$ npm start
$ npm stop
```

### npm scripts

- Use `npm run lint` to check code style.
- Use `npm test` to run unit test.
- Use `npm run autod` to auto detect dependencies upgrade, see [autod](https://www.npmjs.com/package/autod) for more detail.

### 参考
[egg]: https://eggjs.org
[egg-ts-helper]: https://cnpmjs.org/package/egg-ts-helper
[sequelize]: https://sequelize.org/master/class/lib/model.js~Model.html#static-method-findOne
33 changes: 33 additions & 0 deletions app-boot-hook-do/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
module.exports = {
// 资源数据缓存到redis
async permissionsToRedis(app) {
const ctx = await app.createAnonymousContext();
const { models } = app.model;
const { redis } = app;
const permissionsPromise = models.permissions.findAll({ limit: 10000 });
const rolesPromise = models.roles.findAll({
attributes: [ 'id', 'name' ],
include: [ { attributes: [ 'id', 'url', 'action' ], model: models.permissions } ],
limit: 10000,
raw: false,
});
const redisKeysPermissions = redis.keys('permissions:url:*');
const [ permissions, roles, redisKeys ] = await Promise.all([ permissionsPromise, rolesPromise, redisKeysPermissions ]);
const pipeline = redis.pipeline();
// 删除所有permissions:url:*
redisKeys.forEach(v => pipeline.del(v));
permissions.forEach(v => pipeline.hmset(ctx.helper.redisKeys.permissionsBaseActionUrl(v.action, v.url), v.dataValues));
// 根据角色id存储对应资源
const rolesArr = JSON.parse(JSON.stringify(roles));
rolesArr.forEach(e => {
pipeline.del(ctx.helper.redisKeys.rolePermissionsBaseRoleId(e.id));
if (e.permissions.length) {
const arr = [];
e.permissions.forEach(permission => arr.push(`${ permission.action }_${ permission.url }`));
pipeline.sadd(ctx.helper.redisKeys.rolePermissionsBaseRoleId(e.id), arr);
}
});
await pipeline.exec();
},
};
89 changes: 89 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
'use strict';
const lodash = require('lodash');
const dayjs = require('dayjs');
const { permissionsToRedis } = require('./app-boot-hook-do');

class AppBootHook {
constructor(app) {
this.app = app;
}

configWillLoad() {
// 此时 config 文件已经被读取并合并,但是还并未生效
// 这是应用层修改配置的最后时机
// 注意:此函数只支持同步调用

// 例如:参数中的密码是加密的,在此处进行解密
// this.app.config.mysql.password = decrypt(this.app.config.mysql.password);
// 例如:插入一个中间件到框架的 coreMiddleware 之间
// const statusIdx = this.app.config.coreMiddleware.indexOf('status');
// this.app.config.coreMiddleware.splice(statusIdx + 1, 0, 'limit');
}

async didLoad() {
// 所有的配置已经加载完毕
// 可以用来加载应用自定义的文件,启动自定义的服务

// 例如:创建自定义应用的示例
// this.app.queue = new Queue(this.app.config.queue);
// await this.app.queue.init();

// 例如:加载自定义的目录
// this.app.loader.loadToContext(path.join(__dirname, 'app/tasks'), 'tasks', {
// fieldClass: 'tasksClasses',
// });
}

async willReady() {
// 所有的插件都已启动完毕,但是应用整体还未 ready
// 可以做一些数据初始化等操作,这些操作成功才会启动应用

// 例如:从数据库加载数据到内存缓存
// this.app.cacheData = await this.app.model.query(QUERY_CACHE_SQL);
this.app.lodash = lodash;
this.app.dayjs = dayjs;
console.log('willReady');
console.time('willReady');
const { Sequelize } = this.app.model;
// 资源数据缓存到redis
await permissionsToRedis(this.app);

Sequelize.addHook('beforeValidate', (permission, options) => {
// 做些什么
});
Sequelize.addHook('afterBulkUpdate', options => {
console.log(2222222);
console.log(options);
});
Sequelize.addHook('afterSave', (permission, options) => {
console.log(333333);
console.log(permission);
});
}

async didReady() {
// 应用已经启动完毕
console.log('didReady');
console.timeEnd('willReady');
// const ctx = await this.app.createAnonymousContext();
// await ctx.service.Biz.request();
console.log(999999);
// console.log(this.app.service);
// console.log(this.app.service.permissions.findAll);
// const permissionsList = await this.app.service.permissions.findAll({ limit: 10000, offset: 0 });
// console.log(permissionsList);

}

async serverDidReady() {
console.log('serverDidReady');
// http / https server 已启动,开始接受外部请求
// 此时可以从 app.server 拿到 server 的实例

this.app.server.on('timeout', socket => {
// handle socket timeout
});
}
}

module.exports = AppBootHook;
8 changes: 8 additions & 0 deletions app/contract/dto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';

module.exports = {
// user: {
// id: { type: 'number', description: 'id 唯一键' },
// file: { type: 'file', description: '文件' },
// },
};
36 changes: 36 additions & 0 deletions app/contract/request/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const body = {
configurationId: {
id: { type: 'number', required: true, description: 'id' },
},
configurationBodyReq: {
rsa_private_key: {
type: 'string',
required: true,
max: 1000,
trim: true,
example: '',
description: 'rsa私钥',
},
rsa_public_key: {
type: 'string',
required: true,
max: 1000,
trim: true,
example: '',
description: 'rsa公钥',
},
},
};

module.exports = {
...body,
configurationPutBodyReq: {
...body.configurationId,
...body.configurationBodyReq,
},
configurationDelBodyReq: {
ids: { type: 'array', required: true, itemType: 'number', description: 'ids', example: [ 1, 2 ] },
},
};
Loading

0 comments on commit 40d183e

Please sign in to comment.