using System.Globalization; using DysonNetwork.Pass.Account; using DysonNetwork.Pass.Auth; using DysonNetwork.Pass.Auth.OpenId; using DysonNetwork.Pass.Localization; using DysonNetwork.Pass.Permission; using DysonNetwork.Pass.Wallet; using Microsoft.AspNetCore.RateLimiting; using NodaTime; using NodaTime.Serialization.SystemTextJson; using System.Text.Json; using System.Text.Json.Serialization; using System.Threading.RateLimiting; using DysonNetwork.Pass.Auth.OidcProvider.Options; using DysonNetwork.Pass.Auth.OidcProvider.Services; using DysonNetwork.Pass.Credit; using DysonNetwork.Pass.Handlers; using DysonNetwork.Pass.Leveling; using DysonNetwork.Pass.Mailer; using DysonNetwork.Pass.Safety; using DysonNetwork.Pass.Wallet.PaymentHandlers; using DysonNetwork.Shared.Cache; using DysonNetwork.Shared.GeoIp; using DysonNetwork.Shared.Registry; namespace DysonNetwork.Pass.Startup; public static class ServiceCollectionExtensions { public static IServiceCollection AddAppServices(this IServiceCollection services, IConfiguration configuration) { services.AddLocalization(options => options.ResourcesPath = "Resources"); services.AddDbContext(); services.AddSingleton(SystemClock.Instance); services.AddHttpContextAccessor(); services.AddSingleton(); services.AddHttpClient(); // Register gRPC services services.AddGrpc(options => { options.EnableDetailedErrors = true; // Will be adjusted in Program.cs options.MaxReceiveMessageSize = 16 * 1024 * 1024; // 16MB options.MaxSendMessageSize = 16 * 1024 * 1024; // 16MB }); services.AddRingService(); // Register OIDC services 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.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals; options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower; options.JsonSerializerOptions.DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower; options.JsonSerializerOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); }).AddDataAnnotationsLocalization(options => { options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResource)); }); services.AddRazorPages(); services.Configure(options => { var supportedCultures = new[] { new CultureInfo("en-US"), new CultureInfo("zh-Hans"), }; options.SupportedCultures = supportedCultures; options.SupportedUICultures = supportedCultures; }); return services; } public static IServiceCollection AddAppRateLimiting(this IServiceCollection services) { services.AddRateLimiter(o => o.AddFixedWindowLimiter(policyName: "fixed", opts => { opts.Window = TimeSpan.FromMinutes(1); opts.PermitLimit = 120; opts.QueueLimit = 2; opts.QueueProcessingOrder = QueueProcessingOrder.OldestFirst; })); return services; } public static IServiceCollection AddAppAuthentication(this IServiceCollection services) { services.AddAuthorization(); services.AddAuthentication(options => { options.DefaultAuthenticateScheme = AuthConstants.SchemeName; options.DefaultChallengeScheme = AuthConstants.SchemeName; }) .AddScheme(AuthConstants.SchemeName, _ => { }); return services; } public static IServiceCollection AddAppFlushHandlers(this IServiceCollection services) { services.AddSingleton(); services.AddScoped(); services.AddScoped(); return services; } public static IServiceCollection AddAppBusinessServices(this IServiceCollection services, IConfiguration configuration) { services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection("GeoIP")); 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.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.AddScoped(); services.Configure(configuration.GetSection("OidcProvider")); services.AddScoped(); services.AddHostedService(); return services; } }