Rails Grape-Swagger: get current domain for base_path

I have the following Grape root class definition

module Api class Root < Grape::API mount Api::Version1 add_swagger_documentation :mount_path => 'docs', :base_path => "/api", :markdown => true, :hide_documentation_path => true end end 

The problem is that ": base_path" should contain the full url, and I don't know where to get it. I checked the ENV variable and there is nothing close to the current domain. There is also no "request" object. Is there any way in Ruby, I can just get the current domain, i.e. HTTP_HOST variable?

+4
source share
2 answers

Well, for those who have been wandering here after all these years (like me), is a solution that worked for me:

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

Basically, you just need to pass a proc, which gives an instance of Grape::Request , from which you can get the host and port data.

+5
source

I had decent luck using http://localhost:9292/api . I think another alternative would be to save these parameters in the yml configuration file, and you could set this based on the value of RACK_ENV.

0
source

Source: https://habr.com/ru/post/1441644/


All Articles