ruby-on-rails – Rails应用程序和goliath api和数据库/模型共享

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – Rails应用程序和goliath api和数据库/模型共享前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图用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

这是一个基于您的反馈的黑客.

原文链接:https://www.f2er.com/ruby/266591.html

猜你在找的Ruby相关文章