80 lines
2.1 KiB
C#
80 lines
2.1 KiB
C#
|
using Serilog;
|
|||
|
using FortranWebApi.Services;
|
|||
|
using System.IO;
|
|||
|
using System.Text.Json;
|
|||
|
using System.Text.Json.Serialization;
|
|||
|
|
|||
|
// 设置库搜索路径
|
|||
|
string currentDir = Directory.GetCurrentDirectory();
|
|||
|
Environment.SetEnvironmentVariable("LD_LIBRARY_PATH",
|
|||
|
$"{Environment.GetEnvironmentVariable("LD_LIBRARY_PATH")}:{currentDir}");
|
|||
|
|
|||
|
var builder = WebApplication.CreateBuilder(args);
|
|||
|
|
|||
|
// 添加健康检查服务
|
|||
|
builder.Services.AddHealthChecks();
|
|||
|
|
|||
|
// 配置Serilog
|
|||
|
Log.Logger = new LoggerConfiguration()
|
|||
|
.WriteTo.Console(outputTemplate:
|
|||
|
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|||
|
.WriteTo.File("logs/log-.txt",
|
|||
|
rollingInterval: RollingInterval.Day,
|
|||
|
outputTemplate:
|
|||
|
"{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
|
|||
|
.CreateLogger();
|
|||
|
|
|||
|
builder.Host.UseSerilog(); // 将Serilog添加到Host
|
|||
|
|
|||
|
// 配置JSON序列化选项,使用驼峰命名法
|
|||
|
builder.Services.AddControllers().AddJsonOptions(options =>
|
|||
|
{
|
|||
|
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
|
|||
|
options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.CamelCase;
|
|||
|
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
|
|||
|
options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
|
|||
|
});
|
|||
|
|
|||
|
builder.Services.AddEndpointsApiExplorer();
|
|||
|
builder.Services.AddSwaggerGen();
|
|||
|
builder.Services.AddSingleton<FortranInteropService>();
|
|||
|
|
|||
|
// 配置CORS
|
|||
|
builder.Services.AddCors(options =>
|
|||
|
{
|
|||
|
options.AddDefaultPolicy(policy =>
|
|||
|
{
|
|||
|
policy.AllowAnyOrigin()
|
|||
|
.AllowAnyMethod()
|
|||
|
.AllowAnyHeader();
|
|||
|
});
|
|||
|
});
|
|||
|
|
|||
|
var app = builder.Build();
|
|||
|
|
|||
|
// Configure the HTTP request pipeline.
|
|||
|
if (app.Environment.IsDevelopment())
|
|||
|
{
|
|||
|
app.UseSwagger();
|
|||
|
app.UseSwaggerUI();
|
|||
|
}
|
|||
|
|
|||
|
app.UseCors();
|
|||
|
app.UseAuthorization();
|
|||
|
// 映射健康检查端点
|
|||
|
app.MapHealthChecks("/health");
|
|||
|
app.MapControllers();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Log.Information("启动应用程序...");
|
|||
|
app.Run();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
Log.Fatal(ex, "应用程序启动失败");
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
Log.CloseAndFlush();
|
|||
|
}
|