简体   繁体   English

Rails应用程序和goliath api和数据库/模型共享

[英]Rails app and goliath api and database/models sharing

I'm trying to create async api with Goliath framework. 我正在尝试用Goliath框架创建异步api。 Service should write to mysql, add messages to RabbitMQ and receive responses back. 服务应该写入mysql,向RabbitMQ添加消息并接收响应。 There also should be a separate admin application built with Rails. 还应该有一个使用Rails构建的单独管理应用程序。 I have several questions about that: 我有几个问题:

Is there a way to effectively share models between Rails and Goliath? 有没有办法在Rails和Goliath之间有效地共享模型? Is there any problems to use Activerecord or any other orm with em? 使用Activerecord或任何其他orm与em有什么问题吗? Are there any best practices, configuration (connection pool size, driver) or other options about that? 是否有最佳实践,配置(连接池大小,驱动程序)或其他选项? What i have to use to receive messages from AMQP? 我必须使用什么来接收来自AMQP的消息? Would it better to build a separate eventmachine daemon or i can use somehow Goliath's one for that stuff? 是否可以更好地构建一个单独的eventmachine守护进程,或者我可以以某种方式使用Goliath的那个东西? Thanks for advance. 谢谢你提前。

Here's a quick hack to use ActiveRecord models in Goliath. 这是在Goliath中使用ActiveRecord模型的快速入侵。 With this approach you can use the model without using require, but you don't have the relationships on the model level. 使用此方法,您可以在不使用require的情况下使用模型,但在模型级别上没有关系。 To get the has_many and belongs_to relationships (in this approach), I would load the model file and include the lines containing such words in the class definition loop below. 为了获得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

This is a hack based on your feedback. 这是基于您的反馈的黑客攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM