@@ -4,6 +4,7 @@ const http2 = require('spdy');
44const Cabin = require ( 'cabin' ) ;
55const I18N = require ( '@ladjs/i18n' ) ;
66const Koa = require ( 'koa' ) ;
7+ const Redis = require ( '@ladjs/redis' ) ;
78const StoreIPAddress = require ( '@ladjs/store-ip-address' ) ;
89const Timeout = require ( 'koa-better-timeout' ) ;
910const _ = require ( 'lodash' ) ;
@@ -48,6 +49,13 @@ class API {
4849 // initialize the app
4950 const app = new Koa ( ) ;
5051
52+ // initialize redis
53+ const client = new Redis (
54+ this . config . redis ,
55+ logger ,
56+ this . config . redisMonitor
57+ ) ;
58+
5159 // store the server initialization
5260 // so that we can gracefully exit
5361 // later on with `server.close()`
@@ -57,51 +65,6 @@ class API {
5765 app . on ( 'error' , ( err , ctx ) => ctx . logger . error ( err ) ) ;
5866 app . on ( 'log' , logger . log ) ;
5967
60- // check if we've binded _any_ events otherwise
61- // bind all normal events and assume we use the default
62- // <https://github.com/luin/ioredis#events>
63- const client = this . config . redisClient ;
64- // go through each event listener type for ioredis and check
65- // if we've binded any listeners already
66- // <https://nodejs.org/api/events.html#events_emitter_listeners_eventname>
67- const listeners = [
68- 'connect' ,
69- 'ready' ,
70- 'error' ,
71- 'close' ,
72- 'reconnecting' ,
73- 'end' ,
74- '+node' ,
75- '-node' ,
76- 'node error'
77- ] ;
78- let bindListeners = true ;
79- for ( let i = 0 ; i < listeners . length ; i ++ ) {
80- if ( client . listeners ( listeners [ i ] ) . length > 0 ) {
81- bindListeners = false ;
82- break ;
83- }
84- }
85-
86- if ( bindListeners ) {
87- client . on ( 'connect' , ( ) =>
88- app . emit ( 'log' , 'debug' , 'redis connection established' )
89- ) ;
90- client . on ( 'ready' , ( ) =>
91- app . emit ( 'log' , 'debug' , 'redis connection ready' )
92- ) ;
93- client . on ( 'error' , err => app . emit ( 'error' , err ) ) ;
94- client . on ( 'close' , ( ) =>
95- app . emit ( 'log' , 'debug' , 'redis connection closed' )
96- ) ;
97- client . on ( 'reconnecting' , ( ) =>
98- app . emit ( 'log' , 'debug' , 'redis reconnecting' )
99- ) ;
100- client . on ( 'end' , ( ) =>
101- app . emit ( 'log' , 'debug' , 'redis connection ended' )
102- ) ;
103- }
104-
10568 // only trust proxy if enabled
10669 app . proxy = boolean ( process . env . TRUST_PROXY ) ;
10770
0 commit comments