johnnygoodman
johnnygoodman

Reputation: 475

Force Swagger UI To Load https path when hosted on Heroku

I have a rails 4 app with a Grape API and Swagger through the gem grape-swagger and grape-swagger-ui gems.

In dev everything works well, I load http://localhost:3000/api/swagger and the swagger header's text input along the top loads the expected url, http://localhost:3000/api/swagger_doc. This points properly to the file it seeks, swagger_doc.json.

I've pushed this app to heroku, which forces https connections. Unfortunately, when loading https://my-app.herokuapp.com/api/swagger the swagger header's text input along the top loads http://my-app.herokuapp.com/api/swagger_doc instead of loading https://my-app.herokuapp.com/api/swagger_doc (http vs https).

I've tried coming at this from the heroku side with things like:

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/environments/production

config.force_ssl = false

config/environments/production

#config.force_ssl = false

And I've come at it with trying to set or manipulate the base_path attribute of add_swagger_documentation.

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}" }

I recently clicked "view raw" on one of my resources and noticed that it was picking up my changes to base_path but that base_path isn't even used to populate the url in the text input in the swagger header. It seems to be generated from a js file. I'm unable to edit it and would happily accept a hack to do so as a solution. Here's that raw output:

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

The line of interest is:

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

Which would break the app if it was being populated and used, but it is not. I don't see an option in the grape-swagger gem that I can use to pass in this variable and change the path to https.

In conclusion:

I'd like the swagger text input box to load https://my-app.herokuapp.com/api/swagger_doc when I visit https://my-app.herokuapp.com/api/swagger.

Anyone know a hack to accomplish this on heroku?

Upvotes: 0

Views: 1862

Answers (1)

johnnygoodman
johnnygoodman

Reputation: 475

I was able to work around this. I suggest:

  1. Do not use + uninstall #gem 'grape-swagger-ui'

  2. Use and install gem 'grape-swagger-rails' and follow the docs here: https://github.com/ruby-grape/grape-swagger-rails

Upvotes: 1

Related Questions