using System.Net.Http;
using Grpc.Net.Client;
using YGNT.Infrastructure.Helper;

namespace YGNT.Infrastructure.Grpc
{
    public class GrpcHost
    {
        /// <summary>
        /// 用户Grpc通道
        /// </summary>
        public static GrpcChannel userChannel;
        /// <summary>
        /// 资源Grpc通道
        /// </summary>
        public static GrpcChannel resourcesChannel;

        /// <summary>
        /// 链接到grpc
        /// </summary>
        public static void Run()
        {
            //if (channel != null)
            //    return;

            //X509Certificate x509Certificate = new X509Certificate(Path.Combine(FilePath.AppPath, @"Grpc\server.crt"));

            var httpClientHandler = new HttpClientHandler();
            //返回“true”以允许不可信/无效的证书
            httpClientHandler.ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator;
            var httpClient = new HttpClient(httpClientHandler);

            var url = AppSettings.App("GrpcStrings", "UserUrl");
            userChannel = GrpcChannel.ForAddress(url, new GrpcChannelOptions { HttpClient = httpClient });

            url = AppSettings.App("GrpcStrings", "ResourcesUrl");
            resourcesChannel = GrpcChannel.ForAddress(url, new GrpcChannelOptions { HttpClient = httpClient });
        }

    }
}
