Exemplo n.º 1
0
        public static IServiceCollection AddElectSwagger(this IServiceCollection services, [NotNull] Action <ElectSwaggerOptions> configuration)
        {
            services.Configure(configuration);

            var options = configuration.GetValue();

            if (!options.IsEnable)
            {
                return(services);
            }

            // Update File Content base on Configuration

            SwaggerHelper.UpdateApiDocFileContent(options.Title, SwaggerHelper.GetSwaggerEndpoint(options), options.AuthTokenType, options.JsonViewerUrl);

            SwaggerHelper.UpdateApiJsonViewerFileContent(options.Title);

            // Config Swagger
            services.AddSwaggerGen(swaggerGenOptions =>
            {
                swaggerGenOptions.AddElectSwaggerGenOptions(configuration);

                options.ExtendOptions?.Invoke(swaggerGenOptions, options);
            });

            return(services);
        }
Exemplo n.º 2
0
        public static IApplicationBuilder UseElectSwagger(this IApplicationBuilder app)
        {
            var options = app.ApplicationServices.GetService <IOptions <ElectSwaggerOptions> >().Value;

            app.UseSwagger(c =>
            {
                c.RouteTemplate = options.SwaggerRoutePrefix + "/{documentName}/" + options.SwaggerName;

                c.PreSerializeFilters.Add((swaggerDoc, httpReq) => swaggerDoc.Host = httpReq.Host.Value);
            });

            app.UseSwaggerUI(c =>
            {
                // RoutePrefix of Swagger UI must not start by / or ~

                c.RoutePrefix = options.SwaggerRoutePrefix;

                c.SwaggerEndpoint(SwaggerHelper.GetSwaggerEndpoint(options), options.AccessKey);
            });

            // Path and GZip for Statics Content
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Bootstrapper.Instance.WorkingFolder),

                RequestPath = ElectSwaggerConstants.AssetsUrl,

                OnPrepareResponse = context =>
                {
                    var headers = context.Context.Response.GetTypedHeaders();

                    headers.CacheControl = new CacheControlHeaderValue
                    {
                        MaxAge = new TimeSpan(365, 0, 0, 0)
                    };
                }
            });

            app.UseMiddleware <ElectSwaggerMiddleware>();

            return(app);
        }
Exemplo n.º 3
0
        public static IApplicationBuilder UseElectSwagger(this IApplicationBuilder app)
        {
            var options = app.ApplicationServices.GetService <IOptions <ElectSwaggerOptions> >().Value;

            if (!options.IsEnable)
            {
                return(app);
            }

            app.UseSwagger(c =>
            {
                c.RouteTemplate = options.SwaggerRoutePrefix + "/{documentName}/" + options.SwaggerName;

                c.PreSerializeFilters.Add((swaggerDoc, httpReq) =>
                {
                    var host = httpReq.Host.Host;

                    if (httpReq.Headers.TryGetValue("X-Forwarded-Host", out var forwardedHost))
                    {
                        host = forwardedHost;
                    }

                    var port = httpReq.Host.Port;

                    if (httpReq.Headers.TryGetValue("X-Forwarded-Port", out var forwardedPort))
                    {
                        if (int.TryParse(forwardedPort, out var forwardedPortInt))
                        {
                            port = forwardedPortInt;
                        }
                    }

                    swaggerDoc.Host = host + (port.HasValue ? $":{port.Value}" : string.Empty);
                });
            });

            app.UseSwaggerUI(c =>
            {
                // RoutePrefix of Swagger UI must not start by / or ~

                c.RoutePrefix = options.SwaggerRoutePrefix;

                c.SwaggerEndpoint(SwaggerHelper.GetSwaggerEndpoint(options), options.AccessKey);
            });

            // Path and GZip for Statics Content
            app.UseStaticFiles(new StaticFileOptions
            {
                FileProvider = new PhysicalFileProvider(Bootstrapper.Instance.WorkingFolder),

                RequestPath = ElectSwaggerConstants.AssetsUrl,

                OnPrepareResponse = context =>
                {
                    var headers = context.Context.Response.GetTypedHeaders();

                    headers.CacheControl = new CacheControlHeaderValue
                    {
                        MaxAge = new TimeSpan(365, 0, 0, 0)
                    };
                }
            });

            app.UseMiddleware <ElectSwaggerMiddleware>();

            return(app);
        }