@@ -192,4 +192,57 @@ public static class EndpointExtensions
192192 . SetVaryByHost ( true )
193193 . Tag ( "rss" ) ) ;
194194 }
195+
196+ public static void MapCacheRefreshEndpoint ( this WebApplication app )
197+ {
198+ app . MapPost ( "/api/cache/refresh" , async ( HttpContext context , IContentService contentService , ILogger < Program > logger , IWebHostEnvironment environment , IConfiguration configuration ) =>
199+ {
200+ try
201+ {
202+ logger . LogInformation ( "Cache refresh requested via API endpoint" ) ;
203+
204+ // Validate API key for security
205+ var expectedApiKey = configuration [ "CacheRefresh:ApiKey" ] ;
206+ if ( ! string . IsNullOrEmpty ( expectedApiKey ) )
207+ {
208+ var providedApiKey = context . Request . Headers [ "X-API-Key" ] . FirstOrDefault ( ) ;
209+ if ( string . IsNullOrEmpty ( providedApiKey ) || providedApiKey != expectedApiKey )
210+ {
211+ logger . LogWarning ( "Cache refresh request rejected: Invalid or missing API key" ) ;
212+ return Results . Unauthorized ( ) ;
213+ }
214+ }
215+ else if ( ! environment . IsDevelopment ( ) )
216+ {
217+ logger . LogWarning ( "Cache refresh API key not configured in production environment" ) ;
218+ return Results . Problem ( "API key not configured" , statusCode : 500 ) ;
219+ }
220+
221+ // Only perform cache refresh in non-development environments
222+ // In development, the cache isn't as critical and may require Azure Table Storage
223+ if ( environment . IsDevelopment ( ) )
224+ {
225+ logger . LogInformation ( "Development environment detected. Skipping cache refresh." ) ;
226+ return Results . Ok ( new {
227+ message = "Cache refresh skipped in development environment" ,
228+ timestamp = DateTime . UtcNow ,
229+ environment = "Development"
230+ } ) ;
231+ }
232+
233+ await contentService . RefreshContentAsync ( ) ;
234+
235+ logger . LogInformation ( "Cache refresh completed successfully" ) ;
236+ return Results . Ok ( new { message = "Cache refreshed successfully" , timestamp = DateTime . UtcNow } ) ;
237+ }
238+ catch ( Exception ex )
239+ {
240+ logger . LogError ( ex , "Error refreshing cache via API endpoint" ) ;
241+ return Results . Problem ( "Failed to refresh cache" , statusCode : 500 ) ;
242+ }
243+ } )
244+ . WithName ( "RefreshCache" )
245+ . WithSummary ( "Refresh the content cache" )
246+ . WithDescription ( "Triggers a refresh of the in-memory content cache from Azure Table Storage. Requires X-API-Key header for authentication." ) ;
247+ }
195248}
0 commit comments