♻️ Still don't know what I am doing
This commit is contained in:
47
DysonNetwork.Shared/Registry/ServiceHelper.cs
Normal file
47
DysonNetwork.Shared/Registry/ServiceHelper.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using dotnet_etcd.interfaces;
|
||||
using DysonNetwork.Shared.Proto;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace DysonNetwork.Shared.Registry;
|
||||
|
||||
public static class ServiceHelper
|
||||
{
|
||||
public static IServiceCollection AddPusherService(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<PusherService.PusherServiceClient>(sp =>
|
||||
{
|
||||
var etcdClient = sp.GetRequiredService<IEtcdClient>();
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
var clientCertPath = config["Service:ClientCert"];
|
||||
var clientKeyPath = config["Service:ClientKey"];
|
||||
var clientCertPassword = config["Service:CertPassword"];
|
||||
|
||||
return GrpcClientHelper
|
||||
.CreatePusherServiceClient(etcdClient, clientCertPath, clientKeyPath, clientCertPassword)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAccountService(this IServiceCollection services)
|
||||
{
|
||||
services.AddSingleton<AccountService.AccountServiceClient>(sp =>
|
||||
{
|
||||
var etcdClient = sp.GetRequiredService<IEtcdClient>();
|
||||
var config = sp.GetRequiredService<IConfiguration>();
|
||||
var clientCertPath = config["Service:ClientCert"];
|
||||
var clientKeyPath = config["Service:ClientKey"];
|
||||
var clientCertPassword = config["Service:CertPassword"];
|
||||
|
||||
return GrpcClientHelper
|
||||
.CreateAccountServiceClient(etcdClient, clientCertPath, clientKeyPath, clientCertPassword)
|
||||
.GetAwaiter()
|
||||
.GetResult();
|
||||
});
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
@@ -8,16 +8,20 @@ namespace DysonNetwork.Shared.Registry;
|
||||
|
||||
public class ServiceRegistry(IEtcdClient etcd, ILogger<ServiceRegistry> logger)
|
||||
{
|
||||
public async Task RegisterService(string serviceName, string serviceUrl, long leaseTtlSeconds = 60, CancellationToken cancellationToken = default)
|
||||
public async Task RegisterService(string serviceName, string serviceUrl, long leaseTtlSeconds = 60,
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
var key = $"/services/{serviceName}";
|
||||
var leaseResponse = await etcd.LeaseGrantAsync(new LeaseGrantRequest { TTL = leaseTtlSeconds });
|
||||
var leaseResponse = await etcd.LeaseGrantAsync(
|
||||
new LeaseGrantRequest { TTL = leaseTtlSeconds },
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
await etcd.PutAsync(new PutRequest
|
||||
{
|
||||
Key = ByteString.CopyFrom(key, Encoding.UTF8),
|
||||
Value = ByteString.CopyFrom(serviceUrl, Encoding.UTF8),
|
||||
Lease = leaseResponse.ID
|
||||
});
|
||||
}, cancellationToken: cancellationToken);
|
||||
|
||||
_ = Task.Run(async () =>
|
||||
{
|
||||
|
Reference in New Issue
Block a user