Drawaes / CondenserDotNet

API Condenser / Reverse Proxy using Kestrel and Consul, Including light weight consul lib
MIT License
166 stars 31 forks source link

AddStaticKeyPathAsync() method does not return data #121

Open exytab opened 4 years ago

exytab commented 4 years ago

I think, it happed because AddStaticKeyPathAsync() method uses ConsulConfigSource.FormValidKey() which adds / character to the end.
In consul documentation path looks like /kv/:key. https://demo.consul.io/v1/kv/hi - works https://demo.consul.io/v1/kv/hi/ - not:(

Drawaes commented 4 years ago

Ahh you want to add a single key in that case?

exytab commented 4 years ago

yes, if possible

Drawaes commented 4 years ago

Okay that isn't how I have seen it used before but is a reasonable request I will look at the best way to support that later today.

exytab commented 4 years ago

Thank you. And please 4.x package will be updated

Drawaes commented 4 years ago

resolved by #123 also put into 4.1.14 and published for that. You can now do AddStaticKeyPathAsync( keyName, isSingleKey)

exytab commented 4 years ago

Thank you, but now all keys from consul start with : symbol.

exytab commented 4 years ago

I think the problem is in JsonKeyValueParser() method when you create Key: key.Key + ConfigurationPath.KeyDelimiter + kv.Key.

Maybe, simple fix will be something like this:

private async Task<Dictionary<string, string>> BuildDictionaryAsync(string keyPath,
    HttpResponseMessage response)
{
    var content = await response.Content.ReadAsStringAsync();
    var keys = JsonConvert.DeserializeObject<KeyValue[]>(content);

    var parsedKeys = keys.SelectMany(k => _parser.Parse(k)).Distinct(new KeyValueComparer(keyPath, ConsulPath[0], CorePath));

    var dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    foreach (var kv in parsedKeys)
    {
-        var key = kv.Key.Substring(keyPath.Length).Replace(ConsulPath[0], CorePath);
+        var key = kv.Key.Substring(keyPath.Length + Microsoft.Extensions.Configuration.ConfigurationPath.KeyDelimiter.Length).Replace(ConsulPath[0], CorePath);
        var value = kv.IsDerivedKey ? kv.Value : kv.Value == null ? null : kv.ValueFromBase64();
        dictionary[key] = value;
    }
    return dictionary;
}
Drawaes commented 4 years ago

I can hopefully take a look at this tonight.