ComplianceServer/code/Hg.Complaint.WebApi/Program.cs

124 lines
5.1 KiB
C#

using Hg.Complaint.WebApi.workers;
using Microsoft.AspNetCore.ResponseCompression;
try
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("Serilog.json")
.AddJsonFile($"Serilog.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
.Build();
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
var builder = WebApplication.CreateBuilder(args);
var logger = new LoggerConfiguration()
.ReadFrom.Configuration(configuration)
.WriteTo.Exceptionless(builder.Configuration.GetValue<string>("Exceptionless:ApiKey"), builder.Configuration.GetValue<string>("Exceptionless:ServerUrl"), new string[] { "hg-complaint-webapi" })
.CreateLogger();
Log.Logger = logger;
builder.Services.AddLogging(logging =>
{
logging.ClearProviders();
logging.AddSerilog(logger);
});
// Add services to the container.
builder.Services.AddDGEntityFramework<ZxdDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("zxdcrm"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("zxdcrm")));
});
builder.Services.AddDGEntityFramework<CrmDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("crm"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("crm")));
});
builder.Services.AddDGEntityFramework<HgAtionDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("hgaction"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("hgaction")));
});
builder.Services.AddDGEntityFramework<DncmsbaseDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("dncmsbase"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("dncmsbase")));
});
builder.Services.AddDGEntityFramework<UserCenterDbContext>(options =>
{
options.UseMySql(builder.Configuration.GetConnectionString("usercenter"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("usercenter")));
});
builder.Services.AddRedis(builder.Configuration);
builder.Services
.AddAutoIoc(typeof(IScopedDependency), LifeCycle.Scoped)
.AddAutoIoc(typeof(ISingletonDependency), LifeCycle.Singleton)
.AddAutoIoc(typeof(ITransientDependency), LifeCycle.Transient)
.AddMapper();
builder.Services.AddCors(option =>
{
option.AddPolicy(MyAllowSpecificOrigins,
policy =>
{
policy.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
builder.Services.AddExceptionless(builder.Configuration);
builder.Services.AddResponseCompression(opts => //添加压缩中间件服务
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
})
.AddControllers()
.AddApiResult()
.AddApiSignature()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new JsonOptionsExtensions());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
{
Version = "v1",
Title = "HG COMPLAINT API",
Description = "HG COMPLAINT API"
});
var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Hg.Complaint.Domain.xml"));
});
builder.Services.AddDGHttpClient();
//if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") != "PreProduction")
//{
// builder.Services.AddHostedService<ComplaintMessageWorker>();
// builder.Services.AddHostedService<NegativeMessageWorker>();
//}
builder.Services.AddSingleton<ComplaintEventSingleton>();
builder.Services.AddHostedService<DueComplaintWorker>();
builder.Services.AddHostedService<ComplaintMessageWorker>();
builder.Services.AddHostedService<NegativeMessageWorker>();
builder.Services.AddHostedService<LiveDataWorker>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment() || Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "PreProduction")
{
app.UseSwagger();
app.UseSwaggerUI();
//app.UseHttpLogging();
}
app.UseCors(MyAllowSpecificOrigins);
app.UseHttpsRedirection();
app.UseExceptionless();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
catch (Exception ex)
{
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}