@@ -7,9 +7,12 @@ import { sveltekitSessionHandle } from "svelte-kit-sessions";
77import RedisStore from "svelte-kit-connect-redis" ;
88import { Redis } from "ioredis" ;
99import { env } from "$env/dynamic/private" ;
10+ import { PUBLIC_OBP_BASE_URL } from "$env/static/public" ;
1011import { oauth2ProviderManager } from "$lib/oauth/providerManager" ;
1112import { SessionOAuthHelper } from "$lib/oauth/sessionHelper" ;
1213import { resourceDocsCache } from "$lib/stores/resourceDocsCache" ;
14+ import { healthCheckRegistry } from "$lib/health-check/HealthCheckRegistry" ;
15+ import { ensureSystemActivityTrail } from "$lib/opey/bootstrap/activityTrailEntities" ;
1316
1417declare const process : { env : Record < string , string | undefined > ; argv : string [ ] } ;
1518
@@ -83,6 +86,16 @@ if (!env.REDIS_HOST || !env.REDIS_PORT) {
8386// Start OAuth2 provider manager (handles initialization and retries automatically)
8487await oauth2ProviderManager . start ( ) ;
8588
89+ // Register and start health checks
90+ healthCheckRegistry . register ( { serviceName : 'OBP API' , url : `${ PUBLIC_OBP_BASE_URL } /obp/v6.0.0/root` } ) ;
91+ if ( env . OPEY_BASE_URL ) {
92+ healthCheckRegistry . register ( { serviceName : 'Opey II' , url : `${ env . OPEY_BASE_URL } /status` } ) ;
93+ }
94+ healthCheckRegistry . startAll ( ) ;
95+
96+ // Bootstrap: ensure activity trail dynamic entity exists (attempted once per server lifecycle)
97+ let activityTrailBootstrapped = false ;
98+
8699function needsAuthorization ( routeId : string ) : boolean {
87100 // protected routes are put in the /(protected)/ route group
88101 return routeId . startsWith ( "/(protected)/" ) ;
@@ -106,10 +119,11 @@ const checkAuthorization: Handle = async ({ event, resolve }) => {
106119 "No valid OAuth data found in session. Redirecting to login." ,
107120 ) ;
108121 // Redirect to login page if no OAuth data is found
122+ const redirectTo = encodeURIComponent ( event . url . pathname + event . url . search ) ;
109123 return new Response ( null , {
110124 status : 302 ,
111125 headers : {
112- Location : " /login" ,
126+ Location : ` /login?redirect_to= ${ redirectTo } ` ,
113127 } ,
114128 } ) ;
115129 }
@@ -134,21 +148,23 @@ const checkAuthorization: Handle = async ({ event, resolve }) => {
134148 logger . info ( "Destroying expired session and redirecting to login." ) ;
135149 await session . destroy ( ) ;
136150
151+ const redirectTo = encodeURIComponent ( event . url . pathname + event . url . search ) ;
137152 return new Response ( null , {
138153 status : 302 ,
139154 headers : {
140- Location : " /login" ,
155+ Location : ` /login?redirect_to= ${ redirectTo } ` ,
141156 } ,
142157 } ) ;
143158 }
144159 }
145160
146161 if ( ! session || ! session . data . user ) {
147162 // Redirect to login page if not authenticated
163+ const redirectTo = encodeURIComponent ( event . url . pathname + event . url . search ) ;
148164 return new Response ( null , {
149165 status : 302 ,
150166 headers : {
151- Location : " /login" ,
167+ Location : ` /login?redirect_to= ${ redirectTo } ` ,
152168 } ,
153169 } ) ;
154170 } else {
@@ -160,6 +176,21 @@ const checkAuthorization: Handle = async ({ event, resolve }) => {
160176 resourceDocsCache . preWarmCache ( sessionOAuth . accessToken ) . catch ( ( ) => {
161177 // Silently fail - pre-warming is best-effort
162178 } ) ;
179+
180+ // Ensure system_activity_trail entity exists (once per server lifecycle)
181+ if ( ! activityTrailBootstrapped ) {
182+ activityTrailBootstrapped = true ;
183+ ensureSystemActivityTrail ( sessionOAuth . accessToken ) . then ( ( ok ) => {
184+ if ( ! ok ) {
185+ logger . warn (
186+ "WARNING: system_activity_trail entity could not be created. " +
187+ "Ensure the API Manager consumer has the CanCreateSystemLevelDynamicEntity scope. " +
188+ "Opey activity trail features will not work without it."
189+ ) ;
190+ activityTrailBootstrapped = false ; // Allow retry on next request
191+ }
192+ } ) ;
193+ }
163194 }
164195 }
165196 }
0 commit comments