Skip to content

Commit 267a6c1

Browse files
PowerDosthonatos
authored andcommitted
feat: suppurt redis cluster, closes #11 (#12)
1 parent 06248ac commit 267a6c1

File tree

17 files changed

+826
-10
lines changed

17 files changed

+826
-10
lines changed

.travis.yml

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,27 @@
1-
sudo: false
1+
sudo: required
22
language: node_js
33
services:
4-
- redis-server
4+
- redis-server
55
node_js:
6-
- '8'
6+
- '8'
7+
78
install:
8-
- npm i npminstall && npminstall
9+
- npm i npminstall && npminstall
10+
911
script:
10-
- npm run ci
12+
- npm run ci
13+
14+
before_script:
15+
- chmod +x test/travis/redis_setup.sh
16+
- test/travis/redis_setup.sh
17+
1118
after_script:
12-
- npminstall codecov && codecov
19+
- npminstall codecov && codecov
20+
1321
env:
1422
global:
1523
# WECHAT_API_APP_ID
1624
- secure: V/eQAaNWHxx2rJ/pLxJgFqB1K1RuzaoxPmidwLuD2Bnmg1DLJbOjYv126st4qCXyJgntppzzT4CA6F1HeSLzMYgDQJ7uOcLo8c9dpAtKUc55ZSYjfLZTn70WADC4ZBPcRpStDvRW91wFtC+MoMrrol2+p71wA1n3Y3belBC8Ios2KPIEDmhovk0TlGVOkiYEvuLSPNXUrSUeeJx+8NXGYUihmdBX4deCXRPz08YhPEVoTx8KPMRSnplso56XFIDj/RBbJ/GcGWEiEa7cUc+MRd06YUAnuvWEtO8o41BWgcLMBDPqbbkgo3Vob0gCmrk28P78osR/8kRCiZ9gUAXjSYKZq94Yej0QGtNnOrhlddtVsiep2AtAa+trOY63cfTAdUeSDxJZbtO8/+p/dgH+Mk0Y6Qak9KKgUMfbTaOTwD2/bZpKRT32CULNa1R3NRpto2LUcQCtelqVCDT+xAIRQIXYUh4P1Mxy8bgqNXCUTF0VDcQN4Rw9J9ozztucnL720ddTJ8swHI7KmSjNQp+DjA9xWmXBwk8Tf+/bJbh0v0XPTxHJpiSdrCsEJCqoDuoFRdecfuwk3QQtSUnlaabClMnD6Gaxnu4VphSrrpsR+jzF6mtd1vet9FVqiCQKcJ0PbtrgCEHZJt92Df4ekmzjcT8BDRJtFTZmLbU39R1Bd8Y=
1725
# WECHAT_API_APP_SECRET
1826
- secure: L1zBUlb8TOwI4jgYKaGnMQ91cKFCpl5MPu0HTMP23CaxUgI4dVrTo0CWUUte6sGMJ8s3cXYb2N7HztF/TzlyPkmMURnIlAbgGrYeLucK48b1GxyLSACvCqVLlU9avS1c8YH6e6cBQCF/1lK4LsV4LIV7VCH4gyOJdSFAi6mNIVBdWAgALXtrJeWH0dN/XMaRz/Lngk1oI6fcm0R7bjTFo5bjoNovx+oDulPtHzQfQX8fuIIoK9UeibOgjsGr/bLiDmK5H3SWKa0I757NLD5XH36ven0CYdEOygtE49OspUbSEwcwF7Oki3bxydRNgft1A89AvXpf3qK/axkvDUb/mpjYiSyik9fdV8MPqAbYmLfDofvFv68SeudsDQ8Faip8kYezN33pL8GntTGst8WjmyXvYCTneKL/yXttyWEpINHm0uBHau1Q9ZTpr9Y00smGWzCX3/PKKhjuWIFQ3LbdXV0B2QcaBNbtf4LXkHc7Nj7JsgQbAWIWUfc49X5aC4DFg5y9bYBS6H0Ww5uqj3Wyfm8f6vpSZlThUBhSH2IykZXRMph30H9dnyTrOnM4h49WoJXwq3ajcuvChySBxrusDxhmDrkjJpWSYZzxlnyq4785EPh5URr8cUpjhg+XhoSCDw04B98KU79I4r3Mk+XkTMUgvpl8VeKQRbRoW7WZhyY=
27+
- REDIS_INSTANCE=foo

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ exports.wechatApi = {
5656
package: 'egg-wechat-api',
5757
};
5858
```
59+
> if redis is single client
5960
6061
```js
6162
// {app_root}/config/config.default.js
@@ -64,6 +65,16 @@ exports.wechatApi = {
6465
appSecret: '',
6566
};
6667
```
68+
> if redis is multi clients
69+
70+
```js
71+
// {app_root}/config/config.default.js
72+
exports.wechatApi = {
73+
appId: '',
74+
appSecret: '',
75+
redisInstance: '', // select instance of redis
76+
};
77+
```
6778

6879
> __Redis is required !__
6980

config/config.default.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ exports.wechatApi = {
1414
// wechat
1515
appId: '',
1616
appSecret: '',
17+
redisInstance: '',
1718
};

lib/wechat-api.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ const WechatApi = require('co-wechat-api');
55

66
module.exports = app => {
77

8-
const { redis } = app;
9-
const { appId, appSecret } = app.config.wechatApi || {};
8+
let redis = app.redis;
9+
const { appId, appSecret, redisInstance } = app.config.wechatApi || {};
10+
11+
// select redis instance
12+
if (redisInstance) {
13+
redis = app.redis.get(redisInstance);
14+
}
1015

1116
// check key & secret
1217
assert(appId && appSecret,
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
exports.keys = '123456';
4+
5+
exports.redis = {
6+
clients: {
7+
foo: {
8+
port: 6379,
9+
host: '127.0.0.1',
10+
password: null,
11+
db: 0,
12+
},
13+
bar: {
14+
port: 6380,
15+
host: '127.0.0.1',
16+
password: null,
17+
db: 1,
18+
},
19+
},
20+
};
21+
22+
exports.wechatApi = {
23+
default: {
24+
},
25+
app: true,
26+
agent: false,
27+
28+
// wechat
29+
appId: process.env.WECHAT_API_APP_ID,
30+
appSecret: process.env.WECHAT_API_APP_SECRET,
31+
redisInstance: process.env.REDIS_INSTANCE,
32+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
module.exports = app => {
4+
5+
app.get('/', function* () {
6+
const { wechatApi } = app;
7+
8+
try {
9+
const ticket = yield wechatApi.getTicket();
10+
this.status = 200;
11+
this.body = ticket;
12+
13+
} catch (error) {
14+
this.status = 500;
15+
this.body = error;
16+
}
17+
});
18+
};

0 commit comments

Comments
 (0)