Skip to Content
🚀 Drubase One v1.1 - 基于 Drupal 11 的多租户后端即服务平台
Docs模块BaaS API

BaaS API 模块

API 网关和统一入口 - 提供 RESTful API、速率限制、文档生成。

📦 模块信息

  • 依赖: baas_auth, baas_entity
  • 位置: web/modules/custom/baas_api/

🏗️ 核心功能

API 网关

  • 统一的 API 入口点 /api/v1/{path}
  • 自动路由到相应模块
  • 请求/响应日志记录

速率限制

  • Token bucket 算法
  • Redis 存储
  • 用户级和 IP 级限制

API 文档

  • OpenAPI 3.0 规范
  • Swagger UI 集成
  • 自动生成文档

🔧 主要类和方法

GatewayController

API 网关控制器 - src/Controller/GatewayController.php

// 路由请求到相应模块 public function route(Request $request, string $path): Response // 处理认证 protected function authenticate(Request $request): ?int // 应用速率限制 protected function checkRateLimit(Request $request): void

RateLimitService

速率限制服务 - src/Service/RateLimitService.php

// 检查速率限制 public function checkLimit(string $key, int $limit, int $window): bool // 获取剩余配额 public function getRemainingRequests(string $key): int // 重置限制 public function resetLimit(string $key): void

ApiDocsController

API 文档控制器 - src/Controller/ApiDocsController.php

// 生成 OpenAPI 文档 public function getApiDocs(): JsonResponse // Swagger UI 页面 public function getApiDocsSwagger(): Response

📖 使用示例

配置速率限制

// 在 settings.php 中配置 $config['baas_api.settings'] = [ 'rate_limits' => [ 'user' => ['requests' => 60, 'window' => 60], 'ip' => ['requests' => 30, 'window' => 60], ], ];

使用 API 网关

# 所有请求通过网关 curl -X GET http://YOUR_HOST/api/v1/tenant_7375b0cd/projects/xxx/entities/users \ -H "Authorization: Bearer YOUR_TOKEN"

🔌 服务注册

baas_api.services.yml:

services: baas_api.rate_limiter: class: Drupal\baas_api\Service\RateLimitService arguments: - '@redis.factory' - '@config.factory' baas_api.gateway: class: Drupal\baas_api\Service\GatewayService arguments: - '@baas_auth.authentication' - '@baas_api.rate_limiter'

🔗 相关文档

Last updated on