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

Add support for Inertia.js SSR #86

Merged
merged 7 commits into from
Mar 17, 2024
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
2 changes: 2 additions & 0 deletions app/Commands/ProvisionCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use App\Services\Forge\Pipeline\AnnounceSiteOnSlack;
use App\Services\Forge\Pipeline\CreateDatabase;
use App\Services\Forge\Pipeline\DeploySite;
use App\Services\Forge\Pipeline\EnableInertiaSupport;
use App\Services\Forge\Pipeline\EnableQuickDeploy;
use App\Services\Forge\Pipeline\EnsureJobScheduled;
use App\Services\Forge\Pipeline\FindServer;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function handle(ForgeService $service): void
EnsureJobScheduled::class,
PutCommentOnPullRequest::class,
AnnounceSiteOnSlack::class,
EnableInertiaSupport::class,
])
->then(function () use ($service) {
$this->success('Provisioning complete! Your environment is now set up and ready to use.');
Expand Down
2 changes: 2 additions & 0 deletions app/Commands/TearDownCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Services\Forge\Pipeline\FindServer;
use App\Services\Forge\Pipeline\FindSiteOrFail;
use App\Services\Forge\Pipeline\RemoveDatabaseUser;
use App\Services\Forge\Pipeline\RemoveInertiaSupport;
use App\Services\Forge\Pipeline\RemoveTaskScheduler;
use App\Services\Forge\Pipeline\RunOptionalCommands;
use App\Traits\Outputifier;
Expand All @@ -38,6 +39,7 @@ public function handle(ForgeService $service): void
->through([
FindServer::class,
FindSiteOrFail::class,
RemoveInertiaSupport::class,
RunOptionalCommands::class,
RemoveTaskScheduler::class,
RemoveDatabaseUser::class,
Expand Down
5 changes: 5 additions & 0 deletions app/Services/Forge/ForgeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,9 @@ public function getSiteLink(): string

return ($this->site->isSecured ? 'https://' : 'http://').$this->site->name;
}

public function siteDirectory(): string
{
return sprintf('/home/%s/%s', $this->site->username, $this->site->name);
}
}
6 changes: 6 additions & 0 deletions app/Services/Forge/ForgeSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ class ForgeSetting
*/
public ?string $slackChannel;

/**
* Enable support for Inertia SSR
*/
public bool $inertiaSsrEnabled;

/**
* The validation rules.
*/
Expand Down Expand Up @@ -209,6 +214,7 @@ class ForgeSetting
'slack_announcement_enabled' => ['required', 'boolean'],
'slack_bot_user_oauth_token' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'slack_channel' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
'inertia_ssr_enabled' => ['required', 'boolean'],
];

public function __construct()
Expand Down
66 changes: 66 additions & 0 deletions app/Services/Forge/Pipeline/EnableInertiaSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

/**
* This file is part of Laravel Harbor.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge\Pipeline;

use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;

class EnableInertiaSupport
{
use Outputifier;

public function __invoke(ForgeService $service, Closure $next)
{
if (! $service->setting->inertiaSsrEnabled) {
return $next($service);
}

if (! $service->siteNewlyMade) {
return $next($service);
}

$this->addDaemonToStartInertiaSsr($service);

$this->addCommandToStopInertiaOnReDeploy($service);

return $next($service);
}

protected function addDaemonToStartInertiaSsr(ForgeService $service): void
{
$this->information('Create a daemon for Inertia.js SSR.');

$service->forge->createDaemon($service->server->id, [
'command' => 'php artisan inertia:start-ssr',
'user' => 'forge',
'directory' => $service->siteDirectory()
]);
}

protected function addCommandToStopInertiaOnReDeploy(ForgeService $service): void
{
$script = $service->forge->siteDeploymentScript($service->server->id, $service->site->id);

if (!str_contains($script, $command = 'php artisan inertia:stop-ssr')) {
$this->information('Including stop command for Inertia SSR in deploy script.');

$service->forge->updateSiteDeploymentScript(
$service->server->id,
$service->site->id,
$script . "\n\n$command"
);
}
}
}
51 changes: 51 additions & 0 deletions app/Services/Forge/Pipeline/RemoveInertiaSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

/**
* This file is part of Laravel Harbor.
*
* (c) Mehran Rasulian <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace App\Services\Forge\Pipeline;

use App\Services\Forge\ForgeService;
use App\Traits\Outputifier;
use Closure;
use Illuminate\Support\Arr;
use Laravel\Forge\Resources\Daemon;

class RemoveInertiaSupport
{
use Outputifier;

public function __invoke(ForgeService $service, Closure $next)
{
if (! $service->setting->inertiaSsrEnabled) {
return $next($service);
}

if ($daemon = $this->getInertiaDaemon($service)) {
$this->information('Removing the daemon for Inertia.js SSR command.');

$service->forge->deleteDaemon($service->server->id, $daemon->id);
}

return $next($service);
}

protected function getInertiaDaemon(ForgeService $service): ?Daemon
{
$daemons = $service->forge->daemons($service->server->id);
$command = 'php artisan inertia:start-ssr';

return Arr::first(
$daemons,
fn ($daemon) => $daemon->directory == $service->siteDirectory() && $daemon->command == $command
);
}
}
10 changes: 6 additions & 4 deletions app/Services/Forge/Pipeline/UpdateDeployScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ public function __invoke(ForgeService $service, Closure $next)

$this->information('Updating deployment script.');

$service->forge->put(sprintf('servers/%s/sites/%s/deployment/script', $service->server->id, $service->site->id), [
'content' => $service->setting->deployScript,
'auto_source' => $service->setting->autoSourceRequired,
]);
$service->forge->updateSiteDeploymentScript(
$service->server->id,
$service->site->id,
$service->setting->deployScript,
$service->setting->autoSourceRequired
);

return $next($service);
}
Expand Down
Loading
Loading