generalfocus/limitshield 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

generalfocus/limitshield

最新稳定版本:1.0.2

Composer 安装命令:

composer require generalfocus/limitshield

包简介

LimitShield is a middleware package designed to manage and enforce API rate limiting efficiently.

README 文档

README

LimitShield is a Laravel middleware package designed to efficiently manage and enforce API rate limiting. It offers customizable rate limits, supports IP-based and user-based limiting, integrates seamlessly with Redis for distributed rate limiting, and provides flexibility in crafting responses for rate limit breaches.

Features

  • Configurable rate limits
  • IP-based and user-based limiting
  • Redis integration for distributed rate limiting
  • Customizable responses for rate limit breaches

Installation

Requirements

  • PHP ^7.4|^8.0
  • Laravel ^8.0|^9.0|^10.0|^11.0
  • Redis (for distributed rate limiting)

Step-by-Step Installation

  1. Require the Package

    Add LimitShield to your Laravel project using Composer:

    composer require generalfocus/limitshield:*
  2. Publish Configuration

    Publish the configuration file to customize the settings:

    php artisan vendor:publish --provider="GeneralFocus\LimitShield\Providers\PackageServiceProvider" --tag="config"
  3. Configure Middleware

    Add the RateLimitMiddleware to your HTTP kernel. Edit app/Http/Kernel.php:

    protected $routeMiddleware = [
        // Other middleware
        'rate.limit' => \GeneralFocus\LimitShield\Http\Middleware\RateLimitMiddleware::class,
    ];

Usage

Applying Middleware to Routes

You can apply the rate limit middleware to specific routes or route groups:

use Illuminate\Support\Facades\Route;

Route::middleware('rate.limit')->group(function () {
    Route::get('/api/resource', 'ApiResourceController@index');
});

Alternatively, you can apply the middleware directly in route definitions:

use Illuminate\Support\Facades\Route;

Route::get('/api/resource', 'ApiResourceController@index')->middleware('rate.limit:limit=100,duration=60');

Configuring Limits

Edit the config/limitshield.php file to set your desired rate limits and other configurations:

return [
    'limits' => [
        'global' => [
            'enabled' => true,
            'max_requests' => 100,
            'decay_minutes' => 1,
        ],
        'ip' => [
            'enabled' => true,
            'max_requests' => 50,
            'decay_minutes' => 1,
        ],
        'user' => [
            'enabled' => true,
            'max_requests' => 200,
            'decay_minutes' => 1,
        ],
    ],
    'redis' => [
        'connection' => 'default',
    ],
    'response' => [
        'message' => 'Too many requests, please try again later.',
        'retry_after' => 'Retry-After',
    ],
];

Contributing

Guidelines

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Commit your changes (git commit -am 'Add some feature').
  4. Push to the branch (git push origin feature/YourFeature).
  5. Create a new Pull Request.

This README provides comprehensive instructions on installing, configuring, and using LimitShield in your Laravel applications. Feel free to contribute to its development and improve its functionality!

统计信息

  • 总下载量: 8
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-10