@@ -752,7 +752,10 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(Section: TStrin
752752 Section.AppendLine(' ' + Model[TConfigKey.jsonrpc_unit_name] + ' ,' )
753753 end ;
754754
755- Section.AppendLine(' System.IOUtils,' ).AppendLine(' MVCFramework.Commons,' );
755+ Section
756+ .AppendLine(' System.IOUtils,' )
757+ .AppendLine(' MVCFramework.Commons,' )
758+ .AppendLine(' MVCFramework.Logger,' );
756759
757760 if Model.B[TConfigKey.program_ssv_templatepro] then
758761 begin
@@ -785,9 +788,19 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(Section: TStrin
785788 .AppendLine(' MVCFramework.Middleware.Trace,' )
786789 .AppendLine(' MVCFramework.Middleware.CORS,' )
787790 .AppendLine(' MVCFramework.Middleware.ETag,' )
791+ .AppendLine(' MVCFramework.Middleware.RateLimit,' )
788792 .AppendLine(' MVCFramework.Middleware.Compression;' )
789793 .AppendLine
790- .AppendLine(' procedure ' + Model.S[TConfigKey.webmodule_classname] + ' .WebModuleCreate(Sender: TObject);' )
794+ .AppendLine(' procedure ' + Model.S[TConfigKey.webmodule_classname] + ' .WebModuleCreate(Sender: TObject);' );
795+
796+ if Model.B[TConfigKey.webmodule_middleware_ratelimit] then
797+ begin
798+ Section
799+ .AppendLine(' var' )
800+ .AppendLine(' LRateLimitMiddleware: TMVCRateLimitMiddleware;' );
801+ end ;
802+
803+ Section
791804 .AppendLine(' begin' )
792805 .AppendLine(' fMVC := TMVCEngine.Create(Self,' )
793806 .AppendLine(' procedure(Config: TMVCConfig)' )
@@ -873,8 +886,44 @@ procedure TUnitWebModuleDeclarationCommand.ExecuteImplementation(Section: TStrin
873886 .AppendLine;
874887 end ;
875888
889+
890+ if Model.B[TConfigKey.webmodule_middleware_ratelimit] then
891+ begin
892+ Section
893+ .AppendLine(' LRateLimitMiddleware := TMVCRateLimitMiddleware.Create(' )
894+ .AppendLine(' 10, // Max requests' )
895+ .AppendLine(' 60, // Window in seconds' )
896+ .AppendLine(' rlkIPAddress // Rate limit by IP address' )
897+ .AppendLine(' );' )
898+ .AppendLine(' ' )
899+ .AppendLine(' // Exclude health check endpoint from rate limiting (uncomment and configure)' )
900+ .AppendLine(' // LRateLimitMiddleware.AddExcludedPath('' /health'' );' )
901+ .AppendLine(' // LRateLimitMiddleware.AddExcludedPath('' /metrics'' );' )
902+ .AppendLine(' ' )
903+ .AppendLine(' // Set custom callback for rate limit exceeded event' )
904+ .AppendLine(' LRateLimitMiddleware.SetOnRateLimitExceeded(' )
905+ .AppendLine(' procedure(const AContext: TWebContext; const AKey: string;' )
906+ .AppendLine(' const ALimit: Integer; const AWindowSeconds: Integer)' )
907+ .AppendLine(' begin' )
908+ .AppendLine(' LogW(Format('' Rate limit exceeded for key: %s (Limit: %d requests per %d seconds)'' ,' )
909+ .AppendLine(' [AKey, ALimit, AWindowSeconds]));' )
910+ .AppendLine(' end' )
911+ .AppendLine(' );' )
912+
913+ end ;
914+
915+
916+ Section
917+ .AppendLine(' // Middleware' );
918+
919+ if Model.B[TConfigKey.webmodule_middleware_ratelimit] then
920+ begin
921+ Section
922+ .AppendLine(' fMVC.AddMiddleware(LRateLimitMiddleware);' );
923+ end ;
924+
925+
876926 Section
877- .AppendLine(' // Middleware' )
878927 .AppendLine(' // To use memory session uncomment the following line' )
879928 .AppendLine(' // fMVC.AddMiddleware(UseMemorySessionMiddleware);' )
880929 .AppendLine(' //' )
0 commit comments