Skip to content
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: 1 addition & 1 deletion .github/workflows/pint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
run: composer global require laravel/pint

- name: Run Pint
run: pint
run: pint --test
34 changes: 34 additions & 0 deletions app/Console/Commands/PerformSiteHealthCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Console\Commands;

use App\Jobs\CheckSiteHealth;
use App\Models\Site;
use Illuminate\Console\Command;

class PerformSiteHealthCheck extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:check-site-health {siteId}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Check site health for given site id';

/**
* Execute the console command.
*/
public function handle()
{
CheckSiteHealth::dispatchSync(
Site::findOrFail($this->input->getArgument('siteId'))
);
}
}
13 changes: 13 additions & 0 deletions app/Enum/HttpMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Enum;

enum HttpMethod
{
case POST;
case GET;
case PATCH;
case HEAD;
case PUT;
case DELETE;
}
8 changes: 8 additions & 0 deletions app/Enum/WebserviceEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Enum;

enum WebserviceEndpoint: string
{
case HEALTH_CHECK = "health.json";
}
36 changes: 36 additions & 0 deletions app/Http/Middleware/HorizonBasicAuthMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Http\Middleware;

use Closure;

class HorizonBasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$authenticationHasPassed = false;

if ($request->header('PHP_AUTH_USER', null) && $request->header('PHP_AUTH_PW', null)) {
$username = $request->header('PHP_AUTH_USER');
$password = $request->header('PHP_AUTH_PW');

if ($username === config('horizon.basic_auth.username')
&& $password === config('horizon.basic_auth.password')) {
$authenticationHasPassed = true;
}
}

if ($authenticationHasPassed === false) {
return response()->make('Invalid credentials.', 401, ['WWW-Authenticate' => 'Basic']);
}

return $next($request);
}
}
46 changes: 46 additions & 0 deletions app/Jobs/CheckSiteHealth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use App\Models\Site;
use App\Services\SiteConnectionService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class CheckSiteHealth implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(protected readonly Site $site)
{
}

/**
* Execute the job.
*/
public function handle(): void
{
/** @var SiteConnectionService $connection */
$connection = $this->site->connection;

$response = $connection->checkHealth();

$healthData = collect($response);

// Write updated data to DB
$this->site->update(
$healthData->only([
'php_version',
'db_type',
'db_version',
'cms_version',
'server_os'
])->toArray()
);
}
}
29 changes: 29 additions & 0 deletions app/Jobs/UpdateSite.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace App\Jobs;

use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;

class UpdateSite implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct()
{
//
}

/**
* Execute the job.
*/
public function handle(): void
{
//
}
}
19 changes: 15 additions & 4 deletions app/Models/Site.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Services\SiteConnectionService;
use Illuminate\Database\Eloquent\Model;

class Site extends Authenticatable
class Site extends Model
{
protected $fillable = [
'url',
Expand Down Expand Up @@ -34,4 +35,14 @@ protected function casts(): array
'update_major' => 'bool'
];
}

public function getUrlAttribute(string $value): string
{
return rtrim($value, "/") . "/";
}

public function getConnectionAttribute(): SiteConnectionService
{
return new SiteConnectionService($this->url, $this->key);
}
}
32 changes: 0 additions & 32 deletions app/Models/User.php

This file was deleted.

22 changes: 22 additions & 0 deletions app/Providers/HorizonServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Providers;

use Laravel\Horizon\Horizon;
use Laravel\Horizon\HorizonApplicationServiceProvider;

class HorizonServiceProvider extends HorizonApplicationServiceProvider
{
/**
* Overload authorization method from \Laravel\Horizon\HorizonApplicationServiceProvider
* to allow access to Horizon without having a logged in user.
*
* @return void
*/
protected function authorization()
{
Horizon::auth(function () {
return true;
});
}
}
106 changes: 106 additions & 0 deletions app/Services/SiteConnectionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

declare(strict_types=1);

namespace App\Services;

use App\Enum\HttpMethod;
use App\Enum\WebserviceEndpoint;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response;
use Illuminate\Support\Facades\App;
use Psr\Http\Message\RequestInterface;

class SiteConnectionService
{
public function __construct(protected readonly string $baseUrl, protected readonly string $key)
{
}

public function checkHealth(): array
{
$healthData = $this->performWebserviceRequest(
HttpMethod::GET,
WebserviceEndpoint::HEALTH_CHECK
);

// Perform a sanity check
if (empty($healthData['cms_version'])) {
throw new \Exception("Invalid health response content");
}

return $healthData;
}

public function performExtractionRequest(array $data): array
{
$request = new Request(
'POST',
$this->baseUrl . 'extract.php'
);

$data['password'] = $this->key;

return $this->performHttpRequest(
$request,
[
'form_params' => $data,
'timeout' => 300.0
]
);
}

protected function performWebserviceRequest(
HttpMethod $method,
WebserviceEndpoint $endpoint,
array $data = []
): array {
$request = new Request(
$method->name,
$this->baseUrl . $endpoint->value,
[
'Authorization' => 'JUpdate-Token ' . $this->key
]
);

return $this->performHttpRequest(
$request,
[
"json" => $data
]
);
}

protected function performHttpRequest(
RequestInterface $request,
array $options = []
): array {
/** @var Client $httpClient */
$httpClient = App::make(Client::class);

/** @var Response $response */
$response = $httpClient->send(
$request,
$options
);

// Validate response
if (!json_validate((string) $response->getBody())) {
throw new RequestException(
"Invalid JSON body",
$request,
$response
);
}

// Return decoded body
return json_decode(
(string) $response->getBody(),
true,
512,
JSON_THROW_ON_ERROR
);
}
}
4 changes: 3 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
$middleware->alias([
'horizonBasicAuth' => \App\Http\Middleware\HorizonBasicAuthMiddleware::class
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
Expand Down
2 changes: 2 additions & 0 deletions bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

return [
App\Providers\AppServiceProvider::class,
App\Providers\HorizonServiceProvider::class,
App\Providers\HttpclientServiceProvider::class,
];
Loading
Loading