using Microsoft.AspNetCore.Hosting.Server;

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using TouchSocket.Core;

using TouchSocket.Http;
using TouchSocket.Rpc;
using TouchSocket.Sockets;
using TouchSocket.WebApi.Swagger;

namespace SlnMesnac.TouchSocket
{
    public class WebApiServerApp
    {
        private ApiServer apiServer;

        public WebApiServerApp(ApiServer apiServer)
        {
            this.apiServer = apiServer;
        }
     
        public void Init()
        {
            try
            {
                var service = new HttpService();
                service.Setup(new TouchSocketConfig()
                   .SetListenIPHosts(7789)
                   .ConfigureContainer(a =>
                   {
                       a.AddRpcStore(store =>
                       {

                           store.RegisterServer<ApiServer>(apiServer);//注册服务
                       });
                       a.AddCors(corsOption =>
                       {
                           corsOption.Add("cors", corsBuilder =>
                           {
                               corsBuilder.AllowAnyMethod()
                                   .AllowAnyOrigin();
                           });
                       });
                       a.AddLogger(logger =>
                       {
                           logger.AddConsoleLogger();
                           logger.AddFileLogger();
                       });
                   })
                   .ConfigurePlugins(a =>
                   {
                       a.UseCheckClear();
                       a.UseWebApi()
                       .ConfigureConverter(converter =>
                       {
                           converter.AddJsonSerializerFormatter(new Newtonsoft.Json.JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });

                       });
                       a.UseSwagger();//使用Swagger页面
                                      //.UseLaunchBrowser();
                       a.UseDefaultHttpServicePlugin();
                   }));
                service.Start();
                Console.WriteLine("以下连接用于测试webApi");
                Console.WriteLine($"使用：http://127.0.0.1:7789/swagger/index.html");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            //Console.ReadLine();
        }
    }
  
}
