/// <inheritdoc />
        public ISwaggerUI AddServer(HttpRequest req, string routePrefix, IOpenApiConfigurationOptions options = null)
        {
            var prefix  = string.IsNullOrWhiteSpace(routePrefix) ? string.Empty : $"/{routePrefix}";
            var baseUrl = $"{req.Scheme}://{req.Host}{prefix}";

            if (options.IsNullOrDefault())
            {
                this._baseUrl = baseUrl;

                return(this);
            }

            var server = new OpenApiServer {
                Url = baseUrl
            };
            // Filters out the existing base URLs that are the same as the current host URL.
            var servers = options.Servers
                          .Where(p => p.Url.TrimEnd('/') != baseUrl.TrimEnd('/'))
                          .ToList();

            servers.Insert(0, server);

            this._baseUrl = servers.First().Url;

            return(this);
        }
Ejemplo n.º 2
0
        /// <inheritdoc />
        public IDocument AddServer(IHttpRequestDataObject req, string routePrefix, IOpenApiConfigurationOptions options = null)
        {
            this._req = req;

            var prefix  = string.IsNullOrWhiteSpace(routePrefix) ? string.Empty : $"/{routePrefix}";
            var baseUrl = $"{this._req.GetScheme(options)}://{this._req.Host}{prefix}";

            var server = new OpenApiServer {
                Url = baseUrl
            };

            if (options.IsNullOrDefault())
            {
                this.OpenApiDocument.Servers = new List <OpenApiServer>()
                {
                    server
                };

                return(this);
            }

            // Filters out the existing base URLs that are the same as the current host URL.
            var servers = options.Servers
                          .Where(p => p.Url.TrimEnd('/') != baseUrl.TrimEnd('/'))
                          .ToList();

            if (!servers.Any())
            {
                servers.Insert(0, server);
            }

            if (options.IncludeRequestingHostName &&
                !servers.Any(p => p.Url.TrimEnd('/') == baseUrl.TrimEnd('/')))
            {
                servers.Insert(0, server);
            }

            this.OpenApiDocument.Servers = servers;

            return(this);
        }
        /// <summary>
        /// Gets the scheme whether to return either HTTP or HTTPS depending on the condition.
        /// /// </summary>
        /// <param name="req"><see cref="IHttpRequestDataObject"/> instance.</param>
        /// <param name="options"><see cref="IOpenApiConfigurationOptions"/> instance.</param>
        /// <returns>Return either "HTTP" or "HTTPS", depending on the condition.</returns>
        public static string GetScheme(this IHttpRequestDataObject req, IOpenApiConfigurationOptions options)
        {
            req.ThrowIfNullOrDefault();

            if (options.IsNullOrDefault())
            {
                return(req.Scheme);
            }

            if (options.ForceHttps)
            {
                return(HTTPS);
            }

            if (options.ForceHttp)
            {
                return(HTTP);
            }

            return(req.Scheme);
        }