108 lines
3.6 KiB
C#
108 lines
3.6 KiB
C#
using Cms.Core.EntityFramework;
|
|
using Cms.External.WebApi;
|
|
using DG.Core;
|
|
using DG.EntityFramework;
|
|
using Microsoft.AspNetCore.ResponseCompression;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Serilog;
|
|
try
|
|
{
|
|
var configuration = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory())
|
|
.AddJsonFile("Serilog.json")
|
|
.AddJsonFile($"Serilog.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", true)
|
|
.Build();
|
|
|
|
var logger = new LoggerConfiguration()
|
|
.ReadFrom.Configuration(configuration)
|
|
.CreateLogger();
|
|
|
|
Log.Logger = logger;
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
builder.Services.AddLogging(logging =>
|
|
{
|
|
logging.ClearProviders();
|
|
logging.AddSerilog(logger);
|
|
});
|
|
|
|
Log.Information("Starting Cms External WebApi");
|
|
var MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
|
|
// Add services to the container.
|
|
builder.Services.AddSingleton(new InitConfiguration(builder.Configuration));
|
|
builder.Services.AddSwaggerGen(options =>
|
|
{
|
|
options.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo
|
|
{
|
|
Version = "v1",
|
|
Title = "Cms External WebApi",
|
|
Description = "CMS系统外部调用api接口"
|
|
});
|
|
var xmlFilename = $"{System.Reflection.Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
|
|
});
|
|
builder.Services.AddCors(option =>
|
|
{
|
|
option.AddPolicy(MyAllowSpecificOrigins,
|
|
policy =>
|
|
{
|
|
policy.AllowAnyOrigin()
|
|
.AllowAnyHeader()
|
|
.AllowAnyMethod();
|
|
});
|
|
});
|
|
builder.Services.AddDGHttpClient();
|
|
builder.Services.AddResponseCompression(opts => //添加压缩中间件服务
|
|
{
|
|
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
|
|
new[] { "application/octet-stream" });
|
|
})
|
|
.AddControllers()
|
|
.AddApiResult()
|
|
.AddJsonOptions(options =>
|
|
{
|
|
options.JsonSerializerOptions.Converters.Add(new JsonOptionsExtensions());
|
|
});
|
|
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
builder.Services.AddMapper();
|
|
builder.Services.AddResponseCompression(options =>
|
|
{
|
|
options.EnableForHttps = true;
|
|
//options.Providers.Add<BrotliCompressionProvider>();
|
|
options.Providers.Add<GzipCompressionProvider>();
|
|
});
|
|
builder.Services.AddDGEntityFramework<DncmsbaseDbContext>(options =>
|
|
{
|
|
options.UseMySql(builder.Configuration.GetConnectionString("dncmsbase"), ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("dncmsbase")));
|
|
});
|
|
builder.Services.AddAutoIoc(typeof(IScopedDependency), LifeCycle.Scoped)
|
|
.AddAutoIoc(typeof(ISingletonDependency), LifeCycle.Singleton)
|
|
.AddAutoIoc(typeof(ITransientDependency), LifeCycle.Transient)
|
|
.AddMapper();
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment() || Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "PreProduction")
|
|
{
|
|
app.UseSwagger();
|
|
app.UseSwaggerUI();
|
|
}
|
|
app.UseCors(MyAllowSpecificOrigins);
|
|
app.UseHttpsRedirection();
|
|
|
|
app.UseResponseCompression();
|
|
|
|
app.UseAuthorization();
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Fatal(ex, "Host terminated unexpectedly");
|
|
}
|
|
finally
|
|
{
|
|
Log.CloseAndFlush();
|
|
} |