@@ -38,18 +38,8 @@ class API {
3838 ...config
3939 } ;
4040
41- const { logger } = this . config ;
42-
43- let storeIPAddress = false ;
44-
45- if ( this . config . storeIPAddress )
46- storeIPAddress = new StoreIPAddress ( {
47- logger,
48- ...this . config . storeIPAddress
49- } ) ;
50-
5141 const cabin = new Cabin ( {
52- logger,
42+ logger : this . config . logger ,
5343 ...this . config . cabin
5444 } ) ;
5545
@@ -58,14 +48,16 @@ class API {
5848
5949 // listen for error and log events emitted by app
6050 app . on ( 'error' , ( err , ctx ) => {
61- ctx . logger [ err . status && err . status < 500 ? 'warn' : 'error' ] ( err ) ;
51+ const level = err . status && err . status < 500 ? 'warn' : 'error' ;
52+ if ( ctx . logger ) ctx . logger [ level ] ( err ) ;
53+ else cabin [ level ] ( err ) ;
6254 } ) ;
63- app . on ( 'log' , logger . log ) ;
55+ app . on ( 'log' , cabin . log ) ;
6456
6557 // initialize redis
6658 const client = new Redis (
6759 this . config . redis ,
68- logger ,
60+ cabin ,
6961 this . config . redisMonitor
7062 ) ;
7163
@@ -74,19 +66,40 @@ class API {
7466 // later on with `server.close()`
7567 let server ;
7668
77- // override koa's undocumented error handler
78- // <https://github.com/sindresorhus/eslint-plugin-unicorn/issues/174>
79- app . context . onerror = errorHandler ;
80-
8169 // allow middleware to access redis client
8270 app . context . client = client ;
8371
72+ // override koa's undocumented error handler
73+ app . context . onerror = errorHandler ( false , cabin ) ;
74+
8475 // only trust proxy if enabled
8576 app . proxy = boolean ( process . env . TRUST_PROXY ) ;
8677
8778 // specify that this is our api (used by error handler)
8879 app . context . api = true ;
8980
81+ // allow before hooks to get setup
82+ if ( _ . isFunction ( this . config . hookBeforeSetup ) )
83+ this . config . hookBeforeSetup ( app ) ;
84+
85+ // rate limiting
86+ if ( this . config . rateLimit )
87+ app . use (
88+ ratelimit ( {
89+ ...this . config . rateLimit ,
90+ db : client
91+ } )
92+ ) ;
93+
94+ // basic auth
95+ if ( this . config . auth ) app . use ( auth ( this . config . auth ) ) ;
96+
97+ // configure timeout
98+ if ( this . config . timeout ) {
99+ const timeout = new Timeout ( this . config . timeout ) ;
100+ app . use ( timeout . middleware ) ;
101+ }
102+
90103 // adds request received hrtime and date symbols to request object
91104 // (which is used by Cabin internally to add `request.timestamp` to logs
92105 app . use ( requestReceived ) ;
@@ -104,21 +117,10 @@ class API {
104117 if ( this . config . i18n ) {
105118 const i18n = this . config . i18n . config
106119 ? this . config . i18n
107- : new I18N ( { ...this . config . i18n , logger } ) ;
120+ : new I18N ( { ...this . config . i18n , logger : cabin } ) ;
108121 app . use ( i18n . middleware ) ;
109122 }
110123
111- if ( this . config . auth ) app . use ( auth ( this . config . auth ) ) ;
112-
113- // rate limiting
114- if ( this . config . rateLimit )
115- app . use (
116- ratelimit ( {
117- ...this . config . rateLimit ,
118- db : client
119- } )
120- ) ;
121-
122124 // conditional-get
123125 app . use ( conditional ( ) ) ;
124126
@@ -137,20 +139,20 @@ class API {
137139 // pretty-printed json responses
138140 app . use ( json ( ) ) ;
139141
140- // 404 handler
141- app . use ( koa404Handler ) ;
142-
143142 // passport
144143 if ( this . config . passport ) app . use ( this . config . passport . initialize ( ) ) ;
145144
146- // configure timeout
147- if ( this . config . timeout ) {
148- const timeout = new Timeout ( this . config . timeout ) ;
149- app . use ( timeout . middleware ) ;
145+ // store the user's last ip address in the background
146+ if ( this . config . storeIPAddress ) {
147+ const storeIPAddress = new StoreIPAddress ( {
148+ logger : cabin ,
149+ ...this . config . storeIPAddress
150+ } ) ;
151+ app . use ( storeIPAddress . middleware ) ;
150152 }
151153
152- // store the user's last ip address in the background
153- if ( storeIPAddress ) app . use ( storeIPAddress . middleware ) ;
154+ // 404 handler
155+ app . use ( koa404Handler ) ;
154156
155157 // allow before hooks to get setup
156158 if ( _ . isFunction ( this . config . hookBeforeRoutes ) )
0 commit comments