Example #1
0
 private static void ApplyGlobalAfterHook(IApplicationBuilder builder, BotwinOptions options)
 {
     if (options?.After != null)
     {
         builder.Use(async(ctx, next) =>
         {
             await next();
             await options.After(ctx);
         });
     }
 }
Example #2
0
 private static void ApplyGlobalBeforeHook(IApplicationBuilder builder, BotwinOptions options)
 {
     if (options?.Before != null)
     {
         builder.Use(async(ctx, next) =>
         {
             var carryOn = await options.Before(ctx); //TODO Check if return Task.CompletedTask will it continue
             if (carryOn)
             {
                 await next();
             }
         });
     }
 }
Example #3
0
 private static void ApplyGlobalAfterHook(IApplicationBuilder builder, BotwinOptions options)
 {
     if (options?.After != null)
     {
         builder.Use(async(ctx, next) =>
         {
             var loggerFactory = ctx.RequestServices.GetService <ILoggerFactory>();
             var logger        = loggerFactory.CreateLogger("Botwin.GlobalAfterHook");
             await next();
             logger.LogTrace("Executing global after hook");
             await options.After(ctx);
         });
     }
 }
Example #4
0
        private static void ApplyGlobalBeforeHook(IApplicationBuilder builder, BotwinOptions options)
        {
            if (options?.Before != null)
            {
                builder.Use(async(ctx, next) =>
                {
                    var loggerFactory = ctx.RequestServices.GetService <ILoggerFactory>();
                    var logger        = loggerFactory.CreateLogger("Botwin.GlobalBeforeHook");
                    logger.LogTrace("Executing global before hook");

                    var carryOn = await options.Before(ctx);
                    if (carryOn)
                    {
                        logger.LogTrace("Executing next handler after global before hook");
                        await next();
                    }
                });
            }
        }
Example #5
0
        public static IApplicationBuilder UseBotwin(this IApplicationBuilder builder, BotwinOptions options)
        {
            ApplyGlobalBeforeHook(builder, options);

            ApplyGlobalAfterHook(builder, options);

            var routeBuilder = new RouteBuilder(builder);

            //Invoke so ctors are called that adds routes to IRouter
            var srvs = builder.ApplicationServices.GetServices <BotwinModule>();

            //Cache status code handlers
            var statusCodeHandlers = builder.ApplicationServices.GetServices <IStatusCodeHandler>();

            foreach (var module in srvs)
            {
                foreach (var route in module.Routes)
                {
                    RequestDelegate handler;

                    handler = CreateModuleBeforeAfterHandler(module, route);

                    var finalHandler = CreateFinalHandler(handler, statusCodeHandlers);

                    routeBuilder.MapVerb(route.verb, route.path, finalHandler);
                }
            }

            return(builder.UseRouter(routeBuilder.Build()));
        }
Example #6
0
        /// <summary>
        /// Adds Botwin to the specified <see cref="IApplicationBuilder"/>.
        /// </summary>
        /// <param name="builder">The <see cref="IApplicationBuilder"/> to configure.</param>
        /// <param name="options">A <see cref="BotwinOptions"/> instance.</param>
        /// <returns>A reference to this instance after the operation has completed.</returns>
        public static IApplicationBuilder UseBotwin(this IApplicationBuilder builder, BotwinOptions options)
        {
            ApplyGlobalBeforeHook(builder, options);

            ApplyGlobalAfterHook(builder, options);

            var routeBuilder = new RouteBuilder(builder);

            //Create a "startup scope" to resolve modules from
            using (var scope = builder.ApplicationServices.CreateScope())
            {
                //Get all instances of BotwinModule to fetch and register declared routes
                foreach (var module in scope.ServiceProvider.GetServices <BotwinModule>())
                {
                    var moduleType    = module.GetType();
                    var distinctPaths = module.Routes.Keys.Select(route => route.path).Distinct();

                    foreach (var path in distinctPaths)
                    {
                        routeBuilder.MapRoute(path, CreateRouteHandler(path, moduleType));
                    }
                }
            }

            return(builder.UseRouter(routeBuilder.Build()));
        }