Welcome Guest, Not a member yet? Register   Sign In
How should I implement x-api-key and api rate limit in CodeIgniter 4 ?
#1

Hi

I have a bunch of ResourceController controllers which build a simple api rest
Then I have a filter with this before code (which is assigned only to api routes)

Code:
Services::response()->setHeader('Access-Control-Allow-Origin',$base_url);
        if (strtolower($request->getMethod()) === 'options') {
            return Services::response()
                ->setHeader("Access-Control-Allow-Headers",
                    [
                        'X-API-KEY',
                        'Origin',
                        'X-Requested-With',
                        'Content-Type',
                        'Accept',
                        'Access-Control-Request-Method',
                        'Authorization',
                    ])
                ->setHeader("Access-Control-Allow-Methods", ['GET', 'POST', 'OPTIONS', 'PUT', 'DELETE']);
        }

I have to implement for all the api controllers, the following

1. X-API-KEY
2. API rate limit (fixed for all routes, very basic)

Which would be the best way to implement each? Create a new filter for each feature?
Does anyone have any useful links to share to get started?

Thanks a lot
Reply
#2

New filter for each feature.
This will make unit testing easier.
And it will reduce the chance of breaking everything by changing some part of the code.
Reply
#3

Rate Limiting sample code:
https://codeigniter4.github.io/CodeIgnit...e-limiting
Reply
#4

Thanks @iRedds @kenjis
Reply




Theme © iAndrew 2016 - Forum software by © MyBB