Skip to content

Commit 3ae0ef3

Browse files
authored
Merge pull request #86 from mehrancodes/add-support-for-inertia-ssr
Added support for Inertia SSR
2 parents 77ac83a + 9b7b06b commit 3ae0ef3

File tree

9 files changed

+371
-223
lines changed

9 files changed

+371
-223
lines changed

app/Commands/ProvisionCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use App\Services\Forge\Pipeline\AnnounceSiteOnSlack;
1818
use App\Services\Forge\Pipeline\CreateDatabase;
1919
use App\Services\Forge\Pipeline\DeploySite;
20+
use App\Services\Forge\Pipeline\EnableInertiaSupport;
2021
use App\Services\Forge\Pipeline\EnableQuickDeploy;
2122
use App\Services\Forge\Pipeline\EnsureJobScheduled;
2223
use App\Services\Forge\Pipeline\FindServer;
@@ -60,6 +61,7 @@ public function handle(ForgeService $service): void
6061
EnsureJobScheduled::class,
6162
PutCommentOnPullRequest::class,
6263
AnnounceSiteOnSlack::class,
64+
EnableInertiaSupport::class,
6365
])
6466
->then(function () use ($service) {
6567
$this->success('Provisioning complete! Your environment is now set up and ready to use.');

app/Commands/TearDownCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use App\Services\Forge\Pipeline\FindServer;
1919
use App\Services\Forge\Pipeline\FindSiteOrFail;
2020
use App\Services\Forge\Pipeline\RemoveDatabaseUser;
21+
use App\Services\Forge\Pipeline\RemoveInertiaSupport;
2122
use App\Services\Forge\Pipeline\RemoveTaskScheduler;
2223
use App\Services\Forge\Pipeline\RunOptionalCommands;
2324
use App\Traits\Outputifier;
@@ -38,6 +39,7 @@ public function handle(ForgeService $service): void
3839
->through([
3940
FindServer::class,
4041
FindSiteOrFail::class,
42+
RemoveInertiaSupport::class,
4143
RunOptionalCommands::class,
4244
RemoveTaskScheduler::class,
4345
RemoveDatabaseUser::class,

app/Services/Forge/ForgeService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,9 @@ public function getSiteLink(): string
132132

133133
return ($this->site->isSecured ? 'https://' : 'http://').$this->site->name;
134134
}
135+
136+
public function siteDirectory(): string
137+
{
138+
return sprintf('/home/%s/%s', $this->site->username, $this->site->name);
139+
}
135140
}

app/Services/Forge/ForgeSetting.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,11 @@ class ForgeSetting
182182
*/
183183
public ?string $slackChannel;
184184

185+
/**
186+
* Enable support for Inertia SSR
187+
*/
188+
public bool $inertiaSsrEnabled;
189+
185190
/**
186191
* The validation rules.
187192
*/
@@ -215,6 +220,7 @@ class ForgeSetting
215220
'slack_announcement_enabled' => ['required', 'boolean'],
216221
'slack_bot_user_oauth_token' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
217222
'slack_channel' => ['exclude_if:slack_announcement_enabled,false', 'required', 'string'],
223+
'inertia_ssr_enabled' => ['required', 'boolean'],
218224
];
219225

220226
public function __construct()
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of Laravel Harbor.
7+
*
8+
* (c) Mehran Rasulian <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Services\Forge\Pipeline;
15+
16+
use App\Services\Forge\ForgeService;
17+
use App\Traits\Outputifier;
18+
use Closure;
19+
20+
class EnableInertiaSupport
21+
{
22+
use Outputifier;
23+
24+
public function __invoke(ForgeService $service, Closure $next)
25+
{
26+
if (! $service->setting->inertiaSsrEnabled) {
27+
return $next($service);
28+
}
29+
30+
if (! $service->siteNewlyMade) {
31+
return $next($service);
32+
}
33+
34+
$this->addDaemonToStartInertiaSsr($service);
35+
36+
$this->addCommandToStopInertiaOnReDeploy($service);
37+
38+
return $next($service);
39+
}
40+
41+
protected function addDaemonToStartInertiaSsr(ForgeService $service): void
42+
{
43+
$this->information('Create a daemon for Inertia.js SSR.');
44+
45+
$service->forge->createDaemon($service->server->id, [
46+
'command' => 'php artisan inertia:start-ssr',
47+
'user' => 'forge',
48+
'directory' => $service->siteDirectory()
49+
]);
50+
}
51+
52+
protected function addCommandToStopInertiaOnReDeploy(ForgeService $service): void
53+
{
54+
$script = $service->forge->siteDeploymentScript($service->server->id, $service->site->id);
55+
56+
if (!str_contains($script, $command = 'php artisan inertia:stop-ssr')) {
57+
$this->information('Including stop command for Inertia SSR in deploy script.');
58+
59+
$service->forge->updateSiteDeploymentScript(
60+
$service->server->id,
61+
$service->site->id,
62+
$script . "\n\n$command"
63+
);
64+
}
65+
}
66+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of Laravel Harbor.
7+
*
8+
* (c) Mehran Rasulian <[email protected]>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace App\Services\Forge\Pipeline;
15+
16+
use App\Services\Forge\ForgeService;
17+
use App\Traits\Outputifier;
18+
use Closure;
19+
use Illuminate\Support\Arr;
20+
use Laravel\Forge\Resources\Daemon;
21+
22+
class RemoveInertiaSupport
23+
{
24+
use Outputifier;
25+
26+
public function __invoke(ForgeService $service, Closure $next)
27+
{
28+
if (! $service->setting->inertiaSsrEnabled) {
29+
return $next($service);
30+
}
31+
32+
if ($daemon = $this->getInertiaDaemon($service)) {
33+
$this->information('Removing the daemon for Inertia.js SSR command.');
34+
35+
$service->forge->deleteDaemon($service->server->id, $daemon->id);
36+
}
37+
38+
return $next($service);
39+
}
40+
41+
protected function getInertiaDaemon(ForgeService $service): ?Daemon
42+
{
43+
$daemons = $service->forge->daemons($service->server->id);
44+
$command = 'php artisan inertia:start-ssr';
45+
46+
return Arr::first(
47+
$daemons,
48+
fn ($daemon) => $daemon->directory == $service->siteDirectory() && $daemon->command == $command
49+
);
50+
}
51+
}

app/Services/Forge/Pipeline/UpdateDeployScript.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public function __invoke(ForgeService $service, Closure $next)
2929

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

32-
$service->forge->put(sprintf('servers/%s/sites/%s/deployment/script', $service->server->id, $service->site->id), [
33-
'content' => $service->setting->deployScript,
34-
'auto_source' => $service->setting->autoSourceRequired,
35-
]);
32+
$service->forge->updateSiteDeploymentScript(
33+
$service->server->id,
34+
$service->site->id,
35+
$service->setting->deployScript,
36+
$service->setting->autoSourceRequired
37+
);
3638

3739
return $next($service);
3840
}

0 commit comments

Comments
 (0)