151 lines
5.5 KiB
C#
151 lines
5.5 KiB
C#
|
|
|||
|
|
|||
|
using ZhiYi.Core.Application.WebSocket;
|
|||
|
|
|||
|
var builder = WebApplication.CreateBuilder(args);
|
|||
|
builder.Services.AddControllers(option =>
|
|||
|
{
|
|||
|
option.Filters.Add<CustomExceptionFilterAttribute>();
|
|||
|
})
|
|||
|
.AddJsonOptions(jsonOption =>
|
|||
|
{
|
|||
|
jsonOption.JsonSerializerOptions.PropertyNamingPolicy = null; //<2F><><EFBFBD><EFBFBD>С<EFBFBD>շ壬ʵ<E5A3AC>巵<EFBFBD><E5B7B5>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ԭ<EFBFBD>д<EFBFBD>Сд<D0A1><D0B4>ʽ
|
|||
|
});
|
|||
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|||
|
//grpc
|
|||
|
builder.Services.AddGrpc();
|
|||
|
builder.Services.AddAutoMapper(Assembly.GetExecutingAssembly());
|
|||
|
builder.Services.AddEndpointsApiExplorer();
|
|||
|
var startAssembly = Assembly.GetExecutingAssembly();
|
|||
|
builder.Services.AddSwaggerGen(c =>
|
|||
|
{
|
|||
|
var startAssemblyName = startAssembly.GetName().Name;
|
|||
|
if (string.IsNullOrEmpty(startAssemblyName))
|
|||
|
throw new NullReferenceException(nameof(startAssemblyName));
|
|||
|
|
|||
|
var lastName = startAssemblyName.Split('.').Last();
|
|||
|
var apiLayerXmlFilePath = Path.Combine(AppContext.BaseDirectory, $"{startAssemblyName}.xml");
|
|||
|
var applicationContractsLayerXmlFilePath = Path.Combine(AppContext.BaseDirectory, $"{startAssemblyName.Replace($".{lastName}", ".Application.Contracts")}.xml");
|
|||
|
var applicationLayerXmlFilePath = Path.Combine(AppContext.BaseDirectory, $"{startAssemblyName.Replace($".{lastName}", ".Application")}.xml");
|
|||
|
c.IncludeXmlComments(apiLayerXmlFilePath, true);
|
|||
|
if (File.Exists(applicationContractsLayerXmlFilePath))
|
|||
|
{
|
|||
|
c.IncludeXmlComments(applicationContractsLayerXmlFilePath, true);
|
|||
|
}
|
|||
|
else if (File.Exists(applicationLayerXmlFilePath))
|
|||
|
{
|
|||
|
c.IncludeXmlComments(applicationLayerXmlFilePath, true);
|
|||
|
}
|
|||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "API", Version = "v1" });
|
|||
|
|
|||
|
// <20><><EFBFBD><EFBFBD> JWT <20><>֤֧<D6A4><D6A7>
|
|||
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|||
|
{
|
|||
|
Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
|
|||
|
Name = "Authorization",
|
|||
|
In = ParameterLocation.Header,
|
|||
|
Type = SecuritySchemeType.ApiKey,
|
|||
|
Scheme = "Bearer"
|
|||
|
});
|
|||
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|||
|
{
|
|||
|
{
|
|||
|
new OpenApiSecurityScheme
|
|||
|
{
|
|||
|
Reference = new OpenApiReference
|
|||
|
{
|
|||
|
Type = ReferenceType.SecurityScheme,
|
|||
|
Id = "Bearer"
|
|||
|
}
|
|||
|
},
|
|||
|
Array.Empty<string>()
|
|||
|
}
|
|||
|
});
|
|||
|
});
|
|||
|
// ע<><D7A2> Redis <20><><EFBFBD><EFBFBD>
|
|||
|
var constr = builder.Configuration["ConnectionStrings:Redis"];
|
|||
|
builder.Services.AddSingleton<IConnectionMultiplexer>(provider =>
|
|||
|
{
|
|||
|
var configuration = ConfigurationOptions.Parse(constr);
|
|||
|
return ConnectionMultiplexer.Connect(configuration);
|
|||
|
});
|
|||
|
builder.Services.AddSingleton<IUserAppService, UserAppService>();
|
|||
|
builder.Services.AddSingleton<IServerAppService, ServerAppService>();
|
|||
|
builder.Services.AddSingleton<IGroupAppService, GroupAppService>();
|
|||
|
builder.Services.AddSingleton<ICaptchaAppService, CaptchaAppService>();
|
|||
|
builder.Services.AddSingleton<IConnectionClientManagerService, ConnectionClientManagerService>();
|
|||
|
builder.Services.AddScoped<CaptchaService>();
|
|||
|
builder.Services.AddScoped<TokenService>();
|
|||
|
builder.Configuration.AddJsonFile($"{AppContext.BaseDirectory}/appsettings.{builder.Environment.EnvironmentName}.json", true, true);
|
|||
|
/*builder.Services.AddAutoMapper(config =>
|
|||
|
{
|
|||
|
config.AddProfile(new MappingProfile()); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>ͨ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD>÷<EFBFBD><C3B7><EFBFBD>
|
|||
|
}, typeof(YourApplicationNamespace.MappingProfile).Assembly);*/
|
|||
|
//<2F><><EFBFBD><EFBFBD>
|
|||
|
builder.Services.AddCors(options =>
|
|||
|
{
|
|||
|
options.AddPolicy("AllowSpecificOrigin",
|
|||
|
builder => builder.WithOrigins("http://localhost:8080") // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ
|
|||
|
.AllowAnyMethod() // <20><><EFBFBD><EFBFBD><EFBFBD>ķ<EFBFBD><C4B7><EFBFBD>
|
|||
|
.AllowAnyHeader() // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>
|
|||
|
.AllowCredentials()); // <20><><EFBFBD><EFBFBD>Я<EFBFBD><D0AF>ƾ֤
|
|||
|
});
|
|||
|
//<2F><>Ȩ<EFBFBD><C8A8><EFBFBD><EFBFBD>
|
|||
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|||
|
.AddJwtBearer(options =>
|
|||
|
{
|
|||
|
options.TokenValidationParameters = new TokenValidationParameters
|
|||
|
{
|
|||
|
ValidateIssuer = true,
|
|||
|
ValidateAudience = true,
|
|||
|
ValidateLifetime = true,
|
|||
|
ValidateIssuerSigningKey = true,
|
|||
|
ValidIssuer = builder.Configuration["Jwt:Issuer"],
|
|||
|
ValidAudience = builder.Configuration["Jwt:Audience"],
|
|||
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
|
|||
|
};
|
|||
|
});
|
|||
|
|
|||
|
|
|||
|
//<2F><>־
|
|||
|
builder.Logging.ClearProviders();
|
|||
|
LogManager.LoadConfiguration($"{AppContext.BaseDirectory}/nlog.config");
|
|||
|
builder.Host.UseNLog();
|
|||
|
var app = builder.Build();
|
|||
|
app.UseCors("AllowSpecificOrigin");
|
|||
|
// Configure the HTTP request pipeline.
|
|||
|
if (app.Environment.IsDevelopment())
|
|||
|
{
|
|||
|
app.UseSwagger();
|
|||
|
app.UseSwaggerUI();
|
|||
|
}
|
|||
|
app.MapGrpcService<CaptchaGrpcService>();
|
|||
|
app.UseAuthentication();
|
|||
|
//ע<><D7A2>ǩ<EFBFBD><C7A9>/<2F><>֤<EFBFBD>м<EFBFBD><D0BC><EFBFBD>
|
|||
|
app.UseMiddleware<SignatureValidationMiddleware>();
|
|||
|
//app.UseAuthentication(appBuilder => appBuilder Disabled = true)
|
|||
|
app.UseStaticFiles();
|
|||
|
var serviceManger = app.Services.GetRequiredService<IConnectionClientManagerService>();
|
|||
|
|
|||
|
//websocket<65><74><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
var service = new HttpService();
|
|||
|
await service.SetupAsync(new TouchSocketConfig()//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
.SetListenIPHosts(6088)
|
|||
|
.ConfigureContainer(a =>
|
|||
|
{
|
|||
|
a.AddConsoleLogger();
|
|||
|
})
|
|||
|
.ConfigurePlugins(a =>
|
|||
|
{
|
|||
|
a.UseWebSocket()//<2F><><EFBFBD><EFBFBD>WebSocket<65><74><EFBFBD><EFBFBD>
|
|||
|
.SetWSUrl("/ws")//<2F><><EFBFBD><EFBFBD>urlֱ<6C>ӿ<EFBFBD><D3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD>
|
|||
|
.UseAutoPong();//<2F><><EFBFBD>յ<EFBFBD>ping<6E><67><EFBFBD><EFBFBD>ʱ<EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>Ӧpong
|
|||
|
a.Add(new WebSocketPlugin(serviceManger));
|
|||
|
}));
|
|||
|
|
|||
|
await service.StartAsync();
|
|||
|
service.Logger.Info("websocket<65><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
|||
|
app.MapControllers();
|
|||
|
app.Run();
|