@@ -10,6 +10,7 @@ var fs = require('fs')
1010var configUtil = require ( 'sb-config-util' )
1111var _ = require ( 'lodash' )
1212var logger = require ( 'sb_logger_util_v2' )
13+ var ApiInterceptor = require ( 'sb_api_interceptor' )
1314
1415const contentProvider = require ( 'sb_content_provider_util' )
1516var contentMetaProvider = require ( './contentMetaFilter' )
@@ -19,7 +20,7 @@ const contentProviderConfigPath = path.join(__dirname, '/config/contentProviderA
1920var contentProviderApiConfig = JSON . parse ( fs . readFileSync ( contentProviderConfigPath ) )
2021const telemtryEventConfig = JSON . parse ( fs . readFileSync ( path . join ( __dirname , 'config/telemetryEventConfig.json' ) ) )
2122
22- var reqDataLimitOfContentUpload = '50mb'
23+ var reqDataLimitOfContentUpload = process . env . sunbird_content_upload_data_limit || '50mb'
2324
2425const port = process . env . sunbird_content_service_port ? process . env . sunbird_content_service_port : 5000
2526const defaultChannel = process . env . sunbird_default_channel || 'sunbird'
@@ -29,6 +30,11 @@ globalEkstepProxyBaseUrl = process.env.sunbird_content_plugin_base_url ? process
2930const contentRepoBaseUrl = process . env . sunbird_content_repo_api_base_url || 'https://qa.ekstep.in/api'
3031const contentRepoApiKey = process . env . sunbird_content_repo_api_key
3132
33+ const contentServiceBaseUrl = process . env . sunbird_contnet_service_base_url || 'http://content-service:9000'
34+ const contentServiceAuthToken = process . env . sunbird_content_service_auth_token
35+
36+ const assessmentServiceBaseUrl = process . env . sunbird_assessment_service_base_url || 'http://assessment-service:9000'
37+
3238const learnerServiceLocalBaseUrl = process . env . sunbird_learner_service_local_base_url
3339 ? process . env . sunbird_learner_service_local_base_url
3440 : 'http://learner-service:9000'
@@ -50,10 +56,14 @@ const producerId = process.env.sunbird_environment + '.' + process.env.sunbird_i
5056const sunbirdPortalBaseUrl = process . env . sunbird_portal_base_url || 'https://staging.open-sunbird.org'
5157const lockExpiryTime = process . env . sunbird_lock_expiry_time || 3600
5258const isHealthCheckEnabled = process . env . sunbird_health_check_enable || 'true'
53- const contentServiceLocalBaseUrl = process . env . sunbird_content_service_local_base_url ? process . env . sunbird_content_service_local_base_url : 'http://content -service:5000'
59+ const contentServiceLocalBaseUrl = process . env . sunbird_content_service_local_base_url ? process . env . sunbird_content_service_local_base_url : 'http://knowledge-mw -service:5000'
5460const sunbirdGzipEnable = process . env . sunbird_gzip_enable || 'true'
61+ const kidTokenPublicKeyBasePath = process . env . sunbird_kid_public_key_base_path || '/keys/'
5562
5663configUtil . setContentProviderApi ( contentProviderApiConfig . API )
64+ configUtil . setConfig ( 'CONTENT_SERVICE_BASE_URL' , contentServiceBaseUrl )
65+ configUtil . setConfig ( 'CONTENT_SERVICE_AUTH_TOKEN' , contentServiceAuthToken )
66+ configUtil . setConfig ( 'ASSESSMENT_SERVICE_BASE_URL' , assessmentServiceBaseUrl )
5767configUtil . setConfig ( 'CONTENT_REPO_BASE_URL' , contentRepoBaseUrl )
5868configUtil . setConfig ( 'TELEMETRY_BASE_URL' , telemetryBaseUrl )
5969configUtil . setConfig ( 'CONTENT_REPO_AUTHORIZATION_TOKEN' , 'Bearer ' + contentRepoApiKey )
@@ -148,7 +158,7 @@ app.use(function (req, res, next) {
148158 res . sendStatus ( 200 )
149159 } else {
150160 next ( )
151- } ;
161+ }
152162} )
153163
154164require ( './routes/healthCheckRoutes' ) ( app )
@@ -167,10 +177,34 @@ require('./routes/externalUrlMetaRoute')(app)
167177require ( './routes/pluginsRoutes' ) ( app )
168178require ( './routes/collaborationRoutes' ) ( app )
169179require ( './routes/lockRoutes' ) ( app )
180+ require ( './routes/questionRoutes' ) ( app )
170181// this middleware route add after all the routes
171182require ( './middlewares/proxy.middleware' ) ( app )
172183
173- function startServer ( ) {
184+ async function startServer ( cb ) {
185+ var keyCloakConfig = {
186+ 'authServerUrl' : process . env . sunbird_keycloak_auth_server_url ? process . env . sunbird_keycloak_auth_server_url : 'https://staging.open-sunbird.org/auth' ,
187+ 'realm' : process . env . sunbird_keycloak_realm ? process . env . sunbird_keycloak_realm : 'sunbird' ,
188+ 'clientId' : process . env . sunbird_keycloak_client_id ? process . env . sunbird_keycloak_client_id : 'portal' ,
189+ 'public' : process . env . sunbird_keycloak_public ? process . env . sunbird_keycloak_public : true ,
190+ 'realmPublicKey' : process . env . sunbird_keycloak_public_key
191+ }
192+ logger . info ( { msg : 'keyCloakConfig' , keyCloakConfig } )
193+
194+ var cacheConfig = {
195+ store : process . env . sunbird_cache_store ? process . env . sunbird_cache_store : 'memory' ,
196+ ttl : process . env . sunbird_cache_ttl ? process . env . sunbird_cache_ttl : 1800
197+ }
198+
199+ var apiInterceptor = new ApiInterceptor ( keyCloakConfig , cacheConfig )
200+
201+ await apiInterceptor . loadTokenPublicKeys ( path . join ( __dirname , kidTokenPublicKeyBasePath ) )
202+
203+ if ( this . server ) {
204+ cb && cb ( )
205+ return
206+ }
207+
174208 this . server = http . createServer ( app ) . listen ( port , function ( ) {
175209 logger . info ( { msg : `server running at PORT ${ port } ` } )
176210 logger . debug ( { msg : `server started at ${ new Date ( ) } ` } )
@@ -186,8 +220,9 @@ function startServer () {
186220 logger . fatal ( { msg : 'error in getting meta filters' , err } )
187221 process . exit ( 1 )
188222 } )
223+ cb && cb ( )
189224 } )
190- this . server . keepAliveTimeout = 60000 * 5 ;
225+ this . server . keepAliveTimeout = 30000 * 5
191226}
192227
193228// Create server
@@ -206,12 +241,6 @@ if (defaultChannel) {
206241 startServer ( )
207242}
208243
209- // Close server, when we start for test cases
210- exports . close = function ( ) {
211- logger . debug ( { msg : `server stopped at ${ new Date ( ) } ` } )
212- this . server . close ( )
213- }
214-
215244// Telemetry initialization
216245const telemetryBatchSize = parseInt ( process . env . sunbird_telemetry_sync_batch_size , 10 ) || 20
217246telemtryEventConfig . pdata . id = producerId
@@ -226,3 +255,15 @@ const telemetryConfig = {
226255
227256logger . debug ( { msg : 'Telemetry is initialized.' } )
228257telemetry . init ( telemetryConfig )
258+ process . on ( 'unhandledRejection' , ( reason , p ) => {
259+ console . log ( 'Kp-mw Unhandled Rejection' , p , reason )
260+ logger . error ( { msg : 'Kp-mw Unhandled Rejection' , p, reason } )
261+ } )
262+ process . on ( 'uncaughtException' , ( err ) => {
263+ console . log ( 'Kp-mw Uncaught Exception' , err )
264+ logger . error ( { msg : 'Kp-mw Uncaught Exception' , err } )
265+ process . exit ( 1 )
266+ } )
267+
268+ exports . start = startServer
269+ exports . close = ( cb ) => { this . server . close ( cb ) }
0 commit comments