对于应用程序的Heroku API子,这可能吗? [英] API subdomain for Heroku app, is it possible?

查看:113
本文介绍了对于应用程序的Heroku API子,这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个API,我很担心,我所有的资源,要么无法与api.myapp.com域或访问他们将活用错误的URI。

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.

我添加了CNAME我的域名指向我的Heroku应用程序。
(例如:浏览到www.myapp.com带您到 https://myherokuapp.heroku.com

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)

我想建立一个API子域,使A到
https://api.myapp.com 您需要为https://myherokuapp.heroku.com/api/v1

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

最好的情况是,一个POST为 https://api.myapp.com/accounts/12345 将创建一个新帐户。是,即使可能吗?

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

(我知道,子域(如:mysubdomain.myappname.heroku.com)是不可能的Heroku的)

(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. 事做的DNS提供商转发CONFIGS(也许
    是与A记录)。

  2. 的东西在Heroku的配置,可能是一个付费的附加处理域/子域。

  3. 处理我的应用程序内的所有子域。

任何想法?

推荐答案

如果你想之间api.mydomain.com和www.mydomain.com,并有不同的控制器为您的API请求,那么你可以肯定使用Rails的路线约束分化你的API子域来处理这个

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

然后它会使用posts_controller.rb在app /控制器/ API应用程序的文件夹中。

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

然后您就同时拥有www.mydomain.com和api.mydomain.com添加自定义域为您的应用程序,然后路由将采取其他的事情。

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.

您可能还需要寻找到了葡萄宝石帮助建立你的API

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

这篇关于对于应用程序的Heroku API子,这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆