using ODMO.Application.Admin.Repositories; using ODMO.Application.Extensions; using ODMO.Application.Services; using ODMO.Commons.Interfaces; using ODMO.Commons.Repositories.Admin; using ODMO.Infrastructure; using ODMO.Infrastructure.Extensions; using ODMO.Infrastructure.Mapping; using ODMO.Infrastructure.Repositories.Account; using ODMO.Infrastructure.Repositories.Admin; using ODMO.Infrastructure.Repositories.Character; using ODMO.Infrastructure.Repositories.Config; using ODMO.Infrastructure.Repositories.Routine; using ODMO.Infrastructure.Repositories.Server; using MediatR; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Serilog; using Serilog.Events; using System.Globalization; using System.Reflection; using ODMO.Account.Models.Configuration; using ODMO.Commons.Utils; using Microsoft.EntityFrameworkCore; namespace ODMO.Account { public class Program { public static void Main(string[] args) { CreateHostBuilder(args).Run(); } private static void UnhandledExceptionHandler(object sender, UnhandledExceptionEventArgs e) { Console.WriteLine(((Exception)e.ExceptionObject).InnerException); if (e.IsTerminating) { var message = ""; var exceptionStackTrace = ""; if (e.ExceptionObject is Exception exception) { message = exception.Message; exceptionStackTrace = exception.StackTrace; } Console.WriteLine($"{message}"); Console.WriteLine($"{exceptionStackTrace}"); Console.WriteLine("Terminating by unhandled exception..."); } else Console.WriteLine("Received unhandled exception."); Console.ReadLine(); } public static IHost CreateHostBuilder(string[] args) { AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionHandler; Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; var host = Host.CreateDefaultBuilder(args) .UseSerilog() .UseEnvironment("Development") .ConfigureServices((context, services) => { services.AddDbContext(); services.AddOptions() .BindConfiguration("AuthenticationServer") .ValidateOnStart(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); //services.AddScoped(); services.AddSingleton>(); services.AddSingleton(); services.AddSingleton(ConfigureLogger(context.Configuration)); services.AddHostedService(); services.AddMediatR(typeof(MediatorApplicationHandlerExtension).GetTypeInfo().Assembly); services.AddTransient(); services.AddAutoMapper(typeof(AccountProfile)); services.AddAutoMapper(typeof(AssetsProfile)); services.AddAutoMapper(typeof(CharacterProfile)); services.AddAutoMapper(typeof(ConfigProfile)); services.AddAutoMapper(typeof(DigimonProfile)); services.AddAutoMapper(typeof(GameProfile)); services.AddAutoMapper(typeof(SecurityProfile)); services.AddAutoMapper(typeof(ShopProfile)); }) .ConfigureHostConfiguration(hostConfig => { hostConfig.SetBasePath(Directory.GetCurrentDirectory()) .AddEnvironmentVariables(Constants.Configuration.EnvironmentPrefix) .AddUserSecrets(); }).Build(); // Applying migrations. It's enough to do this on the AccountServer, for now. var scopeFactory = host.Services.GetService(); using var scope = scopeFactory.CreateScope(); var context = scope.ServiceProvider.GetService(); context.Database.Migrate(); return host; } private static ILogger ConfigureLogger(IConfiguration configuration) { return new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", restrictedToMinimumLevel: LogEventLevel.Information) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Verbose) .WriteTo.RollingFile(configuration["Log:VerboseRepository"] ?? "logs\\Verbose\\AccountServer", retainedFileCountLimit: 10)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug) .WriteTo.RollingFile(configuration["Log:DebugRepository"] ?? "logs\\Debug\\AccountServer", retainedFileCountLimit: 5)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information) .WriteTo.RollingFile(configuration["Log:InformationRepository"] ?? "logs\\Information\\AccountServer", retainedFileCountLimit: 5)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning) .WriteTo.RollingFile(configuration["Log:WarningRepository"] ?? "logs\\Warning\\AccountServer", retainedFileCountLimit: 5)) .WriteTo.Logger(lc => lc .Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error) .WriteTo.RollingFile(configuration["Log:ErrorRepository"] ?? "logs\\Error\\AccountServer", retainedFileCountLimit: 5)) .CreateLogger(); } } }