Esempio n. 1
0
 public GameStatusController(RequestContext requestContext, IGameApiService gameApiService, IRequestLoggingService requestLoggingService, ILogger <GameStatusController> logger)
 {
     this.requestContext        = requestContext ?? throw new ArgumentNullException("RequestContext");
     this.gameApiService        = gameApiService ?? throw new ArgumentNullException("IGameApiService");
     this.requestLoggingService = requestLoggingService ?? throw new ArgumentNullException("IRequestLoggingService");
     this.logger = logger ?? throw new ArgumentNullException("ILogger");
 }
Esempio n. 2
0
 public JoinController(RequestContext requestContext, IGameApiService gameApiService, IRequestLoggingService requestLoggingService, ILogger <JoinController> logger)
 {
     this.requestContext        = requestContext;
     this.gameApiService        = gameApiService;
     this.requestLoggingService = requestLoggingService;
     this.logger = logger;
 }
 public LoggingController(IRequestLoggingService logginService) => this.loggingService = logginService;
Esempio n. 4
0
        public async Task InvokeAsync(HttpContext context, RequestContext requestContext, ILogger <RequestRecordingMiddleware> logger, IRequestLoggingService requestLoggingService)
        {
            await _next(context);

            try
            {
                var requestMethod = (RequestMethod)Enum.Parse(typeof(RequestMethod), context.Request.Method);
                var statusCode    = context.Response.StatusCode;
                var requestApi    = context.Request.Path;
                logger.LogWarning($"Recording Request { requestContext.RequestId } | {requestMethod} | {requestApi} | {statusCode}");
                await requestLoggingService.RecordRequest(requestMethod, statusCode, requestApi, context.User.Identity.Name);
            }
            catch (Exception)
            {
                logger.LogError($"Failed recording request {requestContext.RequestId} | {context.Request.Method} | {context.Request.Path}");
            }
        }
Esempio n. 5
0
 public RequestLoggingFilter(IRequestLoggingService requestLoggingService)
 {
     this._requestLoggingService = requestLoggingService;
 }
 public ApiExceptionFilter()
 {
     _requestLoggingService = (IRequestLoggingService)NinjectHelper.GetResolveService(typeof(IRequestLoggingService));
 }
Esempio n. 7
0
        public async Task InvokeAsync(HttpContext httpContext, IGameApiService gameApiService, RequestContext requestContext, ILogger <ThrottlingMiddleware> logger, IRequestLoggingService requestLoggingService)
        {
            var endpoint = httpContext.GetEndpoint();

            var jsonSerializerSettings = new JsonSerializerSettings
            {
                ContractResolver = new DefaultContractResolver
                {
                    NamingStrategy = new CamelCaseNamingStrategy()
                }
            };

            if (endpoint != null)
            {
                var metadata = endpoint.Metadata;

                foreach (var metadataObject in metadata)
                {
                    if (metadataObject.GetType().Equals(typeof(ThrottleAttribute)))
                    {
                        var throttleAttribute = (ThrottleAttribute)metadataObject;
                        var path     = httpContext.Request.Path;
                        var username = httpContext.User.Identity.Name;
                        if (username == null || !path.HasValue || !await gameApiService.ValidRequest(path.ToString(), username, throttleAttribute.ticks))
                        {
                            logger.LogWarning($"Request { requestContext.RequestId } rejected in api throttling");

                            string json = JsonConvert.SerializeObject(new GuessResponse
                            {
                                RequestId = requestContext.RequestId,
                                Err       = new Error
                                {
                                    Message     = "Rejected in Api Throttling",
                                    Description = $"Please send a valid request. Only 1 request allowed every { throttleAttribute.ticks / ticksPerSecond } seconds"
                                }
                            }, jsonSerializerSettings);

                            httpContext.Response.StatusCode  = 400;
                            httpContext.Response.ContentType = "application/json; charset=utf-8";

                            await httpContext.Response.WriteAsync(json, Encoding.UTF8);

                            return;
                        }
                    }
                }
            }

            await _next(httpContext);
        }