You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
124 lines
4.1 KiB
124 lines
4.1 KiB
using BBWYB.Common.Http;
|
|
using BBWYB.Common.Log;
|
|
using BBWYB.Server.Business;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using Yitter.IdGenerator;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
var services = builder.Services;
|
|
var configuration = builder.Configuration;
|
|
|
|
services.AddMemoryCache();
|
|
|
|
var idOption = new IdGeneratorOptions(1);
|
|
var idGenerator = new DefaultIdGenerator(idOption);
|
|
services.AddSingleton(typeof(IIdGenerator), idGenerator);
|
|
|
|
//var fsql = new FreeSql.FreeSqlBuilder().UseConnectionString(FreeSql.DataType.MySql, configuration.GetConnectionString("DB")).Build();
|
|
//services.AddSingleton(typeof(IFreeSql), fsql);
|
|
|
|
services.AddSingleton<NLogManager>();
|
|
services.AddSingleton<RestApiService>();
|
|
services.AddSingleton<TaskSchedulerManager>();
|
|
services.AddMemoryCache();
|
|
services.AddControllers();
|
|
services.AddHttpContextAccessor();
|
|
services.AddHttpClient();
|
|
services.AddCors(options =>
|
|
{
|
|
options.AddPolicy("cors", p =>
|
|
{
|
|
p.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
|
|
});
|
|
});
|
|
|
|
services.AddControllers();
|
|
services.AddEndpointsApiExplorer();
|
|
services.AddSwaggerGen(c =>
|
|
{
|
|
c.SwaggerDoc("v1", new OpenApiInfo
|
|
{
|
|
Version = "v1.0.0",
|
|
Title = "步步为盈B端API",
|
|
Description = "注意事项\r\n1.返回参数名称采用大驼峰命名\r\n2.ApiResponse为基础返回对象(Code,Data,Message),接口中所有的返回值均属于Data属性\r\n3.正常返回Code=200"
|
|
});
|
|
//JWT认证
|
|
c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new OpenApiSecurityScheme
|
|
{
|
|
Scheme = JwtBearerDefaults.AuthenticationScheme,
|
|
BearerFormat = "JWT",
|
|
Type = SecuritySchemeType.ApiKey,
|
|
Name = "Authorization",
|
|
In = ParameterLocation.Header,
|
|
Description = "Authorization:Bearer {your JWT token}<br/>",
|
|
});
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
{
|
|
{
|
|
new OpenApiSecurityScheme{Reference = new OpenApiReference
|
|
{
|
|
Type = ReferenceType.SecurityScheme,
|
|
Id = JwtBearerDefaults.AuthenticationScheme
|
|
}
|
|
},
|
|
new string[] { }
|
|
}
|
|
});
|
|
|
|
var executingAssembly = Assembly.GetExecutingAssembly();
|
|
var assemblyNames = executingAssembly.GetReferencedAssemblies().Union(new AssemblyName[] { executingAssembly.GetName() }).ToArray();
|
|
Array.ForEach(assemblyNames, (assemblyName) =>
|
|
{
|
|
//var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
var xmlFile = $"{assemblyName.Name}.xml";
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
|
if (!File.Exists(xmlPath))
|
|
return;
|
|
c.IncludeXmlComments(xmlPath, true);
|
|
});
|
|
});
|
|
|
|
var secret = configuration.GetSection("Secret").Value;
|
|
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, x =>
|
|
{
|
|
x.SaveToken = true;
|
|
x.RequireHttpsMetadata = false;
|
|
x.TokenValidationParameters = new TokenValidationParameters()
|
|
{
|
|
ClockSkew = TimeSpan.Zero,
|
|
ValidateIssuerSigningKey = true,
|
|
ValidateIssuer = false,
|
|
ValidateAudience = false,
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
|
|
//ValidIssuer = issuer,
|
|
//ValidAudience = audience,
|
|
//ValidateLifetime = true
|
|
};
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
//yunDingBusiness.RefreshKey();
|
|
var isAllowedSwagger = configuration.GetValue<bool>("AllowedSwagger");
|
|
if (isAllowedSwagger)
|
|
{
|
|
app.UseSwagger(c => c.SerializeAsV2 = true)
|
|
.UseSwaggerUI(c =>
|
|
{
|
|
c.SwaggerEndpoint("/swagger/v1/swagger.json", "BBWYB API");
|
|
c.RoutePrefix = string.Empty;
|
|
});
|
|
}
|
|
|
|
//app.UseHttpsRedirection();
|
|
app.UseRouting();
|
|
app.UseCors("cors");
|
|
app.UseAuthorization();
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
|