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

查看:26
本文介绍了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 子域,以便 GET 到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

最好的情况是发布到 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 提供商转发配置有关(也许与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

然后将使用应用程序的 app/controllers/api 文件夹中的 posts_controller.rb.

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.

您可能还想查看 Grape Gem 以帮助构建您的 api

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

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

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