API subdomain for Heroku app, is it possible?

雨燕双飞 提交于 2019-11-29 19:45:05

问题


I am trying to build an API and I am concerned that all my resources will either not be accessible with the api.myapp.com domain or that they will "live" with the wrong uris.

I have added the CNAME for my domain name to point to my Heroku app. (ex: browsing to www.myapp.com takes you to https://myherokuapp.heroku.com)

I would like to set up an API subdomain, so that a GET to https://api.myapp.com takes you to https://myherokuapp.heroku.com/api/v1

The best scenario would be that a POST to https://api.myapp.com/accounts/12345 would create a new account. Is that even possible?

(I know that subdomains (eg: mysubdomain.myappname.heroku.com) are not possible with Heroku)

I believe the answer could be in three different places:

  1. Something to do with DNS provider forwarding configs (maybe something to do with "A" records).
  2. Something to config in Heroku, possibly a paid add-on to handle domains/subdomains.
  3. Handle all subdomains within my app.

回答1:


If you want to differentiate between api.mydomain.com and www.mydomain.com and have different controllers for your API requests then you could certainly use Rails routes constrained to your api subdomain to handle this

constraints :subdomain => "api" do
  scope :module => "api", :as => "api" do
   resources :posts
  end
end

which would then use the posts_controller.rb in the app/controllers/api folder of your application.

You'll then have both www.mydomain.com and api.mydomain.com added a custom domains for your application and then the routes will take care of the rest.

You might also want to look into the Grape Gem for helping build your api



来源:https://stackoverflow.com/questions/15160328/api-subdomain-for-heroku-app-is-it-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!