150 lines
5.1 KiB
C#
150 lines
5.1 KiB
C#
|
|
using Gatedge.ScanCode.Common;
|
|
using Gatedge.ScanCode.Extension;
|
|
using Gatedge.ScanCode.Middleware;
|
|
using Gatedge.ScanCode.Options;
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.AspNetCore.StaticFiles;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.FileProviders;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Microsoft.OpenApi.Models;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Text.Encodings.Web;
|
|
using System.Text.Unicode;
|
|
|
|
namespace Gatedge.ScanCode
|
|
{
|
|
/// <summary>
|
|
/// 主程序
|
|
/// </summary>
|
|
public class Program
|
|
{
|
|
/// <summary>
|
|
/// main方法
|
|
/// </summary>
|
|
/// <param name="args"></param>
|
|
public static void Main(string[] args)
|
|
{
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
// 配置K3CloudApi类
|
|
builder.Services.AddConfigureK3CloudApi(builder.Configuration);
|
|
// 配置Jwt
|
|
builder.Services.ConfigureJwtContext(builder.Configuration);
|
|
// 配置文件
|
|
builder.Services.AddConfigureK3FileService(builder.Configuration);
|
|
// 配置json
|
|
//builder.Services.ConfigureServices();
|
|
// 添加Services
|
|
builder.Services.ConfigureCore();
|
|
// Add services to the container.
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
builder.Services.AddSwaggerGen(option =>
|
|
{
|
|
option.AddSecurityRequirement(new OpenApiSecurityRequirement()
|
|
{
|
|
|
|
{
|
|
new OpenApiSecurityScheme
|
|
{
|
|
Reference = new OpenApiReference
|
|
{
|
|
Id = "Bearer",
|
|
Type = ReferenceType.SecurityScheme
|
|
}
|
|
},
|
|
new List<string>()
|
|
}
|
|
|
|
});
|
|
|
|
// 添加Bearer认证
|
|
option.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"
|
|
});
|
|
|
|
|
|
|
|
// 输出swagger注释
|
|
var basePath = AppContext.BaseDirectory;
|
|
var xmlPath = Path.Combine(basePath, "Gatedge.ScanCode.xml");
|
|
option.IncludeXmlComments(xmlPath);
|
|
|
|
});
|
|
|
|
builder.Logging.AddProvider(new FileLoggerProvider(
|
|
filePath: "../logs/app.log", // 日志文件路径
|
|
maxFileSize: 10 * 1024 * 1024, // 10MB
|
|
maxRetainedFiles: 1000 // 保留最近1000个滚动文件
|
|
));
|
|
|
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.PropertyNamingPolicy = null;//解决后端传到前端全大写
|
|
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);//解决后端返回数据中文被编码
|
|
});
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
app.UseStaticFiles();
|
|
|
|
var filePath = Path.Combine(builder.Environment.ContentRootPath, "K3CloudFile");
|
|
if (!Directory.Exists(filePath))
|
|
{
|
|
Directory.CreateDirectory(filePath);
|
|
}
|
|
var fileProvider = new PhysicalFileProvider(filePath);
|
|
var provider = new FileExtensionContentTypeProvider();
|
|
provider.Mappings[".apk"] = "application/vnd.android.package-archive";
|
|
provider.Mappings[".wgt"] = "application/widget";
|
|
var requestPath = "/Download";
|
|
|
|
// Enable displaying browser links.
|
|
app.UseStaticFiles(new StaticFileOptions
|
|
{
|
|
FileProvider = fileProvider,
|
|
RequestPath = requestPath,
|
|
ContentTypeProvider = provider
|
|
});
|
|
|
|
app.UseDirectoryBrowser(new DirectoryBrowserOptions
|
|
{
|
|
FileProvider = fileProvider,
|
|
RequestPath = requestPath,
|
|
});
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
/*
|
|
中间件注册顺序影响执行顺序,注册顺序 = 执行顺序,异常处理中间件应该放在最后
|
|
*/
|
|
// 1.先授权、再认证
|
|
app.UseAuthentication(); //认证中间件
|
|
app.UseAuthorization(); //授权中间件
|
|
// 日志中间件
|
|
app.UseMiddleware<ResponseLoggingMiddleware>();
|
|
// 全局异常中间件
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
|
//app.UseHttpsRedirection(); // 转发HTTP到HTTPS
|
|
app.MapControllers();
|
|
app.Run();
|
|
}
|
|
}
|
|
}
|