api-platform.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. use ApiPlatform\Metadata\Operation\DashPathSegmentNameGenerator;
  12. use ApiPlatform\Metadata\UrlGeneratorInterface;
  13. use Illuminate\Auth\Access\AuthorizationException;
  14. use Illuminate\Auth\AuthenticationException;
  15. use Webkul\BagistoApi\Exception\InvalidInputException;
  16. use Webkul\BagistoApi\Exception\ValidationException;
  17. use Webkul\BagistoApi\Serializer\OutputOnlySnakeToCamelNameConverter;
  18. return [
  19. 'title' => '',
  20. 'description' => '',
  21. 'version' => '1.0.0',
  22. 'show_webby' => true,
  23. 'routes' => [
  24. 'domain' => null,
  25. // Global middleware applied to every API Platform routes
  26. // HandleInvalidInputException: Catches validation errors and returns RFC 7807 format
  27. // VerifyStorefrontKey: Validates X-STOREFRONT-KEY header and rate limiting for shop APIs
  28. // BagistoApiDocumentationMiddleware: Handles custom /api index and documentation pages
  29. // ForceApiJson: Ensures API responses have JSON content-type
  30. // CacheResponse: Using custom ApiAwareResponseCache profile that:
  31. // - Excludes API routes from caching (APIs need fresh data)
  32. // - Caches shop pages for performance
  33. // - Only caches HTML, not JSON responses
  34. 'middleware' => [
  35. 'Webkul\BagistoApi\Http\Middleware\ParseMultipartFormData',
  36. 'Webkul\BagistoApi\Http\Middleware\NormalizeEmptyJsonBody',
  37. 'Webkul\BagistoApi\Http\Middleware\HandleInvalidInputException',
  38. 'Webkul\BagistoApi\Http\Middleware\SecurityHeaders',
  39. 'Webkul\BagistoApi\Http\Middleware\LogApiRequests',
  40. 'Webkul\BagistoApi\Http\Middleware\SetLocaleChannel',
  41. 'Webkul\BagistoApi\Http\Middleware\VerifyStorefrontKey',
  42. 'Webkul\BagistoApi\Http\Middleware\EnforceAdminApiAuth',
  43. 'Webkul\BagistoApi\Http\Middleware\ThrottleAdminApi',
  44. 'Webkul\BagistoApi\Http\Middleware\SetAdminApiAuditContext',
  45. 'Webkul\BagistoApi\Http\Middleware\BagistoApiDocumentationMiddleware',
  46. 'Webkul\BagistoApi\Http\Middleware\ForceApiJson',
  47. 'Webkul\BagistoApi\Http\Middleware\PaginationHeaders',
  48. 'Webkul\BagistoApi\Http\Middleware\SearchMetadataResponse',
  49. 'Webkul\BagistoApi\Http\Middleware\WrapApiResponse',
  50. 'Webkul\BagistoApi\Http\Middleware\PaginationHeaders',
  51. 'Spatie\ResponseCache\Middlewares\CacheResponse',
  52. ],
  53. ],
  54. 'resources' => [
  55. base_path('packages/Webkul/BagistoApi/src/Models/'),
  56. base_path('packages/Webkul/BagistoApi/src/Admin/Models/'),
  57. base_path('packages/Webkul/BagistoApi/src/Admin/Dto/'),
  58. base_path('packages/Webkul/BagistoApi/src/Dto/ProductDetail/'),
  59. base_path('packages/Webkul/BagistoApi/src/Dto/CustomerOrder/'),
  60. base_path('packages/Webkul/BagistoApi/src/Dto/ProductSearch/'),
  61. base_path('packages/Webkul/BagistoApi/src/Dto/CategoryProducts/'),
  62. // Longyi\FreeGift:GraphQL 赠品候选(giftCandidates)与
  63. // 领取 / 移除 / 恢复(createAddGift、createRemoveGift、createRestoreGifts)
  64. base_path('packages/Longyi/FreeGift/src/ApiResource/'),
  65. ],
  66. 'formats' => [
  67. 'json'=> ['application/json'],
  68. ],
  69. 'patch_formats' => [
  70. 'json' => ['application/merge-patch+json'],
  71. ],
  72. 'docs_formats' => [
  73. 'jsonopenapi' => ['application/vnd.openapi+json'],
  74. 'html' => ['text/html'],
  75. ],
  76. 'error_formats' => [
  77. 'jsonproblem' => ['application/problem+json'],
  78. ],
  79. 'defaults' => [
  80. 'pagination_enabled' => true,
  81. 'pagination_partial' => false,
  82. 'pagination_client_enabled' => false,
  83. 'pagination_client_items_per_page' => true,
  84. 'pagination_client_partial' => false,
  85. 'pagination_items_per_page' => 10,
  86. 'pagination_maximum_items_per_page' => 50,
  87. 'route_prefix' => '/api',
  88. 'middleware' => [],
  89. ],
  90. 'pagination' => [
  91. 'page_parameter_name' => 'page',
  92. 'enabled_parameter_name' => 'pagination',
  93. 'items_per_page_parameter_name' => 'per_page',
  94. 'partial_parameter_name' => 'partial',
  95. ],
  96. 'graphql' => [
  97. 'enabled' => true,
  98. 'nesting_separator' => '__',
  99. 'introspection' => ['enabled' => true],
  100. 'max_query_complexity' => 400,
  101. 'max_query_depth' => 20,
  102. 'graphiql' => [
  103. 'enabled' => true,
  104. 'default_query' => null,
  105. 'default_variables' => null,
  106. ],
  107. 'graphql_playground' => [
  108. 'enabled' => true,
  109. 'default_query' => null,
  110. 'default_variables' => null,
  111. ],
  112. // GraphQL middleware for authentication and rate limiting
  113. 'middleware' => [
  114. 'Webkul\BagistoApi\Http\Middleware\SetLocaleChannel',
  115. 'Webkul\BagistoApi\Http\Middleware\VerifyGraphQLStorefrontKey',
  116. 'Webkul\BagistoApi\Http\Middleware\SearchMetadataResponse',
  117. ],
  118. ],
  119. 'graphiql' => [
  120. 'enabled' => true,
  121. ],
  122. 'name_converter' => OutputOnlySnakeToCamelNameConverter::class,
  123. 'path_segment_name_generator' => DashPathSegmentNameGenerator::class,
  124. 'exception_to_status' => [
  125. AuthenticationException::class => 401,
  126. AuthorizationException::class => 403,
  127. ValidationException::class => 400,
  128. InvalidInputException::class => 400,
  129. ],
  130. 'swagger_ui' => [
  131. 'enabled' => true,
  132. 'apiKeys' => [
  133. 'api' => [
  134. 'name' => 'Authorization',
  135. 'type' => 'header',
  136. 'scheme' => 'bearer',
  137. ],
  138. ],
  139. ],
  140. 'url_generation_strategy' => UrlGeneratorInterface::ABS_PATH,
  141. 'serializer' => [
  142. 'hydra_prefix' => false,
  143. 'datetime_format' => 'Y-m-d\TH:i:sP',
  144. ],
  145. 'cache' => 'redis',
  146. 'schema_cache' => [
  147. 'enabled' => true,
  148. 'store' => 'redis',
  149. ],
  150. 'security' => [
  151. 'sanctum' => true,
  152. ],
  153. 'rate_limit' => [
  154. 'skip_localhost' => env('RATE_LIMIT_SKIP_LOCALHOST', true),
  155. 'auth' => env('RATE_LIMIT_AUTH', 5),
  156. 'admin' => env('RATE_LIMIT_ADMIN', 60),
  157. 'shop' => env('RATE_LIMIT_SHOP', 100),
  158. 'graphql' => env('RATE_LIMIT_GRAPHQL', 100),
  159. 'cache_driver' => env('RATE_LIMIT_CACHE', 'redis'),
  160. 'cache_prefix' => 'api:rate-limit:',
  161. ],
  162. 'security_headers' => [
  163. 'enabled' => true,
  164. 'force_https' => env('APP_FORCE_HTTPS', false),
  165. 'csp_header' => "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'",
  166. ],
  167. 'api_logging' => [
  168. 'enabled' => env('API_LOG_ENABLED', true),
  169. 'log_sensitive_data' => env('API_LOG_SENSITIVE_DATA', false),
  170. 'exclude_paths' => ['docs', 'graphiql', 'swagger-ui', 'docs.json'],
  171. 'channel' => 'api',
  172. 'async' => env('API_LOG_ASYNC', true),
  173. 'queue' => env('API_LOG_QUEUE', 'api-logs'),
  174. ],
  175. 'graphql_validation' => [
  176. 'max_depth' => env('GRAPHQL_MAX_DEPTH', 10),
  177. 'max_complexity' => env('GRAPHQL_MAX_COMPLEXITY', 300),
  178. ],
  179. 'request_limits' => [
  180. 'max_size_mb' => env('MAX_REQUEST_SIZE', 10),
  181. 'max_pagination_limit' => env('MAX_PAGINATION_LIMIT', 100),
  182. ],
  183. 'database' => [
  184. 'log_queries' => env('DB_QUERY_LOG_ENABLED', false),
  185. 'slow_query_threshold' => env('DB_SLOW_QUERY_THRESHOLD', 1000),
  186. ],
  187. 'caching' => [
  188. 'enable_security_cache' => env('API_SECURITY_CACHE', true),
  189. 'security_cache_ttl' => env('API_SECURITY_CACHE_TTL', 3600),
  190. 'enable_response_cache' => env('API_RESPONSE_CACHE', true),
  191. 'response_cache_ttl' => env('API_RESPONSE_CACHE_TTL', 3600),
  192. ],
  193. 'http_cache' => [
  194. 'etag' => true,
  195. 'max_age' => 3600,
  196. 'shared_max_age' => null,
  197. 'vary' => null,
  198. 'public' => true,
  199. 'stale_while_revalidate' => 30,
  200. 'stale_if_error' => null,
  201. 'invalidation' => [
  202. 'urls' => [],
  203. 'scoped_clients' => [],
  204. 'max_header_length' => 7500,
  205. 'request_options' => [],
  206. 'purger' => ApiPlatform\HttpCache\SouinPurger::class,
  207. ],
  208. ],
  209. 'key_rotation_policy' => [
  210. 'enabled' => true,
  211. 'expiration_months' => env('API_KEY_EXPIRATION_MONTHS', 12),
  212. 'transition_days' => env('API_KEY_TRANSITION_DAYS', 7),
  213. 'cleanup_days' => env('API_KEY_CLEANUP_DAYS', 90),
  214. 'cache_ttl' => env('API_KEY_CACHE_TTL', 3600),
  215. 'storefront_key_prefix' => env('STOREFRONT_KEY_PREFIX', 'pk_storefront_'),
  216. ],
  217. ];