Skip to content

Commit

Permalink
Merge pull request #217 from pieterclaerhout/meilisearch-auth-token-s…
Browse files Browse the repository at this point in the history
…upport

Added the option to specify a HTTP Authentication token for the Meilisearch check
  • Loading branch information
freekmurze committed Feb 5, 2024
2 parents b3c6865 + 274b5b9 commit 3db9166
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 13 additions & 0 deletions docs/available-checks/meilisearch.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,16 @@ Health::checks([
MeiliSearchCheck::new()->timeout(2),
]);
```

### Adding an authorization header

You can use `token()` to add an authorization header to the request.

```php
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\MeiliSearchCheck;

Health::checks([
MeiliSearchCheck::new()->token('auth-token'),
]);
```
13 changes: 12 additions & 1 deletion src/Checks/Checks/MeiliSearchCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class MeiliSearchCheck extends Check
protected int $timeout = 1;

protected string $url = 'http://127.0.0.1:7700/health';
protected ?string $token = null;

public function timeout(int $seconds): self
{
Expand All @@ -28,6 +29,13 @@ public function url(string $url): self
return $this;
}

public function token(string $token): self
{
$this->token = $token;

return $this;
}

public function getLabel(): string
{
return $this->getName();
Expand All @@ -36,7 +44,10 @@ public function getLabel(): string
public function run(): Result
{
try {
$response = Http::timeout($this->timeout)->asJson()->get($this->url);
$response = Http::timeout($this->timeout)
->when($this->token !== null, fn ($r) => $r->withToken($this->token))
->asJson()
->get($this->url);
} catch (Exception) {
return Result::make()
->failed()
Expand Down

0 comments on commit 3db9166

Please sign in to comment.