mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 19:33:11 +08:00
Implement request handler for maintenance mode
This commit is contained in:
parent
554322f0b8
commit
02f27b0fa1
@ -18,9 +18,7 @@ use Flarum\Foundation\Console\CacheClearCommand;
|
|||||||
use Flarum\Foundation\Console\InfoCommand;
|
use Flarum\Foundation\Console\InfoCommand;
|
||||||
use Flarum\Http\Middleware\DispatchRoute;
|
use Flarum\Http\Middleware\DispatchRoute;
|
||||||
use Flarum\Settings\SettingsRepositoryInterface;
|
use Flarum\Settings\SettingsRepositoryInterface;
|
||||||
use Zend\Diactoros\Response\HtmlResponse;
|
|
||||||
use Zend\Stratigility\MiddlewarePipe;
|
use Zend\Stratigility\MiddlewarePipe;
|
||||||
use function Zend\Stratigility\middleware;
|
|
||||||
use function Zend\Stratigility\path;
|
use function Zend\Stratigility\path;
|
||||||
|
|
||||||
class InstalledApp implements AppInterface
|
class InstalledApp implements AppInterface
|
||||||
@ -47,7 +45,7 @@ class InstalledApp implements AppInterface
|
|||||||
public function getRequestHandler()
|
public function getRequestHandler()
|
||||||
{
|
{
|
||||||
if ($this->inMaintenanceMode()) {
|
if ($this->inMaintenanceMode()) {
|
||||||
return $this->getMaintenanceHandler();
|
return new MaintenanceModeHandler();
|
||||||
} elseif ($this->needsUpdate()) {
|
} elseif ($this->needsUpdate()) {
|
||||||
return $this->getUpdaterHandler();
|
return $this->getUpdaterHandler();
|
||||||
}
|
}
|
||||||
@ -66,24 +64,6 @@ class InstalledApp implements AppInterface
|
|||||||
return $this->config['offline'] ?? false;
|
return $this->config['offline'] ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \Psr\Http\Server\RequestHandlerInterface
|
|
||||||
*/
|
|
||||||
private function getMaintenanceHandler()
|
|
||||||
{
|
|
||||||
$pipe = new MiddlewarePipe;
|
|
||||||
|
|
||||||
$pipe->pipe(middleware(function () {
|
|
||||||
// FIXME: Fix path to 503.html
|
|
||||||
// TODO: FOR API render JSON-API error document for HTTP 503
|
|
||||||
return new HtmlResponse(
|
|
||||||
file_get_contents(__DIR__.'/../../503.html'), 503
|
|
||||||
);
|
|
||||||
}));
|
|
||||||
|
|
||||||
return $pipe;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function needsUpdate(): bool
|
private function needsUpdate(): bool
|
||||||
{
|
{
|
||||||
$settings = $this->laravel->make(SettingsRepositoryInterface::class);
|
$settings = $this->laravel->make(SettingsRepositoryInterface::class);
|
||||||
|
58
framework/core/src/Foundation/MaintenanceModeHandler.php
Normal file
58
framework/core/src/Foundation/MaintenanceModeHandler.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of Flarum.
|
||||||
|
*
|
||||||
|
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Flarum\Foundation;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
use Tobscure\JsonApi\Document;
|
||||||
|
use Zend\Diactoros\Response\HtmlResponse;
|
||||||
|
use Zend\Diactoros\Response\JsonResponse;
|
||||||
|
|
||||||
|
class MaintenanceModeHandler implements RequestHandlerInterface
|
||||||
|
{
|
||||||
|
const MESSAGE = 'Currently down for maintenance. Please come back later.';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handle the request and return a response.
|
||||||
|
*/
|
||||||
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
|
{
|
||||||
|
// Special handling for API requests: they get a proper API response
|
||||||
|
if ($this->isApiRequest($request)) {
|
||||||
|
return $this->apiResponse();
|
||||||
|
}
|
||||||
|
|
||||||
|
// By default, return a simple text message.
|
||||||
|
return new HtmlResponse(self::MESSAGE, 503);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function isApiRequest(ServerRequestInterface $request): bool
|
||||||
|
{
|
||||||
|
return str_contains(
|
||||||
|
$request->getHeaderLine('Accept'),
|
||||||
|
'application/vnd.api+json'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function apiResponse(): ResponseInterface
|
||||||
|
{
|
||||||
|
return new JsonResponse(
|
||||||
|
(new Document)->setErrors([
|
||||||
|
'status' => '503',
|
||||||
|
'title' => self::MESSAGE
|
||||||
|
]),
|
||||||
|
503,
|
||||||
|
['Content-Type' => 'application/vnd.api+json']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user