Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the possibility of serving assets from application's domain #61

Merged
merged 2 commits into from
Sep 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/vapor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,18 @@

'redirect_robots_txt' => true,

/*
|--------------------------------------------------------------------------
| Serve Assets
|--------------------------------------------------------------------------
|
| Here you can configure list of public assets that should be
| served directly from your application's "domain" instead
| of the S3 asset bucket or the CloudFront's asset URL.
|
*/
'serve_assets' => [
//
],

];
47 changes: 47 additions & 0 deletions src/Http/Middleware/ServeStaticAssets.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Laravel\Vapor\Http\Middleware;

use Closure;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;

class ServeStaticAssets
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null ...$guards
* @return mixed
*/
public function handle($request, Closure $next, ...$guards)
{
$response = $next($request);

if (isset($_ENV['VAPOR_SSM_PATH']) && $response->getStatusCode() === 404) {
$requestUri = $request->getRequestUri();

if (in_array(ltrim($requestUri, '/'), config('vapor.serve_assets', [])) !== false) {
$asset = null;

try {
$asset = (new Client)->get(asset($requestUri));
} catch (ClientException $e) {
report($e);
}

if ($asset && $asset->getStatusCode() === 200) {
$headers = collect($asset->getHeaders())
->only(['Content-Length', 'Content-Type'])
->all();

return response($asset->getBody())->withHeaders($headers);
}
}
}

return $response;
}
}
14 changes: 14 additions & 0 deletions src/VaporServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Laravel\Vapor;

use Illuminate\Contracts\Console\Kernel as ConsoleKernel;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Queue;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;
use Laravel\Vapor\Console\Commands\VaporWorkCommand;
use Laravel\Vapor\Http\Controllers\SignedStorageUrlController;
use Laravel\Vapor\Http\Middleware\ServeStaticAssets;
use Laravel\Vapor\Queue\VaporConnector;

class VaporServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -75,6 +77,7 @@ public function register()
$this->configureTrustedProxy();

$this->registerCommands();
$this->registerMiddleware();
}

/**
Expand Down Expand Up @@ -150,4 +153,15 @@ protected function registerCommands()

$this->commands(['command.vapor.work']);
}

/**
* Register the Vapor Http middleware.
*
* @return void
*/
protected function registerMiddleware()
{
$this->app[HttpKernel::class]
->pushMiddleware(ServeStaticAssets::class);
}
}