@@ -25,6 +25,7 @@ app.use(requestId);
2525
2626// ─── SSE Event Stream ────────────────────────────────────────
2727const sseClients = [ ] ;
28+ let x402MiddlewareReady = false ;
2829
2930function broadcast ( event ) {
3031 const data = JSON . stringify ( event ) ;
@@ -33,6 +34,55 @@ function broadcast(event) {
3334 } ) ;
3435}
3536
37+ function buildReadinessPayload ( ) {
38+ const anthropicConfigured = ! ! config . anthropicApiKey ;
39+ const x402Enabled = ! ! config . serverAddress ;
40+
41+ const components = {
42+ app : {
43+ ready : true ,
44+ description : 'Core HTTP server initialized' ,
45+ } ,
46+ anthropic : {
47+ configured : anthropicConfigured ,
48+ ready : anthropicConfigured ,
49+ description : anthropicConfigured
50+ ? 'Anthropic API key is configured for Claude-powered agents'
51+ : 'Missing Anthropic API key; demo fallback responses are available' ,
52+ } ,
53+ x402 : {
54+ enabled : x402Enabled ,
55+ ready : ! x402Enabled || ( x402MiddlewareReady && ! ! config . facilitatorUrl ) ,
56+ description : x402Enabled
57+ ? ( x402MiddlewareReady
58+ ? 'x402 payment middleware initialized'
59+ : 'x402 is enabled but middleware initialization failed' )
60+ : 'x402 paywall is disabled; premium endpoints are not protected by payments' ,
61+ } ,
62+ } ;
63+
64+ const ready = Object . values ( components ) . every ( component => component . ready ) ;
65+ return {
66+ status : ready ? 'ready' : 'not_ready' ,
67+ ready,
68+ timestamp : new Date ( ) . toISOString ( ) ,
69+ components,
70+ } ;
71+ }
72+
73+ app . get ( '/healthz' , ( req , res ) => {
74+ res . json ( {
75+ status : 'ok' ,
76+ uptime : process . uptime ( ) ,
77+ timestamp : new Date ( ) . toISOString ( ) ,
78+ } ) ;
79+ } ) ;
80+
81+ app . get ( '/readyz' , ( req , res ) => {
82+ const payload = buildReadinessPayload ( ) ;
83+ res . status ( payload . ready ? 200 : 503 ) . json ( payload ) ;
84+ } ) ;
85+
3686app . get ( '/api/events' , ( req , res ) => {
3787 res . setHeader ( 'Content-Type' , 'text/event-stream' ) ;
3888 res . setHeader ( 'Cache-Control' , 'no-cache' ) ;
@@ -96,11 +146,14 @@ if (config.serverAddress) {
96146 [ { network : config . network , server : stellarScheme } ] ,
97147 )
98148 ) ;
149+ x402MiddlewareReady = true ;
99150 console . log ( '✅ x402 payment middleware active' ) ;
100151 } catch ( err ) {
152+ x402MiddlewareReady = false ;
101153 console . warn ( '⚠️ x402 middleware init failed (non-fatal):' , err . message ) ;
102154 }
103155} else {
156+ x402MiddlewareReady = false ;
104157 console . warn ( '⚠️ No SERVER_STELLAR_ADDRESS set — x402 paywall disabled' ) ;
105158}
106159
0 commit comments