using System.Text.Json; using AdminApi; using AdminHost.Extension; using AdminHost.Jobs; using AdminHost.Middleware; using Microsoft.AspNetCore.SignalR; using AdminService; using Autofac; using Autofac.Extensions.DependencyInjection; using Common.Request; using JWTHelper; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Options; using MongoRepository.Config; using Newtonsoft.Json; using NLog.Extensions.Logging; using NLog.Web; using RedisHelper; using ScheduledService; using SharpAbp.Infrastructure.Api.Middleware; Environment.SetEnvironmentVariable("SERVICE_NAME", Environment.GetEnvironmentVariable("SERVICE_NAME") ?? System.Reflection.Assembly.GetEntryAssembly()?.GetName().Name ?? "AdminHost"); Environment.SetEnvironmentVariable("INSTANCE_ID", Environment.GetEnvironmentVariable("INSTANCE_ID") ?? "default"); var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddControllers() .AddApplicationPart(typeof(AssemblyMarker).Assembly) .AddControllersAsServices() .ConfigureApiBehaviorOptions(options => { options.SuppressModelStateInvalidFilter = true; }); builder.Services.AddControllers(); builder.Logging.AddNLog(); builder.Host.UseNLog(); builder.Services.AddControllers().AddNewtonsoftJson( option => { option.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; option.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; option.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; option.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); } ); builder.Configuration.AddJsonFile(Path.Combine("Config", "system.json"), optional: true); builder.Services.Configure(builder.Configuration.GetSection(nameof(MongoDbSettings))); builder.Services.AddSingleton(sp => sp.GetRequiredService>().Value); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddAutoMapper(typeof(AutoMapperConfig)); builder.Services.AddHealthChecks() .AddCheck("self", () => HealthCheckResult.Healthy(), tags: new[] { "self" }); builder.Services.AddCors(options => { options.AddPolicy("Cors", policy => { policy .SetIsOriginAllowed(_ => true) .AllowAnyHeader() .AllowAnyMethod() .AllowCredentials(); }); }); builder.Services.AddSignalR(); #region jwt builder.Services.AddCustomJwt(builder.Configuration); #endregion #region redis builder.Services.AddRedisCache(builder.Configuration); #endregion #region Cap builder.Services.AddCap(builder.Configuration); #endregion builder.Services.AddCronJob(c => { c.TimeZoneInfo = TimeZoneInfo.Local; c.CronExpression = @"0 0 0 * * *"; }); builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory()); builder.Host.ConfigureContainer(builder => { builder.RegisterModule(); }); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseCors("Cors"); app.UseRouting(); app.UseStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); app.UseApiLogging(); app.UseOperationLog(); app.MapControllers(); app.UseGlobalExceptionHandler(); app.MapHealthChecks("/health", new HealthCheckOptions { Predicate = _ => true, ResponseWriter = async (context, report) => { context.Response.ContentType = "application/json"; var result = new { status = report.Status.ToString(), checks = report.Entries.Select(e => new { name = e.Key, status = e.Value.Status.ToString(), description = e.Value.Description, duration = e.Value.Duration.TotalMilliseconds }), totalDuration = report.TotalDuration.TotalMilliseconds }; await context.Response.WriteAsync(System.Text.Json.JsonSerializer.Serialize(result)); } }); app.Run();