@@ -5,7 +5,8 @@ import { Web } from "@rabbit-company/web";
55import { Logger } from "./logger" ;
66import { cors } from "@rabbit-company/web-middleware/cors" ;
77import { logger } from "@rabbit-company/web-middleware/logger" ;
8- import { Algorithm , rateLimit } from "@rabbit-company/web-middleware/rate-limit" ;
8+ import { IP_EXTRACTION_PRESETS , ipExtract } from "@rabbit-company/web-middleware/ip-extract" ;
9+ import type { CloudProvider } from "./types" ;
910
1011const { apiKey, apiSecret } = validateEnvironment ( ) ;
1112const config = {
@@ -16,6 +17,13 @@ const config = {
1617
1718const host = process . env . SERVER_HOST || "0.0.0.0" ;
1819const port = parseInt ( process . env . SERVER_PORT || "3000" ) || 3000 ;
20+ const proxy = Object . keys ( IP_EXTRACTION_PRESETS ) . includes ( process . env . PROXY || "direct" ) ? ( process . env . PROXY as CloudProvider ) : "direct" ;
21+ const updateInterval = parseInt ( process . env . UPDATE_INTERVAL || "10000" ) || 10000 ;
22+ const actualInterval = Math . max ( updateInterval , 5000 ) ;
23+
24+ if ( updateInterval < 5000 ) {
25+ Logger . warn ( `Update interval too low: ${ updateInterval } ms. Using minimum 5000ms for Trading212 API compliance` ) ;
26+ }
1927
2028const tradingClient = new Trading212Client ( config ) ;
2129const stockCache = new StockCache ( ) ;
@@ -35,25 +43,26 @@ app.use(
3543 } )
3644) ;
3745
38- app . use (
39- rateLimit ( {
40- algorithm : Algorithm . FIXED_WINDOW ,
41- windowMs : 10 * 1000 , // 10 seconds
42- max : 10 ,
43- } )
44- ) ;
46+ app . use ( ipExtract ( proxy ) ) ;
4547
4648app . get ( "/" , ( c ) => {
47- return c . json ( {
48- message : "RabbitStockAPI is running" ,
49- stocksCount : stockCache . getStockCount ( ) ,
50- instrumentsCount : stockCache . getInstrumentCount ( ) ,
51- lastUpdate : new Date ( ) . toISOString ( ) ,
52- } ) ;
49+ return c . json (
50+ {
51+ message : "RabbitStockAPI is running" ,
52+ stocksCount : stockCache . getStockCount ( ) ,
53+ instrumentsCount : stockCache . getInstrumentCount ( ) ,
54+ updateInterval : `${ actualInterval } ms` ,
55+ lastUpdate : new Date ( ) . toISOString ( ) ,
56+ } ,
57+ 200 ,
58+ { "Cache-Control" : `public, s-maxage=${ Math . floor ( actualInterval / 2 / 1000 ) } , max-age=0, stale-while-revalidate=300, stale-if-error=86400` }
59+ ) ;
5360} ) ;
5461
55- app . get ( "/stocks" , ( c ) => {
56- return c . json ( stockCache . getStocks ( ) ) ;
62+ app . get ( "/prices" , ( c ) => {
63+ return c . json ( stockCache . getStocks ( ) , 200 , {
64+ "Cache-Control" : `public, s-maxage=${ Math . floor ( actualInterval / 2 / 1000 ) } , max-age=0, stale-while-revalidate=300, stale-if-error=86400` ,
65+ } ) ;
5766} ) ;
5867
5968async function fetchInstruments ( ) : Promise < void > {
@@ -84,10 +93,12 @@ async function initializeApp(): Promise<void> {
8493 await fetchInstruments ( ) ;
8594 await updateStockPrices ( ) ;
8695
87- setInterval ( updateStockPrices , 10000 ) ;
96+ const actualInterval = Math . max ( updateInterval , 5000 ) ;
97+ setInterval ( updateStockPrices , actualInterval ) ;
8898
8999 Logger . info ( "RabbitStockAPI started successfully" ) ;
90- Logger . info ( `Server running on http://localhost:${ port } ` ) ;
100+ Logger . info ( `Server running on http://${ host } :${ port } ` ) ;
101+ Logger . info ( `Stock updates every ${ actualInterval } ms (Trading212 compliant)` ) ;
91102 Logger . info ( "Available endpoints:" ) ;
92103 Logger . info ( " GET / - Health check" ) ;
93104 Logger . info ( " GET /stocks - Stock data" ) ;
0 commit comments