【问题标题】:Force Swagger UI To Load https path when hosted on Heroku在 Heroku 上托管时强制 Swagger UI 加载 https 路径
【发布时间】:2016-03-02 06:19:05
【问题描述】:

我有一个带有 Grape API 和 Swagger 的 rails 4 应用程序,通过 gem grape-swagger 和 grape-swagger-ui gems。

在开发中一切正常,我加载了http://localhost:3000/api/swagger,顶部的大摇大摆标题的文本输入加载了预期的url,http://localhost:3000/api/swagger_doc。这正确地指向它寻找的文件,swagger_doc.json。

我已将此应用程序推送到 Heroku,它会强制使用 https 连接。不幸的是,当加载https://my-app.herokuapp.com/api/swagger 时,顶部的招摇标题文本输入加载http://my-app.herokuapp.com/api/swagger_doc 而不是加载https://my-app.herokuapp.com/api/swagger_doc(http 与 https)。

我已经尝试从 heroku 方面来解决这个问题,例如:

routes.rb

  unless Rails.env.development? 
    get "*path" => redirect("https://my-app.herokuapp.com%{path}"), :constraints => { :protocol => "http://" } 
    post "*path" => redirect("https://my-app.herokuapp.com%{path}"), :constraints => { :protocol => "http://" } 
  end

配置/环境/生产

config.force_ssl = false

配置/环境/生产

#config.force_ssl = false

我尝试设置或操作 add_swagger_documentation 的 base_path 属性。

app/controllers/api/base.rb

base_path: "my-app.herokuapp.com",

app/controllers/api/base.rb

base_path: "http://my-app.herokuapp.com",

app/controllers/api/base.rb

base_path: = lambda do |request|
               return "http://my-app.herokuapp.com"
             end

app/controllers/api/base.rb

base_path: lambda { |request| "http://#{request.host}:#{request.port}" }

我最近在我的一个资源上单击了“查看原始数据”,并注意到它正在获取我对 base_path 的更改,但该 base_path 甚至没有用于填充招摇标题中文本输入中的 url。它似乎是从 js 文件生成的。我无法对其进行编辑,并且很乐意接受 hack 作为解决方案。这是原始输出:

https://gist.github.com/johnnygoodman/5fd246765dc5236fb8c4

感兴趣的线是:

"basePath":"http://localhost:3000/my-app.herokuapp.com"

如果正在填充和使用它会破坏应用程序,但事实并非如此。我在grape-swagger gem 中没有看到可用于传递此变量并将路径更改为https 的选项。

总结:

我希望在访问 https://my-app.herokuapp.com/api/swagger 时加载 https://my-app.herokuapp.com/api/swagger_doc 的招摇文本输入框。

有人知道在heroku 上实现此目的的技巧吗?

【问题讨论】:

    标签: api ruby-on-rails-4 heroku swagger-ui grape


    【解决方案1】:

    我能够解决这个问题。我建议:

    1. 不要使用 + 卸载 #gem 'grape-swagger-ui'

    2. 使用和安装 gem 'grape-swagger-rails' 并按照此处的文档操作:https://github.com/ruby-grape/grape-swagger-rails

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-29
      • 2016-12-05
      • 2021-09-01
      • 1970-01-01
      • 2020-05-12
      • 2018-05-22
      • 2014-12-20
      • 2021-02-17
      相关资源
      最近更新 更多