ruby - 无法将我的简单 Sinatra 应用程序部署到 Heroku
问题描述
我的 Sinatra 应用程序非常简单。
配置.ru
require './hello_app'
run Sinatra::Application
宝石文件
source 'https://rubygems.org'
ruby "2.6.6"
gem 'sinatra'
hello_app.rb
require 'sinatra'
get '/' do
"Hello World!"
end
这个应用程序在本地工作。但是,当我尝试部署到 Heroku 时,出现以下错误:
2020-12-27T23:29:11.036031+00:00 heroku[web.1]: State changed from crashed to starting
2020-12-27T23:29:12.572800+00:00 heroku[web.1]: Starting process with command `APP_ENV=production bundle exec rackup -p "57643"`
2020-12-27T23:29:16.071115+00:00 app[web.1]: [heroku-exec] Starting
2020-12-27T23:29:16.663254+00:00 app[web.1]: bundler: failed to load command: rackup (/app/vendor/bundle/ruby/2.6.0/bin/rackup)
2020-12-27T23:29:16.663275+00:00 app[web.1]: Gem::Exception: can't find executable rackup for gem rack. rack is not currently included in the bundle, perhaps you meant to add it to your Gemfile?
2020-12-27T23:29:16.663276+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:374:in `block in replace_bin_path'
2020-12-27T23:29:16.663277+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/gems/bundler-2.1.4/lib/bundler/rubygems_integration.rb:402:in `block in replace_bin_path'
2020-12-27T23:29:16.663277+00:00 app[web.1]: /app/vendor/bundle/ruby/2.6.0/bin/rackup:23:in `<top (required)>'
2020-12-27T23:29:16.722478+00:00 heroku[web.1]: Process exited with status 1
2020-12-27T23:29:16.770208+00:00 heroku[web.1]: State changed from starting to crashed
我尝试了其他线程中提供的许多建议,例如添加 Procfile 等,但每次都会遇到相同的错误。我是一个新学习者,我已经走到了死胡同。关于我如何前进的任何想法?
解决方案
我刚刚遇到了一个类似的问题,现在已经解决了。我的应用程序称为生日(在 app.rb 中定义为def class Birthday < Sinatra::Base
)。
我有一个config.ru
文件说:
require_relative "./lib/app.rb"
run Birthday
我的 Gemfile 是:
source "https://rubygems.org"
ruby "2.7.2"
gem "sinatra"
...
gem "rack"
我不知道机架 gem 是否必要,我在尝试让应用程序在 Heroku 上运行时添加了它。ruby app.rb
我可以使用或在本地运行我的项目rackup config.ru
。但它在 Heroku 上运行不正确。因此,我在其他地方的答案中添加了一个 Procfile,说:
dev: bundle exec rackup
web: APP_ENV=production bundle exec rackup -p "$PORT"
您的 Heroku 进程报告:Starting process with command 'APP_ENV=production bundle exec rackup -p "57643"'
,所以这可能是一个类似的问题。
Procfile 没有工作,我的应用程序没有正确加载。在我的 Heroku 控制台中,我发现我可以为该项目选择两个测功机。如果我只启用
dev bundle exec rackup
该应用程序根本不会加载。如果我只启用
web APP_ENV=production bundle exec rackup -p "$PORT"
,页面加载但它只是说GET /
。
我突然想到我从来没有在任何地方设置过 APP_ENV。所以我从 Procfile 中删除了那部分。现在 Heroku dyno 列出了 `web bundle exec rackup -p "$PORT"。有效!
推荐阅读
- db2 - 无法将数据插入到 IDAA 服务器中的 AOT(仅加速表)表中
- android - 如何使用 volley 访问 json 数据?
- python - 在 Celery - Python 中的进程(工作者)之间共享数据的最佳解决方案是什么?
- php - 防止日志中的重复条目
- python-3.x - 评估功能没有返回正确答案?
- android - Android - 即时更改语言
- mysql - 如何在 CASE 中有 AS 别名
- shell - 通过 Jenkins 管道中的 shell 脚本运行 ansible playbook 时,回显输出被缓冲并且不会实时显示
- css - 在 Polymer 3 中使用共享样式模块的正确方法
- swift - 如何快速在侧菜单表中添加视图