// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IUserRepository <string> userRepository, IOptions <ClientRateLimitPolicies> clientRateLimitPolicies) { // will load user rate limit in memory cache RateLimitHelper.LoadUsersRateLimits(userRepository, clientRateLimitPolicies); //app.UseClientRateLimiting(); app.UseMiddleware <CustomClientRateLimit>(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler(); } app.UseStatusCodePages(); app.UseStaticFiles(); // Config Auto Mapper AutoMapperConfig.Setup(); // Seed Database with initial data // SeedDBHelper.EnsureDBSeeded(logSystemUnitOfWork, settings); // setup swagger ui SwaggerConfig.Config(app); app.UseMvc(); }
private static async Task <int> HandleAlbumOptions(AlbumOptions options) { var handler = await DefaultOptionsHandler.CreateAsync(options).ConfigureAwait(false); var client = handler.GetCurrentMuzlanClient(); var authResponse = await client.Auth.Authenticate().ConfigureAwait(false); if (!authResponse.HasResult) { return(ExitCodeHelper.Set(ExitCode.AuthenticationFailure)); } if (!string.IsNullOrEmpty(options.Url)) { // TODO: parse album or artist url return(ExitCodeHelper.Set(ExitCode.ParserError)); } else if (string.IsNullOrEmpty(options.Album)) { var artistResponse = await RateLimitHelper.BypassRateLimitAsync( handler, (client) => client.Albums.FindAlbums(options.Artist ?? string.Empty)).ConfigureAwait(false); if (artistResponse.HasResult) { var json = await JsonHelper.SerializeAsync(artistResponse.Result, handler.PrettyPrint).ConfigureAwait(false); Console.WriteLine(json); } return(ExitCodeHelper.Set(artistResponse)); } else { var albumResponse = await RateLimitHelper.BypassRateLimitAsync( handler, (client) => client.Albums.GetTracks(options.Artist ?? string.Empty, options.Album ?? string.Empty)) .ConfigureAwait(false); if (albumResponse.HasResult) { var json = await JsonHelper.SerializeAsync(albumResponse.Result, handler.PrettyPrint).ConfigureAwait(false); Console.WriteLine(json); } return(ExitCodeHelper.Set(albumResponse)); } }
public async Task <IActionResult> CreateUser([FromBody] UserCreateDto dto) { if (dto == null) { return(BadRequest()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var user = Mapper.Map <UserEntity>(dto); user = await userService.CreateUserAsync(user); #region User Rate Limit // set user rate limit var clientPolicy = RateLimitHelper.CreateUserRateLimitPolicy(user.ID, user.ThrottlingLimit, user.ThrottlingPeriod); if (clientPolicy != null && clientRateLimitOptions != null && clientPolicyStore != null && !clientPolicyStore.Exists(clientPolicy.ClientId)) { clientPolicyStore.Set($"{clientRateLimitOptions.ClientPolicyPrefix}_{clientPolicy.ClientId}", clientPolicy); } #endregion return(CreatedAtRoute("GetUser", new { Id = user.ID }, user)); } catch (CustomException e) { return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message)); } }