Ejemplo n.º 1
0
        public static HttpResponseBuilder WhenGet(this HttpHandlerBuilder builder, string path = null)
        {
            bool Match(HttpContext context)
            {
                if (path != null && !context.MatchPath(path))
                {
                    return(false);
                }
                return(context.Request.Method.Equals(HttpMethod.Get.ToString()));
            }

            return(builder.When(Match));
        }
Ejemplo n.º 2
0
        private static void Configure(IApplicationBuilder app, HttpHandlerBuilder requestHandlerBuilder)
        {
            app.Run(async context =>
            {
                requestHandlerBuilder.AddReceivedRequest(context);

                var handler = requestHandlerBuilder.RequestHandlers.FirstOrDefault(rm => rm.Key(context));

                if (!handler.Equals(default(KeyValuePair <Func <HttpContext, bool>, Action <HttpContext> >)))
                {
                    handler.Value(context);
                }
                else
                {
                    context.Response.StatusCode = 400;
                    await context.Response.WriteAsync("no handler found for this request");
                }
            });
        }
Ejemplo n.º 3
0
        public static HttpHandlerBuilder Start(int port, Action <ILoggingBuilder> loggingBuilder = null)
        {
            var handlerBuilder = new HttpHandlerBuilder();

            var webHostBuilder = new WebHostBuilder()
                                 .UseKestrel(k => k.Listen(IPAddress.Any, port))
                                 .Configure(app => Configure(app, handlerBuilder));

            if (loggingBuilder != null)
            {
                webHostBuilder.ConfigureLogging(loggingBuilder);
            }

            var webHost = webHostBuilder.Build();

            handlerBuilder.SetServer(webHost);
            webHost.Start();

            return(handlerBuilder);
        }
Ejemplo n.º 4
0
 internal HttpResponseBuilder(HttpHandlerBuilder httpHandlerBuilder, Func <HttpContext, bool> requestMatcher)
 {
     _httpHandlerBuilder = httpHandlerBuilder;
     _requestMatcher     = requestMatcher;
 }
Ejemplo n.º 5
0
 public static HttpResponseBuilder WhenPost(this HttpHandlerBuilder builder, string path, Func <HttpContext, bool> matcher)
 {
     return(builder.When(context => context.Request.Method.Equals(HttpMethod.Post.ToString()) && context.MatchPath(path) && matcher(context)));
 }