using FastGithub.HttpServer;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace FastGithub
{
    /// 
    /// ApplicationBuilder扩展
    /// 
    public static class ApplicationBuilderExtensions
    {
        /// 
        /// 使用服务头
        /// 
        /// 
        /// 
        /// 
        public static IApplicationBuilder UseServerHeader(this IApplicationBuilder app, string serverHeader = nameof(FastGithub))
        {
            return app.Use(next => async context =>
            {
                if (context.Response.HasStarted == false)
                {
                    context.Response.Headers.Server = serverHeader;
                }
                await next(context);
            });
        }
        /// 
        /// 使用http代理中间件
        /// 
        ///  
        /// 
        public static IApplicationBuilder UseHttpProxy(this IApplicationBuilder app)
        {
            var middleware = app.ApplicationServices.GetRequiredService();
            return app.Use(next => context => middleware.InvokeAsync(context));
        }
        /// 
        /// 使用请求日志中间件
        /// 
        ///  
        /// 
        public static IApplicationBuilder UseRequestLogging(this IApplicationBuilder app)
        {
            var middleware = app.ApplicationServices.GetRequiredService();
            return app.Use(next => context => middleware.InvokeAsync(context, next));
        }
        /// 
        /// 使用反向代理中间件
        /// 
        ///  
        /// 
        public static IApplicationBuilder UseHttpReverseProxy(this IApplicationBuilder app)
        {
            var middleware = app.ApplicationServices.GetRequiredService();
            return app.Use(next => context => middleware.InvokeAsync(context, next));
        }
    }
}