gay*_*vat 11 ruby ruby-on-rails eventmachine goliath
我正在尝试用Goliath框架创建异步api.服务应该写入mysql,向RabbitMQ添加消息并接收响应.还应该有一个使用Rails构建的单独管理应用程序.我有几个问题:
有没有办法在Rails和Goliath之间有效地共享模型?使用Activerecord或任何其他orm与em有什么问题吗?是否有最佳实践,配置(连接池大小,驱动程序)或其他选项?我必须使用什么来接收来自AMQP的消息?是否可以更好地构建一个单独的eventmachine守护进程,或者我可以以某种方式使用Goliath的那个东西?谢谢你提前.
这是在Goliath中使用ActiveRecord模型的快速入侵.使用此方法,您可以在不使用require的情况下使用模型,但在模型级别上没有关系.为了获得has_many和belongs_to关系(在这种方法中),我将加载模型文件并在下面的类定义循环中包含包含这些单词的行.
require 'goliath'
require 'active_record'
require 'active_support'
# The location of the Rails app to integrate
RAILS_APP ||= ENV['HOME']+"/dev/qtrack"
# Load the ActiveRecord database configuration, development settings
configpath = File.join(RAILS_APP, "config", "database.yml")
config = YAML::load_file(configpath)
ActiveRecord::Base.establish_connection config["development"]
# Set the names of all Rails models to a constant
MODELS ||= []
models_dir = File.join(RAILS_APP, "app", "models")
model_names = Dir[models_dir+"/*.rb"]
# Loop over each file name, define a class for each
model_names.each do |fname|
mname = File.basename(fname, '.rb').titleize.sub(/ /, '')
eval %Q{
class ::#{mname} < ActiveRecord::Base
end
}
m = mname.constantize
MODELS << m unless MODELS.include?(m)
end
class Hello < Goliath::API
# default to JSON output, allow Yaml as secondary
use Goliath::Rack::Render, ['json', 'yaml']
def response(env)
# Create a Hash with each model name and the object count
models = MODELS.inject({}) {|hsh,model| hsh[model] = model.count; hsh }
[200, {}, models.to_json ]
end
end
Run Code Online (Sandbox Code Playgroud)
这是基于您的反馈的黑客攻击.