mirror of
https://github.com/jikan-me/jikan-rest.git
synced 2025-02-20 11:23:35 +08:00
improved CORS support
This commit is contained in:
parent
8917cda52c
commit
e6a969402d
35
app/Http/Middleware/CorsMiddleware.php
Normal file
35
app/Http/Middleware/CorsMiddleware.php
Normal file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
use Laravel\Lumen\Http\ResponseFactory;
|
||||
|
||||
class CorsMiddleware
|
||||
{
|
||||
public function __construct(private readonly ResponseFactory $responseFactory)
|
||||
{
|
||||
}
|
||||
|
||||
public function handle(Request $request, \Closure $next): Response
|
||||
{
|
||||
if ($request->isMethod('OPTIONS')) {
|
||||
$headers = [
|
||||
'Access-Control-Allow-Origin' => '*',
|
||||
'Access-Control-Allow-Methods' => 'GET, OPTIONS',
|
||||
'Access-Control-Max-Age' => '86400',
|
||||
'Accept-Control-Allow-Headers' => 'Accept,Accept-Encoding,DNT,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range',
|
||||
'Content-Type' => 'text/plain',
|
||||
'Content-Length' => '0'
|
||||
];
|
||||
$response = $this->responseFactory->make("", 204, $headers);
|
||||
$response->setProtocolVersion("1.1");
|
||||
}
|
||||
else {
|
||||
$response = $next($request);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
@ -82,6 +82,10 @@ if (env('INSIGHTS', false)) {
|
||||
$globalMiddleware[] = \App\Http\Middleware\Insights::class;
|
||||
}
|
||||
|
||||
if (env('CORS_MIDDLEWARE', false)) {
|
||||
$globalMiddleware[] = \App\Http\Middleware\CorsMiddleware::class;
|
||||
}
|
||||
|
||||
$app->middleware($globalMiddleware);
|
||||
|
||||
$app->routeMiddleware([
|
||||
|
@ -28,8 +28,6 @@ ob_start("ob_gzhandler");
|
||||
|
||||
if (!env('APP_DEBUG')) {
|
||||
header("Content-Type: application/json");
|
||||
header("Access-Control-Allow-Origin: *");
|
||||
header("Access-Control-Allow-Methods: GET");
|
||||
}
|
||||
|
||||
$app->run();
|
||||
|
Loading…
x
Reference in New Issue
Block a user