using System.Reflection; using Microsoft.Extensions.Options; using Microsoft.OpenApi.Models; using StackExchange.Redis; using SWAD.API.Middlewares; using SWAD.API.Models.Config; using SWAD.API.Models.Config.ApiServices; using SWAD.API.Services.Links; using SWAD.API.Services.MusicAPI.Api; using SWAD.API.Services.MusicAPI.Auth; namespace SWAD.API; public static class Startup { private static ServicesEndpointsConfig? _servicesEndpointsConfig; public static void Configure(WebApplicationBuilder builder) { SetConfigurations(builder); } public static void ConfigureServices(IServiceCollection services) { SetServiceUsages(services); SetCustomServices(services); } public static void ConfigureApplication(WebApplication app) { SetAppUsages(app); SetAppMappings(app); } private static void SetConfigurations(WebApplicationBuilder builder) { //Json config setup builder.Configuration.AddJsonFile("appsettings.json", false, true) //load base settings .AddJsonFile("appsettings.local.json", true, true); //load local settings builder.Services.Configure (builder.Configuration.GetSection(ApiServicesConfig.ConfigName)); builder.Services.Configure (builder.Configuration.GetSection(ServicesEndpointsConfig.ConfigName)); //Get config for setup _servicesEndpointsConfig = builder.Configuration .GetSection(ServicesEndpointsConfig.ConfigName) .Get(); } private static void SetServiceUsages(IServiceCollection services) { services.AddProblemDetails(); services.AddControllers(); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Version = Assembly.GetExecutingAssembly().GetName().Version!.ToString(), Title = "SWAD.API", Description = "Share With All, Dude - Platform for sharing music links from one music service to another for free!\n" + "API Reference for SWAD.Front and for guys who trying to make themself stuff :)", TermsOfService = new Uri("https://github.com/SpectruMTeamCode/SWAD/tree/main"), Contact = new OpenApiContact { Name = "GitHub", Url = new Uri("https://github.com/SpectruMTeamCode/SWAD/tree/main") } }); options.IncludeXmlComments(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "doc.xml")); options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First()); }); } private static void SetCustomServices(IServiceCollection services) { //Custom services setup //TODO read from config services.AddSingleton(ConnectionMultiplexer.Connect(_servicesEndpointsConfig.Redis)); services.AddSingleton(p => p.GetService()?.GetDatabase() ?? throw new NullReferenceException("Чёт с инжектом редиса не то")); services.AddSingleton(p => ConnectionMultiplexer.Connect(_servicesEndpointsConfig.Redis)); services.AddSingleton(p => p.GetService()!.GetDatabase()); services.AddSingleton(); //Register all IAPIService implementations foreach (var service in ApiService.GetAllImplementations()) services.AddSingleton(typeof(ApiService), service); //Register all AuthService implementations foreach (var service in AbstractAuthService.GetAllImplementations()) services.AddSingleton(typeof(AbstractAuthService), service); } private static void SetAppUsages(WebApplication app) { app.UseMiddleware(); app.UseSwagger(); app.UseSwaggerUI(options => { options.InjectStylesheet("/swagger-ui/theme-flattop.css"); options.IndexStream = () => Assembly.GetEntryAssembly()!.GetManifestResourceStream("SWAD.API.index.html"); }); app.UseStaticFiles(); } private static void SetAppMappings(WebApplication app) { app.MapControllers(); //Redirect to swagger app.Map("/", () => { return Results.LocalRedirect("/swagger"); }); } }