Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gnikolovski/cb-rate-limiter
API rate limiter which uses Couchbase for storing data
https://github.com/gnikolovski/cb-rate-limiter
couchbase middleware nosql php rate-limiter rate-limiting slim-framework
Last synced: 2 months ago
JSON representation
API rate limiter which uses Couchbase for storing data
- Host: GitHub
- URL: https://github.com/gnikolovski/cb-rate-limiter
- Owner: gnikolovski
- License: mit
- Created: 2016-11-13T07:35:04.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2020-06-04T15:35:14.000Z (over 4 years ago)
- Last Synced: 2024-09-28T20:21:07.157Z (3 months ago)
- Topics: couchbase, middleware, nosql, php, rate-limiter, rate-limiting, slim-framework
- Language: PHP
- Homepage: https://gorannikolovski.com/blog/couchbase-api-rate-limiter
- Size: 6.84 KB
- Stars: 5
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# CB Rate Limiter
Couchbase API Rate Limiter is a super simple PHP package for limiting access to
your public API. It was originally created for one of my projects, and I'm using
it with Slimframework, but it could be used in any of your projects.I decided to go with Couchbase, because the rest of my application was using
this database to store data. Couchbase is a super fast NoSQL database, so it is
perfect for this kind of tasks.## How to install?
The easiest and recommended method to install CB Rate Limiter is via composer:
```
composer require gnikolovski/cb-rate-limiter
```## How to use it?
Best way to use this package is as a middleware on your routes. Put it first -
before any other middleware, and if user has reached the limit you imposed, you
should return http code 429 (too many requests) with appropriate headers.This is how you could use this package with Slimframework as a middleware:
```php
$app = new \Slim\App;$app->add(function ($request, $response, $next) {
$limiter = new CbRateLimiter($hostname, $bucket, $password);
$limiter->whitelist($your_ip_address);
$exceeded = $limiter->isExceeded($ip_address, $max_requests, $in_minutes);if (!$exceeded) {
$resp = $next($request, $response)
->withHeader('X-RateLimit-Limit', 10)
->withHeader('X-RateLimit-Remaining', $limiter->getRemaining());
}
else {
$resp = $response->withStatus(429)
->withHeader('X-RateLimit-Limit', 10)
->withHeader('X-RateLimit-Remaining', 0);
}return $resp;
});$app->get('/', function ($request, $response, $args) {
$response->getBody()->write('Hello world');
return $response;
});$app->run();
```Class is instantiated with the following three variables in the constructor:
$hostname - IP address of your Couchbase server
$bucket - name of the bucket where you will store data
$password - database password
After you create $limiter object you have to call isExceeded() method and
provide the following data:$ip_address - IP address of user trying to access API route
$max_requests - max requests you are willing to accept in a unit of time
$in_minutes - unit of time
IP address of the user could be supplied using global PHP variable:
```php
$_SERVER['REMOTE_ADDR']
```
or by using some package, like akrabat/rka-ip-address-middleware.Variables $max_requests and $in_minutes should be integers. If you want to
accept 100 requests from one IP address in 60 minutes you would write:```php
$limiter->isExceeded($_SERVER['REMOTE_ADDR'], 100, 60);
```## Requirements
To use Couchbase in PHP you must install PHP SDK. To find out more visit:
(http://developer.couchbase.com/documentation/server/4.0/sdks/php-2.0/download-links.html)### AUTHOR
Goran Nikolovski - https://gorannikolovski.com