using AspectCore.Extensions.DependencyInjection;
using Dpz.Core.Infrastructure;
using Dpz.Core.Infrastructure.Configuration;
using Dpz.Core.Service;
using Dpz.Core.Web.Jobs;
using Dpz.Core.Web.Jobs.Hangfire;
using Microsoft.AspNetCore.HttpOverrides;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Serializers;
using Serilog;
using Serilog.Events;
Log.Logger = new LoggerConfiguration()
.Enrich.FromLogContext()
.CreateBootstrapLogger();
try
{
var builder = WebApplication.CreateBuilder(args);
builder.Host.UseAgileConfig();
// Add services to the container.
var services = builder.Services;
var configuration = builder.Configuration;
var objectSerializer = new ObjectSerializer(_ => true);
BsonSerializer.RegisterSerializer(objectSerializer);
#region configuration logs
bool LogFilter(LogEvent logEvent)
{
if (logEvent.Properties.ContainsKey("RequestPath"))
{
var pathPrefix = new[]
{
"/jobs",
"/account"
};
if (logEvent.Properties["RequestPath"] is ScalarValue value)
{
return pathPrefix.Any(x =>
value.Value?.ToString()?.StartsWith(x, StringComparison.CurrentCultureIgnoreCase) ?? false);
}
}
return false;
}
var logSeq = configuration.GetSection("LogSeq").Get<LogSeq>();
builder.Host.ConfigurationLog(logSeq,LogFilter);
#endregion
const int threads = 100;
if (ThreadPool.SetMinThreads(threads, threads))
{
Log.Information("set worker threads:{WorkerThreads},set completion port threads {CompletionPortThreads}",
threads,
threads);
}
services.AddInject();
services.AddAppHangfireService(configuration);
services.AddControllersWithViews();
services.AddSignalR();
services.AddBusinessServices(configuration);
services.ConfigureDynamicProxy();
builder.Host.UseServiceProviderFactory(new DynamicProxyServiceProviderFactory());
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseSerilogRequestLogging();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCors(appBuilder =>
{
appBuilder.WithOrigins(configuration.GetSection("Origins").Get<string[]>())
.AllowAnyHeader()
.WithMethods("GET", "PUT", "POST", "DELETE", "PATCH", "OPTION")
.AllowCredentials();
});
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.HangfireSetting();
app.Run();
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
File.AppendAllTextAsync(Path.Combine("error_logs", $"ERR{DateTime.Now:yyyyMMdd}.log"), ex.ToString());
Log.Fatal(ex, "Host terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
public partial class Program
{
/// <summary>
/// cookie name
/// </summary>
public const string AuthorizeCookieName = "Dpz.Core.Jobs.Authoriza";
}