【问题标题】:Deploying a Rails API on either Heroku/AWS Elastic Beanstalk在 Heroku/AWS Elastic Beanstalk 上部署 Rails API
【发布时间】:2016-10-12 08:53:24
【问题描述】:

我正在尝试将移动客户端与 REST API 配对,以便获得可以在应用程序中显示的 JSON 数据。我正在关注 Abraham Kuri Vargas 的 Apis on Rails。在 API 中,我创建了一个子域以及一些版本控制,因此控制器文件位于 app/controllers/api/v1 中。当我在本地运行 API 时,它会返回所需的 JSON 数据。我面临的问题是,每当我将 API 部署到 Heroku/AWS 上时,它都会显示“找不到应用程序”。是远程服务器没有找到进入 app/controllers/api/v1 的方式吗?是否有任何解决方案可以解决此类问题,因为即使在书中作者也写过“由于应用程序的结构,我们不会将应用程序部署到任何服务器”。我真的被这个问题困扰了很长时间,并且希望有任何建议!

app/controllers/api/v1/users_controller.rb

    class Api::V1::UsersController < ApplicationController
     respond_to :json

     def show
      respond_with User.find(params[:id])
     end

     def create
     user = User.new(user_params)
      if user.save
       render json: user, status: 201, location: [ :api,user ]
      else
       render json: { errors: user.errors }, status: 422
      end
     end

     def update
     user = User.find(params[:id])
      if user.update(user_params)
       render json: user, status: 200, location: [ :api,user ]
      else
       render json: { errors: user.errors }, status: 422
      end
     end

     def destroy
      user = User.find(params[:id])
      user.destroy
      head 204
     end

     private
      def user_params
       params.require(:user).permit(:email,:password,:password_confirmation)
      end
    end

config/routes.rb

    require 'api_constraints'

    Rails.application.routes.draw do

      devise_for :users
      namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
        scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
          #resources 
          resources :users, :only => [:show, :create, :update, :destroy]
        end 
      end
    end

输出,当我在本地运行 localhosts://3000/users/1 时

{"id":1,"email":"example@marketplace.com","created_at":"2016-06-09T05:36:06.606Z","updated_at":"2016-06-09T05: 36:06.606Z"}

我运行 petoye.herokuapp.com/users/1 时的 Heroku 服务器日志

于 2016 年 6 月 12 日 06:43:36 +0000 开始为 120.60.22.244 获取“/users/1”
ActionController::RoutingError (没有路由匹配 [GET] "/users/1"):
供应商/bundle/ruby/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/debug_exceptions.rb:21:in call'
vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:38:in
call_app'
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in block in tagged'
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in
tagged' vendor/bundle/ruby/2.2.0/gems/railties-4.2。 6/lib/rails/rack/logger.rb:20:in call'
vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in
call'
vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in call'
2016-06-12T06:43:36.946702+00:00 app[web.1]: vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in
call'
vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/server.rb:569:in handle_request'
vendor/bundle/ruby/2.2.0/gems/puma-3.4.0/lib/puma/thread_pool.rb:114:in
block in spawn_thread'

【问题讨论】:

  • 您可以根据您的请求从服务器发布日志吗?
  • 用服务器日志更新了帖子。你会看看吗?谢谢。我注意到的一件事是,即使我在本地使用 puma 运行相同的东西,我也会得到相同的错误。但是,当我使用 WEBrick 时没有问题。
  • 是的,您的 puma 服务器似乎无法运行
  • 我不认为 puma 服务器用于托管 Rails API。在使用任何 Rails 应用程序时都可以正常工作。
  • 应该没问题。这可能会有所帮助devcenter.heroku.com/articles/…

标签: ruby-on-rails json heroku puma rails-api


【解决方案1】:

如何为 Rails API 配置 Heroku 的步骤

  1. 首先,您需要使用任何注册商购买一个域,例如您购买 www.example.com,这样您就可以使用像 api.example.com 这样的子域。完成后,您需要告诉 heroku 在 api.example.com 上收听请求。这可以通过在你的 cmd 上输入来轻松完成

    heroku domains:add api.example.com
    
  2. 使用 domain:add 命令后,您必须将 DNS 提供商指向 Heroku 提供的域的 DNS 目标。这是通过为 example.herokuapp.com 之类的东西创建一个 CNAME 条目来完成的(输入 heroku info 以获取您的应用程序的名称)。如果您使用 DNSimple (https://www.youtube.com/watch?v=rr6Jy9i0Cws) 之类的服务,上述过程会变得简单。基本上,您添加指向 example.herokuapp.com 的 api.example.com 的 CNAME 记录

  3. 一旦部署到 heroku,每个人都会忘记的一件重要事情是运行 heroku run rake db:migrate 命令。

完成所有这些后,您就可以使用 api.example.com/any_endpoints 访问 api

其他资源: https://devcenter.heroku.com/articles/custom-domains https://support.dnsimple.com/articles/alias-record/

【讨论】:

    【解决方案2】:

    heroku 的问题在于,如果您不为域付费,而只使用基础服务,它会为您的应用分配一个子域。所以我需要做的就是将heroku重定向到一个自定义的url,比如api.any_domain.com。

    更简单的替代方法是覆盖 routes.rb 文件中 api 的约束:

      namespace :api, defaults: { format: :json }, path: '/api'  do
    

    您现在可以在 http://herokuapp.com/api/any_endpoint 下看到 API

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2020-08-24
      • 1970-01-01
      • 2012-09-24
      • 2016-09-18
      • 2015-09-20
      • 2015-07-06
      • 2020-08-02
      • 2014-12-25
      相关资源
      最近更新 更多