【发布时间】:2017-05-14 00:22:55
【问题描述】:
我有一个名为 app.rb 的文件,当我在本地机器上运行它时,它会运行一个服务器。我想将 app.rb 部署到 Heroku,以便服务器将在 Heroku 上运行。我想我需要在 routes.rb 指向它。我该怎么做呢?
这是 config.ru:
require_relative 'config/environment'
require '/.app.rb'
run Rails.application
run Sinatra::Application
这是 app.rb 中的 Web 服务器代码,来自代码路径指南 (https://guides.codepath.com/android/Google-Cloud-Messaging#step-3-setup-web-server):
require 'sinatra'
require 'rest-client'
require 'sequel'
# Create a SQLite3 database
DB = Sequel.connect('sqlite://gcm-test.db')
# Create a Device table if it doesn't exist
DB.create_table? :Device do
primary_key :reg_id
String :user_id
String :reg_token
String :os, :default => 'android'
end
Device = DB[:Device] # create the dataset
# Registration endpoint mapping reg_token to user_id
# POST /register?reg_token=abc&user_id=123
post '/register' do
if Device.filter(:reg_token => params[:reg_token]).count == 0
device = Device.insert(:reg_token => params[:reg_token], :user_id => params[:user_id], :os => 'android')
end
end
# Ennpoint for sending a message to a user
# POST /send?user_id=123&title=hello&body=message
post '/send' do
# Find devices with the corresponding reg_tokens
reg_tokens = Device.filter(:user_id => params[:user_id]).map(:reg_token).to_a
if reg_tokens.count != 0
send_gcm_message(params[:title], params[:body], reg_tokens)
end
end
# Sending logic
# send_gcm_message(["abc", "cdf"])
def send_gcm_message(title, body, reg_tokens)
# Construct JSON payload
post_args = {
# :to field can also be used if there is only 1 reg token to send
:registration_ids => reg_tokens,
:data => {
:title => title,
:body => body,
:anything => "foobar"
}
}
# Send the request with JSON args and headers
RestClient.post 'https://gcm-http.googleapis.com/gcm/send', post_args.to_json,
:Authorization => 'key=' + AUTHORIZE_KEY, :content_type => :json, :accept => :json
end
这是过程文件:
web: bundle exec puma -C config/puma.rb
当我按照 Heroku 上的 Ruby 入门示例进行操作时,我可以看到示例网页。但是,如果我使用“run Sinatra::Application”编辑 config.ru 文件,然后部署到 heroku,它将无法再显示示例网页,而只会显示“未找到”
【问题讨论】:
-
您能否提供更多详细信息?
app.rb是否为应用服务器定义了 Rack 应用?你能粘贴你的config.ru(如果你有的话)吗? -
require_relative 'config/environment' require '/.app.rb' 运行 Rails.application 运行 Sinatra::Application
-
好的,谢谢,我已经输入了 app.rb 和 config.ru 代码。我不确定 Rack 应用程序是什么。该代码是一个接收和发送推送通知的服务器。
-
谢谢。你有
Procfile吗?如果是这样,请也发布其内容。 -
好的,谢谢,我发布了 Procfile
标签: ruby heroku sinatra heroku-toolbelt