在ruby中使用单个代码库的多个应用程序 [英] Multiple applications using a single code base in ruby

查看:56
本文介绍了在ruby中使用单个代码库的多个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图对如何在可管理WordPress的东西的ruby/rack(或更理想的是现有框架)之上构建应用程序有一个合理的了解.特别是,它具有从同一代码库为多个站点提供服务的能力,每个站点都有其自己的功能和配置.

I'm trying to get a reasonable understanding of how one can build an app on top of ruby/rack (or even more ideally, an existing framework) that manages something equivalent to WordPress. Specifically, with the ability to serve multiple sites from the same code base, each with its own features and configuration.

例如,假设:

  • example.com使用身份验证,页面,博客模块
  • forum.example.com->身份验证,论坛模块
  • api.example.com->身份验证,api模块

此测试用例似乎可以正常运行,包括在生产环境中使用:

This test case seems to work, including in a production environment:

# test.rb
class Foo
end

# config.ru
require 'rack'

use Rack::ShowExceptions
use Rack::CommonLogger

run lambda { |env|
  case env['HTTP_HOST']
  when /^test\./
    require './test'
    # answers true, regardless of subdomain loaded first
    [200, {'Content-Type'=>'text/plain'}, "#{Kernel.const_defined? :Foo}"]
  else
    # answers false, regardless of subdomain loaded first
    [200, {'Content-Type'=>'text/plain'}, "#{Kernel.const_defined? :Foo}"]
  end
}

到目前为止,大多数情况下都在几乎没有任何状态的环境中工作,但是,我有点担心这种情况可能会重新出现,并在以后给我带来麻烦.

Having mostly worked in environments with little if any state until now, however, I'm a bit nervous that this might come back and bite me down the road.

无论如何,我想念了什么/我应该在哪里期待它咬我? (由于文件重新加载而导致的性能?在适当的情况下需要重新初始化数据库连接池?会话在不同的域之间无效共享?等等.除了显而易见的事实,任何缓存作为静态文件都是不合适的.)

At any rate, what did I miss/where should I expect it to come back and bite me? (Performance due to file reloads? DB connection pools that need to be re-initialized if appropriate? Sessions being invalidly shared across different domains? etc. besides the obvious fact that any caching as static files will be inappropriate.)

而且,周围有没有可以立即使用此功能的应用程序?

And, is there any app around that allows to do this out of the box?

(我对Rails的最初印象是它不适合这种用例.也许是错误的.我遇到的唯一的多站点插件是允许example.com/site1、example.com/site2等. )

(My initial impression with Rails was that it won't fit for such a use-case. Perhaps wrongly. The only multisite plugin I ran into was to allow example.com/site1, example.com/site2, etc.)

这两个线程体现了我担心的事情:

These two threads exemplify what I'm worried about:

Rails-具有自定义框架的多租户应用程序

推荐答案

我认为您可能使情况变得有些复杂.您可以使用Web服务器配置轻松地将不同的子域指向不同的Rails应用程序.例如,在Nginx中,您只需创建不同的虚拟主机.

I think you've probably overcomplicated the situation somewhat. You can easily point different subdomains to different Rails applications using your web server configuration. For example in Nginx, you'd simply create different virtual hosts.

如果要将所有模块包含在一个应用程序中,则可以有一个带有通配符子域的虚拟主机,并使用Rails应用程序中的路由通过子域路由到应用程序的不同部分.这将非常适合 Engine 体系结构.

If you want all the modules contained in one application, then you can have a single virtual host with a wildcard subdomain, and use the routing in your Rails app to route via subdomain to different parts of your app. This would lend itself very well to an Engine architecture.

关于数据库,在第一个示例中根本没有问题,因为不同的应用程序可以处理自己的数据库连接.对于引擎示例,引擎表通常位于同一数据库中,但具有名称空间.

With regards databases, in the first example there's no problem at all as the different apps can handle their own database connections. With the engine example, typically engines tables would be in the same database but namespaced.

编辑-我的回答是关于Rails的,而您的问题更为笼统.

Edit - my answer is specifically talking about Rails, whereas your question was more generic.

这篇关于在ruby中使用单个代码库的多个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆