diff --git a/src/Foundation/InstalledApp.php b/src/Foundation/InstalledApp.php index a8cabf9d4..9149f3806 100644 --- a/src/Foundation/InstalledApp.php +++ b/src/Foundation/InstalledApp.php @@ -18,9 +18,7 @@ use Flarum\Foundation\Console\CacheClearCommand; use Flarum\Foundation\Console\InfoCommand; use Flarum\Http\Middleware\DispatchRoute; use Flarum\Settings\SettingsRepositoryInterface; -use Zend\Diactoros\Response\HtmlResponse; use Zend\Stratigility\MiddlewarePipe; -use function Zend\Stratigility\middleware; use function Zend\Stratigility\path; class InstalledApp implements AppInterface @@ -47,7 +45,7 @@ class InstalledApp implements AppInterface public function getRequestHandler() { if ($this->inMaintenanceMode()) { - return $this->getMaintenanceHandler(); + return new MaintenanceModeHandler(); } elseif ($this->needsUpdate()) { return $this->getUpdaterHandler(); } @@ -66,24 +64,6 @@ class InstalledApp implements AppInterface 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 { $settings = $this->laravel->make(SettingsRepositoryInterface::class); diff --git a/src/Foundation/MaintenanceModeHandler.php b/src/Foundation/MaintenanceModeHandler.php new file mode 100644 index 000000000..554ac002a --- /dev/null +++ b/src/Foundation/MaintenanceModeHandler.php @@ -0,0 +1,58 @@ + + * + * 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'] + ); + } +}