#if NETCORE

using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using System.Net;

namespace Apewer.Web
{

    /// <summary>Kestrel 服务器。</summary>
    public class KestrelServer<TStartup> : KestrelServer where TStartup : AspNetCoreStartup
    {

        /// <summary>启动。</summary>
        public virtual void Run() => Run<TStartup>(false);

    }

    /// <summary>Kestrel 服务器。</summary>
    public class KestrelServer : AspNetCoreServer
    {

        /// <summary>配置 WebHost 构建程序。</summary>
        protected override void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureKestrel((context, options) =>
            {
                Configure(options);
            });
        }

        /// <summary>配置 Kestrel 服务器。</summary>
        private void Configure(KestrelServerOptions options)
        {
            // 配置端口和证书。
            var certificate = Certificate;
            if (certificate == null) options.Listen(IPAddress.Any, Port);
            else options.Listen(IPAddress.Any, Port, (listen) =>
            {
                listen.UseHttps(certificate, (ao) =>
                {
                    ao.CheckCertificateRevocation = false;
                    ao.ServerCertificate = certificate;
                    ao.AllowAnyClientCertificate();
                });
            });
        }

        /// <summary>启动。</summary>
        public override void Run<T>() => Run<T>(false);

    }

}

#endif