Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
grkngrn committed Jan 6, 2023
1 parent df3b989 commit 6c3b49f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 37 deletions.
35 changes: 0 additions & 35 deletions src/modules/Elsa.Elasticsearch/Common/Configuration.cs

This file was deleted.

6 changes: 4 additions & 2 deletions src/modules/Elsa.Elasticsearch/Common/ElasticFeatureBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using Elasticsearch.Net;
using Elsa.Elasticsearch.Extensions;
using Elsa.Elasticsearch.Options;
using Elsa.Elasticsearch.Services;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -26,8 +28,8 @@ public override void Apply()
private ConnectionSettings GetSettings()
{
return new ConnectionSettings(new Uri(Options.Endpoint))
.SetupAuthentication(Options)
.SetupMappingsAndIndices();
.ConfigureAuthentication(Options)
.ConfigureMapping();
}

protected void AddStore<TModel, TStore>() where TModel : class where TStore : class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using Elasticsearch.Net;
using Elsa.Elasticsearch.Options;
using Elsa.Elasticsearch.Services;
using Nest;

namespace Elsa.Elasticsearch.Extensions;

public static class ConnectionSettingsExtensions
{
public static ConnectionSettings ConfigureAuthentication(this ConnectionSettings settings, ElasticsearchOptions options)
{
if (!string.IsNullOrEmpty(options.ApiKey))
{
settings.ApiKeyAuthentication(new ApiKeyAuthenticationCredentials(options.ApiKey));
}
else if (!string.IsNullOrEmpty(options.Username) && !string.IsNullOrEmpty(options.Password))
{
settings.BasicAuthentication(options.Username, options.Password);
}

return settings;
}

public static ConnectionSettings ConfigureMapping(this ConnectionSettings settings)
{
var configs = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeof(IElasticConfiguration).IsAssignableFrom(p) && p.IsClass);

foreach (var config in configs)
{
var configInstance = (IElasticConfiguration)Activator.CreateInstance(config)!;
configInstance.Apply(settings);
}

return settings;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Elsa.Elasticsearch.Options;
using Elsa.Elasticsearch.Services;
using Elsa.Workflows.Management.Entities;
using Nest;

namespace Elsa.Elasticsearch.Modules.Management;

public class WorkflowInstanceConfiguration : IElasticConfiguration
{
public void Apply(ConnectionSettings connectionSettings)
{
connectionSettings.DefaultMappingFor<WorkflowInstance>(m =>
m.IndexName(ElasticsearchOptions.Indices[typeof(WorkflowInstance)]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using Elsa.Elasticsearch.Options;
using Elsa.Elasticsearch.Services;
using Elsa.Workflows.Runtime.Entities;
using Nest;

namespace Elsa.Elasticsearch.Modules.Runtime;

public class ExecutionLogConfiguration : IElasticConfiguration
{
public void Apply(ConnectionSettings connectionSettings)
{
connectionSettings.DefaultMappingFor<WorkflowExecutionLogRecord>(m =>
m.IndexName(ElasticsearchOptions.Indices[typeof(WorkflowExecutionLogRecord)]));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using Nest;

namespace Elsa.Elasticsearch.Services;

public interface IElasticConfiguration
{
void Apply(ConnectionSettings connectionSettings);
}

0 comments on commit 6c3b49f

Please sign in to comment.