diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index b5021079ec..20aba4d0aa 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -53,6 +53,7 @@ class Kernel extends HttpKernel ], 'api' => [ + \App\Http\Middleware\EnforceJson::class, \Illuminate\Routing\Middleware\SubstituteBindings::class, 'authenticate:token', ], diff --git a/app/Http/Middleware/EnforceJson.php b/app/Http/Middleware/EnforceJson.php new file mode 100644 index 0000000000..289c7b3282 --- /dev/null +++ b/app/Http/Middleware/EnforceJson.php @@ -0,0 +1,45 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Tony Murray + * @author Tony Murray + */ + +namespace App\Http\Middleware; + +use Closure; + +class EnforceJson +{ + /** + * Enforce json + * + * @param \Illuminate\Http\Request $request + * @param \Closure $next + * @return mixed + */ + public function handle($request, Closure $next) + { + $request->headers->set('Accept', 'application/json'); + + return $next($request); + } +} diff --git a/html/api_v0.php b/html/api_v0.php index 64abeb8130..0fbc0c43be 100644 --- a/html/api_v0.php +++ b/html/api_v0.php @@ -1,10 +1,24 @@ - */ +use Illuminate\Contracts\Http\Kernel; +use Illuminate\Http\Request; + +define('LARAVEL_START', microtime(true)); + +/* +|-------------------------------------------------------------------------- +| Check If Application Is Under Maintenance +|-------------------------------------------------------------------------- +| +| If the application is maintenance / demo mode via the "down" command we +| will require this file so that any prerendered template can be shown +| instead of starting the framework, which could cause an exception. +| +*/ + +if (file_exists(__DIR__ . '/../storage/framework/maintenance.php')) { + require __DIR__ . '/../storage/framework/maintenance.php'; +} /* |-------------------------------------------------------------------------- @@ -12,9 +26,8 @@ |-------------------------------------------------------------------------- | | Composer provides a convenient, automatically generated class loader for -| our application. We just need to utilize it! We'll simply require it -| into the script here so that we don't have to worry about manual -| loading any of our classes later on. It feels great to relax. +| this application. We just need to utilize it! We'll simply require it +| into the script here so we don't need to manually load our classes. | */ @@ -22,38 +35,21 @@ require __DIR__ . '/../vendor/autoload.php'; /* |-------------------------------------------------------------------------- -| Turn On The Lights +| Run The Application |-------------------------------------------------------------------------- | -| We need to illuminate PHP development, so let us turn on the lights. -| This bootstraps the framework and gets it ready for use, then it -| will load up this application so that we can run it and send -| the responses back to the browser and delight our users. +| Once we have the application, we can handle the incoming request using +| the application's HTTP kernel. Then, we will send the response back +| to this client's browser, allowing them to enjoy our application. | */ $app = require_once __DIR__ . '/../bootstrap/app.php'; -/* -|-------------------------------------------------------------------------- -| Run The Application -|-------------------------------------------------------------------------- -| -| Once we have the application, we can handle the incoming request -| through the kernel, and send the associated response back to -| the client's browser allowing them to enjoy the creative -| and wonderful application we have prepared for them. -| -*/ +$kernel = $app->make(Kernel::class); -$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class); - -$_SERVER['HTTP_ACCEPT'] = 'application/json'; // force json accepted - -$response = $kernel->handle( - $request = Illuminate\Http\Request::capture() -); - -$response->send(); +$response = tap($kernel->handle( + $request = Request::capture() +))->send(); $kernel->terminate($request, $response);