This commit is contained in:
Daniël Klabbers 2021-10-30 13:01:51 +02:00
parent 12bc9d74e0
commit 6c045ab595
2 changed files with 15 additions and 11 deletions

View File

@ -34,6 +34,7 @@ app.initializers.add('blomstra-search', () => {
setting: 'blomstra-search.elastic-index', setting: 'blomstra-search.elastic-index',
label: app.translator.trans('blomstra-search.admin.elastic-index.label'), label: app.translator.trans('blomstra-search.admin.elastic-index.label'),
help: app.translator.trans('blomstra-search.admin.elastic-index.help'), help: app.translator.trans('blomstra-search.admin.elastic-index.help'),
default: 'flarum',
type: 'input' type: 'input'
}) })
.registerSetting({ .registerSetting({

View File

@ -7,10 +7,10 @@ use Blomstra\Search\Observe\SavingJob;
use Blomstra\Search\Seeders; use Blomstra\Search\Seeders;
use Elasticsearch\ClientBuilder; use Elasticsearch\ClientBuilder;
use Flarum\Foundation\AbstractServiceProvider; use Flarum\Foundation\AbstractServiceProvider;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container; use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Contracts\Queue\Queue; use Illuminate\Contracts\Queue\Queue;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@ -23,25 +23,28 @@ class Provider extends AbstractServiceProvider
Seeders\CommentSeeder::class, Seeders\CommentSeeder::class,
], 'blomstra.search.seeders'); ], 'blomstra.search.seeders');
$config = $this->container->make('flarum.config') ?? []; /** @var SettingsRepositoryInterface $settings */
$elastic = Arr::get($config, 'elastic', []); $settings = $this->container->make(SettingsRepositoryInterface::class);
$this->container->singleton('blomstra.search.elastic', function (Container $container) use ($elastic) { $this->container->singleton('blomstra.search.elastic', function (Container $container) use ($settings) {
$builder = ClientBuilder::create() $builder = ClientBuilder::create()
->setHosts([$elastic['endpoint']]) ->setHosts([$settings->get('blomstra-search.elastic-endpoint')])
->setLogger($container->make(LoggerInterface::class)); ->setLogger($container->make(LoggerInterface::class));
if ($elastic['api-key'] ?? false) { if ($settings->get('blomstra-search.elastic-username')) {
$builder->setApiKey($elastic['api-id'], $elastic['api-key']); $builder->setBasicAuthentication(
} $settings->get('blomstra-search.elastic-username'),
if ($elastic['username'] ?? false) { $settings->get('blomstra-search.elastic-password')
$builder->setBasicAuthentication($elastic['username'], $elastic['password']); );
} }
return $builder->build(); return $builder->build();
}); });
$this->container->instance('blomstra.search.elastic_index', Arr::get($elastic, 'index', 'flarum')); $this->container->instance(
'blomstra.search.elastic_index',
$settings->get('blomstra-search.elastic-index', 'flarum')
);
} }
public function boot() public function boot()