You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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;
#region << 版 本 注 释 >>
/*--------------------------------------------------------------------
* 版权所有 (c) 2024 WenJY 保留所有权利。
* CLR版本4.0.30319.42000
* 机器名称T14-GEN3-7895
* 命名空间SlnMesnac.TouchSocket
* 唯一标识4e47989b-9d43-426e-b67a-529de3b1b0e8
*
* 创建者WenJY
* 电子邮箱:
* 创建时间2024-09-04 10:51:29
* 版本V1.0.0
* 描述:
*
*--------------------------------------------------------------------
* 修改人:
* 时间:
* 修改说明:
*
* 版本V1.0.0
*--------------------------------------------------------------------*/
#endregion << 版 本 注 释 >>
namespace SlnMesnac.TouchSocket
{
public class WebApiServer
{
private ApiServer _apiServer;
public WebApiServer(ApiServer apiServer)
{
_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.Add<AuthenticationPlugin>();
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();
}
}
internal class AuthenticationPlugin : PluginBase, IHttpPlugin
{
public async Task OnHttpRequest(IHttpSocketClient client, HttpContextEventArgs e)
{
await e.InvokeNext();
}
}
}