laravel

API Rate-Limiting with Lumen 5.6 and Illuminate Routing/ThrottleRequests Class

Off the back of this article, there could be some potential improvements to make the setup a bit more robust. Instead of copying classes manually into the Lumen’s middleware, it may be best to pull packages from composer and sub-class Laravel’s ThrottleRequests class – part of Laravel’s Illuminate Routing library – to avoid missing any potential changes/features in the future.

This process can be done in the following steps:


Add the Illuminate Routing library via Composer

Add Laravel’s Illuminate Routing library to your project by running the following command within your root Lumen directory:

  1. composer require illuminate/routing

 


Create a sub-classed ThrottleRequests middleware class

Instead of modifying core libraries or copying it into your project, simply create your own middleware class (in this example it was arbitrarily named RateLimits) to sub-class the existing ThrottleRequests class from the Illumincate Routing library and implement the fingerprinting function within your class.

Within your `app/Http/Middleware` folder create a new middleware class that extends \Illuminate\Routing\Middleware\ThrottleRequests.  The key is to implement the resolveRequestSignature function to define how we want to fingerprint each request.

  1. <?php
  2.  
  3. namespace App\Http\Middleware;
  4.  
  5. use Closure;
  6.  
  7. class RateLimits extends \Illuminate\Routing\Middleware\ThrottleRequests
  8. {
  9. protected function resolveRequestSignature($request)
  10. {
  11. return sha1(implode('|', [
  12. $request->method(),
  13. $request->root(),
  14. $request->path(),
  15. $request->ip(),
  16. $request->query('access_token')
  17. ]
  18. ));
  19.  
  20. return $request->fingerprint();
  21. }
  22.  
  23. }

 


Add the middleware class to the bootstrap

Once your middleware class is created. Activate the middleware via your `bootstrap/app.php` file by adding:

  1. $app->routeMiddleware([
  2. 'throttle' => App\Http\Middleware\RateLimits::class
  3. ]);

 


Implement middleware into the routes

Use your middleware class in your routes. In the following example we are allowing 2 requests every minute for the “myroute” endpoint.

  1. $router->get('/myroute', ['middleware' => ['throttle:2,1'], function () use ($router) {
  2. //do things
  3. }]);

All steps should now be completed for the Throttling middleware to be activated. If the route is refreshed 3 times very quickly (i.e. within a minute) a 429 HTTP Error response should be returned. In development mode you should see:

Farmer

Share
Published by
Farmer

Recent Posts

Changing the Facebook “tr” endpoint to send Facebook Events to another URL (GTM Server-Side – Conversion API)

As a caveat this is an **unofficial** way of achieving this as of this article's…

3 years ago

Facebook Business Manager Verification – “Start Verification” Button Disabled!?

There are many benefits to verify your Business Manager on Facebook; however, it is not…

5 years ago

Adding the Facebook Pixel to a GTM AMP Container

One issue when adding the Facebook pixel to a Google Tag Manager AMP container is…

6 years ago

Installing dependencies to get Puppeteer 1.0 running on Ubuntu 16.04

When trying to run Puppeteer 1.0 within your Node.JS scripts on an Ubuntu 16.04 box and…

6 years ago

How to use the Facebook img tag to fire pixel events

Before implementing the img tag, the previous article should be reviewed: it discusses some of…

6 years ago