在Heroku/AWS上部署Rails API Elastic Beanstalk

问题描述:

我正在尝试将移动客户端与REST API配对,以便我可以在应用中显示JSON数据。我正在关注亚伯拉罕库里巴尔加斯的Apis on Rails。在API中,我创建了一个子域以及一些版本控制,因此控制器文件位于app/controllers/api/v1中。当我在本地运行API时,API会返回所需的JSON数据。我面临的问题是每当我将API部署到Heroku/AWS时,它都会显示“找不到应用程序”。是否远程服务器没有找到进入app/controllers/api/v1的路径?是否有任何修复这样的问题,因为即使在作者写的书中“由于应用程序的结构,我们不会将应用程序部署到任何服务器”。我很长一段时间都在困扰这个问题,并且会喜欢任何建议!在Heroku/AWS上部署Rails API Elastic Beanstalk

应用程序/控制器/ 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 

配置/ 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 /用户/ 1本地

{“id”:1,“email”:“example @ marketpl ace.com“,”created_at“:”2016-06-09T05:36:06.606Z“,”updated_at“:”2016-06-09T05:36:06.606Z“}

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

入门使用 “/用户/ 1” 为120.60.22.244,在2016年6月12日6时43分36秒+0000
的ActionController :: RoutingError(无路由匹配[ GET]“/ users/1”):
vendor/bundle/ruby​​/2.2.0/gems/actionpack-4.2.6/lib/action_dispatch/middleware/debug_exceptions.rb:21:call'
vendor/bundle/ruby/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:38:in
call_app'
vendor /束/红宝石/ 2.2.0 /宝石/的ActiveSupport-4.2.6/LIB/active_support/tagged_log ging.rb:68:在block in tagged'
vendor/bundle/ruby/2.2.0/gems/activesupport-4.2.6/lib/active_support/tagged_logging.rb:68:in
上标记'vendor/bundle/ruby​​/2.2.0/gems/railties-4.2.6/lib/rails/rack/logger.rb:20:在call'
vendor/bundle/ruby/2.2.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in
中调用'
vendor/ruby/2.2.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:在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
调用'
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'

+0

您可以发布从服务器日志从你的要求? – user2954587

+0

用服务器日志更新了帖子。你会看看吗?谢谢。我注意到的一件事是,即使当我用puma在本地运行相同的东西时,我也会得到相同的确切错误。但是,当我使用WEBrick时,没有问题。 –

+0

是的,它看起来像你有麻烦让您的美洲狮服务器运行 – user2954587

步骤的API如何为Rails API

  1. 首先,你需要使用任何注册商购买域名配置的Heroku,说你购买www.example.com,因此您可以使用像api.example.com这样的子域名。一旦完成,您需要告诉heroku在api.example.com上收听请求。这可以很容易地在你的CMD

    heroku domains:add api.example.com 
    
  2. 打字时使用的域之后完成:add命令必须指向由Heroku的供应域的DNS目标DNS提供商。这是通过创建CNAME条目来完成的,例如example.herokuapp.com(键入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/

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

一种更简便的方法是覆盖约束上的routes.rb文件API:

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

现在,您将能够看到下http://herokuapp.com/api/any_endpoint