using AspNetCoreRateLimit; using ODMO.Api.Dtos.Errors; using ODMO.Application.Admin.Commands; 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.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 FluentValidation; using MediatR; using Microsoft.AspNetCore.Diagnostics; using Microsoft.EntityFrameworkCore; using Microsoft.OpenApi.Models; using Serilog; using System.Net; using System.Reflection; namespace ODMO.Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddDbContext(options => options.UseSqlServer(Configuration["Database:Connection"])); 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.AddTransient(); services.AddSingleton>(); services.AddMediatR(typeof(MediatorApplicationHandlerExtension).GetTypeInfo().Assembly); services.AddSingleton(ConfigureLogger(Configuration)); services.AddMemoryCache(); services.AddControllers(); services.AddValidatorsFromAssemblyContaining(ServiceLifetime.Transient); services.AddEndpointsApiExplorer(); services.AddAuthentication("Bearer"); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Api Digital Master", Description = "Digital Master - API", Version = "v1" }); }); //services.AddOptions(); //services.AddMemoryCache(); //services.Configure(Configuration.GetSection("ClientRateLimiting")); //services.Configure(Configuration.GetSection("ClientRateLimitPolicies")); //services.AddInMemoryRateLimiting(); //services.AddMvc(); //services.AddSingleton(); AddAutoMapper(services); } private static void AddAutoMapper(IServiceCollection services) { 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)); } static Serilog.ILogger ConfigureLogger(IConfiguration configuration) { return new LoggerConfiguration() .ReadFrom.Configuration(configuration) .Enrich.FromLogContext() .WriteTo.RollingFile(configuration["Log:DebugRepository"] ?? "logs\\Debug\\Api", Serilog.Events.LogEventLevel.Debug, retainedFileCountLimit: 100) .WriteTo.RollingFile(configuration["Log:InformationRepository"] ?? "logs\\Information\\Api", Serilog.Events.LogEventLevel.Information, retainedFileCountLimit: 100) .WriteTo.RollingFile(configuration["Log:WarningRepository"] ?? "logs\\Warning\\Api", Serilog.Events.LogEventLevel.Warning, retainedFileCountLimit: 100) .WriteTo.RollingFile(configuration["Log:ErrorRepository"] ?? "logs\\Error\\Api", Serilog.Events.LogEventLevel.Error, retainedFileCountLimit: 100) .CreateLogger(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ODMO.Api v1")); } else { app.UseSwagger(); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "ODMO.Api v1")); app.UseExceptionHandler(appError => { appError.Run(async context => { context.Response.ContentType = "application/json"; var contextFeature = context.Features.Get(); if (contextFeature != null) { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; await context.Response.WriteAsync(new ErrorDetails() { code = 9999, error = "Internal Server Error" }.ToString()); } }); }); } //app.UseClientRateLimiting(); //app.UseMvc(); app.UseAuthentication(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } } }