fix: custom api key

This commit is contained in:
Damien Ostler 2024-10-01 23:03:55 -04:00
parent 4101b1bb93
commit 1d190eb8b6
3 changed files with 18 additions and 3 deletions

View File

@ -1,7 +1,9 @@
{
"Meili": {
"Port": 7700,
"UiEnabled" : true
"UiEnabled" : true,
"CustomApiKey": false,
"ApiKey": "YourOptionalApiKey"
},
"Logging": {
"LogLevel": {

View File

@ -6,6 +6,8 @@ public class MeiliSearchConfiguration
{
private readonly string _meiliPort = "Meili:Port";
private readonly string _meiliUiEnabled = "Meili:UiEnabled";
private readonly string _meiliEnableCustomApiKey = "Meili:CustomApiKey";
private readonly string _meiliApiKey = "Meili:ApiKey";
private readonly IConfiguration _configuration;
public MeiliSearchConfiguration(IConfiguration configuration)
{
@ -13,4 +15,6 @@ public class MeiliSearchConfiguration
}
public int MeiliPort => _configuration.GetValue<int>(_meiliPort);
public bool UiEnabled => _configuration.GetValue<bool>(_meiliUiEnabled);
public string ApiKey => _configuration.GetValue<string>(_meiliApiKey) ?? "";
public bool EnableCustomApiKey => _configuration.GetValue<bool>(_meiliEnableCustomApiKey);
}

View File

@ -33,7 +33,12 @@ public class MeiliSearchService:IMeiliSearchService
_meiliSearchConfiguration = meiliSearchConfiguration;
var binaryName = GetBinaryName();
MakeExecutable(Path.Combine(AppContext.BaseDirectory, binaryName));
var apiKey = ApiKeyGenerator.GenerateApiKey();
string apiKey = "";
if (_meiliSearchConfiguration.EnableCustomApiKey)
apiKey = _meiliSearchConfiguration.ApiKey;
else
apiKey = ApiKeyGenerator.GenerateApiKey();
_sdkHttpClient = httpClient;
_sdkHttpClient.BaseAddress = new Uri("http://localhost:"+meiliSearchConfiguration.MeiliPort);
Sdk = new MeilisearchClient(httpClient, apiKey);
@ -190,7 +195,11 @@ public class MeiliSearchService:IMeiliSearchService
public void RefreshApiKey()
{
_logger.LogInformation("Refreshing API key.");
var apiKey = ApiKeyGenerator.GenerateApiKey();
string apiKey = "";
if (_meiliSearchConfiguration.EnableCustomApiKey)
apiKey = _meiliSearchConfiguration.ApiKey;
else
apiKey = ApiKeyGenerator.GenerateApiKey();
Sdk = new MeilisearchClient(_sdkHttpClient, apiKey);
_logger.LogInformation("New API key generated.");
Restart();