Skip to content

Commit

Permalink
feat: add support for basic authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfvandekrol committed Nov 17, 2023
1 parent 157abf7 commit 0127f99
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions config/elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

return [
'host' => env('ELASTICSEARCH_HOST', 'localhost:9200'),
'username' => env('ELASTICSEARCH_USERNAME', null),
'password' => env('ELASTICSEARCH_PASSWORD', null),
'index' => env('ELASTICSEARCH_INDEX', 'index'),
'enabled' => env('ELASTICSEARCH_ENABLED', false),
'models' => [
Expand Down
9 changes: 8 additions & 1 deletion src/LaravelElasticServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public function configurePackage(Package $package): void
public function packageRegistered(): void
{
$this->app->singleton(Client::class, function () {
return ClientBuilder::create()->setHosts([config('elasticsearch.host')])->build();
$builder = ClientBuilder::create()
->setHosts([config('elasticsearch.host')]);

if (config('elasticsearch.username') && config('elasticsearch.password')) {
$builder->setBasicAuthentication(config('elasticsearch.username'), config('elasticsearch.password'));
}

return $builder->build();
});
}
}

0 comments on commit 0127f99

Please sign in to comment.