{"id":15028698,"url":"https://github.com/gnikolovski/cb-rate-limiter","last_synced_at":"2025-04-09T20:32:16.240Z","repository":{"id":62511537,"uuid":"73602008","full_name":"gnikolovski/cb-rate-limiter","owner":"gnikolovski","description":"API rate limiter which uses Couchbase for storing data","archived":false,"fork":false,"pushed_at":"2020-06-04T15:35:14.000Z","size":7,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T10:50:06.850Z","etag":null,"topics":["couchbase","middleware","nosql","php","rate-limiter","rate-limiting","slim-framework"],"latest_commit_sha":null,"homepage":"https://gorannikolovski.com/blog/couchbase-api-rate-limiter","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gnikolovski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-13T07:35:04.000Z","updated_at":"2024-01-24T23:43:16.000Z","dependencies_parsed_at":"2022-11-02T12:46:26.517Z","dependency_job_id":null,"html_url":"https://github.com/gnikolovski/cb-rate-limiter","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gnikolovski%2Fcb-rate-limiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gnikolovski%2Fcb-rate-limiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gnikolovski%2Fcb-rate-limiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gnikolovski%2Fcb-rate-limiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gnikolovski","download_url":"https://codeload.github.com/gnikolovski/cb-rate-limiter/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248107271,"owners_count":21048889,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["couchbase","middleware","nosql","php","rate-limiter","rate-limiting","slim-framework"],"created_at":"2024-09-24T20:08:54.766Z","updated_at":"2025-04-09T20:32:16.210Z","avatar_url":"https://github.com/gnikolovski.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CB Rate Limiter\n\nCouchbase API Rate Limiter is a super simple PHP package for limiting access to\nyour public API. It was originally created for one of my projects, and I'm using\nit with Slimframework, but it could be used in any of your projects.\n\nI decided to go with Couchbase, because the rest of my application was using\nthis database to store data. Couchbase is a super fast NoSQL database, so it is\nperfect for this kind of tasks.\n\n## How to install?\n\nThe easiest and recommended method to install CB Rate Limiter is via composer:\n\n```\ncomposer require gnikolovski/cb-rate-limiter\n```\n\n## How to use it?\n\nBest way to use this package is as a middleware on your routes. Put it first -\nbefore any other middleware, and if user has reached the limit you imposed, you\nshould return http code 429 (too many requests) with appropriate headers.\n\nThis is how you could use this package with Slimframework as a middleware:\n\n```php\n$app = new \\Slim\\App;\n\n$app-\u003eadd(function ($request, $response, $next) {\n  $limiter = new CbRateLimiter($hostname, $bucket, $password);\n  $limiter-\u003ewhitelist($your_ip_address);\n  $exceeded = $limiter-\u003eisExceeded($ip_address, $max_requests, $in_minutes);\n\n  if (!$exceeded) {\n    $resp = $next($request, $response)\n      -\u003ewithHeader('X-RateLimit-Limit', 10)\n      -\u003ewithHeader('X-RateLimit-Remaining', $limiter-\u003egetRemaining());\n  }\n  else {\n    $resp = $response-\u003ewithStatus(429)\n      -\u003ewithHeader('X-RateLimit-Limit', 10)\n      -\u003ewithHeader('X-RateLimit-Remaining', 0);\n  }\n\n  return $resp;\n});\n\n$app-\u003eget('/', function ($request, $response, $args) {\n\t$response-\u003egetBody()-\u003ewrite('Hello world');\n\treturn $response;\n});\n\n$app-\u003erun();\n```\n\nClass is instantiated with the following three variables in the constructor:\n\n$hostname - IP address of your Couchbase server\n\n$bucket - name of the bucket where you will store data\n\n$password - database password\n\nAfter you create $limiter object you have to call isExceeded() method and\nprovide the following data:\n\n$ip_address - IP address of user trying to access API route\n\n$max_requests - max requests you are willing to accept in a unit of time\n\n$in_minutes - unit of time\n\nIP address of the user could be supplied using global PHP variable:\n\n```php\n$_SERVER['REMOTE_ADDR']\n```\nor by using some package, like akrabat/rka-ip-address-middleware.\n\nVariables $max_requests and $in_minutes should be integers. If you want to\naccept 100 requests from one IP address in 60 minutes you would write:\n\n```php\n$limiter-\u003eisExceeded($_SERVER['REMOTE_ADDR'], 100, 60);\n```\n\n## Requirements\n\nTo use Couchbase in PHP you must install PHP SDK. To find out more visit:\n(http://developer.couchbase.com/documentation/server/4.0/sdks/php-2.0/download-links.html)\n\n### AUTHOR\n\nGoran Nikolovski - https://gorannikolovski.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgnikolovski%2Fcb-rate-limiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgnikolovski%2Fcb-rate-limiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgnikolovski%2Fcb-rate-limiter/lists"}