在Heroku / AWS Elastic Beanstalk上部署Rails API

时间:2022-08-25 09:01:19

I'm trying to pair a mobile client with a REST API so that I get JSON data which I can display in the app. I'm following Apis on Rails by Abraham Kuri Vargas. In the API I have created a subdomain as well as some versioning, so the controller files are in app/controllers/api/v1. The API returns desired JSON data when I run it locally. The problem I'm facing is whenever I deploy the API onto Heroku/AWS it will show "Application not found". Is it that the remote server isn't finding its way into app/controllers/api/v1? Is there any fix for a problem like this because even in the book the author has written "Due to the structure of the application we are not going to deploy the app to any server". I'm really stuck on this problem for a long time and would love any suggestions!

我正在尝试将移动客户端与REST API配对,以便获取可以在应用程序中显示的JSON数据。亚伯拉罕库里巴尔加斯跟随阿皮斯在铁路上。在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

Output, when I run localhosts://3000/users/1 locally

输出,当我在本地运行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"}

Heroku server logs when I run petoye.herokuapp.com/users/1

当我运行petoye.herokuapp.com/users/1时,Heroku服务器会记录

Started GET "/users/1" for 120.60.22.244 at 2016-06-12 06:43:36 +0000
ActionController::RoutingError (No route matches [GET] "/users/1"):
vendor/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'

在2016-06-12 06:43:36开始获取“/ users / 1”为120.60.22.244 +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:在调用'vendor / bundle / ruby​​ / 2.2.0 / gems / railties-4.2.6 / lib / rails / rack /logger.rb:38:incall_app'porpers / bundle / ruby​​ / 2.2.0 / gems / activesupport-4.2.6 / lib / active_support / tagged_logging.rb:68:在标记为'vendor / bundle / ruby​​ / 2.2的块中。 0 / gems / activesupport-4.2.6 / lib / active_support / tagged_logging.rb:68:进入'vendor / bundle / ruby​​ / 2.2.0 / gems / railties-4.2.6 / lib / rails / rack / logger.rb: 20:在调用'vendor / bundle / ruby​​ / 2.2.0 / gems / rack-1.6.4 / lib / rack / methodoverride.rb:22:incall'proforts / 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:incall'proforts / bundle / ruby​​ / 2.2.0 / gems / puma-3.4.0 / lib / puma / server。 rb:569:在handle_request'forser / bundle / ruby​​ / 2.2.0 / gems / puma-3.4.0 / lib / puma / thread_pool.rb:114:inblock in spawn_thread'

2 个解决方案

#1


2  

Steps on how to configure Heroku for a Rails API

有关如何为Rails API配置Heroku的步骤

  1. First you need to buy a domain using any registrar, say you buy www.example.com, so you can use a subdomain like api.example.com. Once that's done you need to tell heroku to listen to requests at api.example.com. This can be done easily by typing this on your cmd

    首先,您需要使用任何注册商购买域名,比如说您购买www.example.com,因此您可以使用api.example.com等子域名。完成后,您需要告诉heroku在api.example.com上收听请求。这可以通过在cmd上键入来轻松完成

    heroku domains:add api.example.com
    
  2. After using the domains:add command you must point your DNS provider at the domain’s DNS Target supplied by Heroku. This is done by creating a CNAME entry to something like example.herokuapp.com (type heroku info to get the name of your app). The above process is made simple if you use a service like DNSimple (https://www.youtube.com/watch?v=rr6Jy9i0Cws). Basically you add a CNAME record of api.example.com which points to example.herokuapp.com

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

  3. One important thing everyone forgets once they deploy to heroku is to run the heroku run rake db:migrate command.

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

Once all this is done you would be able to access the api using api.example.com/any_endpoints

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

Additional resources: https://devcenter.heroku.com/articles/custom-domains https://support.dnsimple.com/articles/alias-record/

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

#2


3  

The problem with heroku is that if you don't pay for a domain, and just use the base service, it will assign a subdomain for your app. So all I needed to do was to redirect heroku to a custom url such as api.any_domain.com.

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

An easier alternative would be to override the constraints for the api on the routes.rb file:

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

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

You will now be able to see the API under http://herokuapp.com/api/any_endpoint

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

#1


2  

Steps on how to configure Heroku for a Rails API

有关如何为Rails API配置Heroku的步骤

  1. First you need to buy a domain using any registrar, say you buy www.example.com, so you can use a subdomain like api.example.com. Once that's done you need to tell heroku to listen to requests at api.example.com. This can be done easily by typing this on your cmd

    首先,您需要使用任何注册商购买域名,比如说您购买www.example.com,因此您可以使用api.example.com等子域名。完成后,您需要告诉heroku在api.example.com上收听请求。这可以通过在cmd上键入来轻松完成

    heroku domains:add api.example.com
    
  2. After using the domains:add command you must point your DNS provider at the domain’s DNS Target supplied by Heroku. This is done by creating a CNAME entry to something like example.herokuapp.com (type heroku info to get the name of your app). The above process is made simple if you use a service like DNSimple (https://www.youtube.com/watch?v=rr6Jy9i0Cws). Basically you add a CNAME record of api.example.com which points to example.herokuapp.com

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

  3. One important thing everyone forgets once they deploy to heroku is to run the heroku run rake db:migrate command.

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

Once all this is done you would be able to access the api using api.example.com/any_endpoints

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

Additional resources: https://devcenter.heroku.com/articles/custom-domains https://support.dnsimple.com/articles/alias-record/

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

#2


3  

The problem with heroku is that if you don't pay for a domain, and just use the base service, it will assign a subdomain for your app. So all I needed to do was to redirect heroku to a custom url such as api.any_domain.com.

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

An easier alternative would be to override the constraints for the api on the routes.rb file:

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

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

You will now be able to see the API under http://herokuapp.com/api/any_endpoint

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