使用 ruby​​ 中的单个代码库的多个应用程序

发布于 2024-11-19 11:16:28 字数 1488 浏览 3 评论 0原文

我试图合理地理解如何在 ruby​​/rack(或者更理想的是现有框架)之上构建一个应用程序来管理类似于 WordPress 的东西。具体来说,能够从同一代码库为多个站点提供服务,每个站点都有自己的功能和配置。

例如,假设:

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

这个测试用例似乎有效,包括在生产环境中:

# 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
}

然而,到目前为止,我主要在几乎没有任何状态的环境中工作,但是,我有点紧张,因为这可能会回来并在路上咬我。

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

并且,是否有任何周围的应用程序允许开箱即用地执行此操作?

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


这两个线程例证了我担心的事情:

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.

Suppose, for instance:

  • example.com using auth, pages, blog modules
  • forum.example.com -> auth, forum modules
  • api.example.com -> auth, api modules

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?

(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:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻泪 2024-11-26 11:16:28

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

如果您希望一个应用程序中包含所有模块,那么您可以拥有一个带有通配符子域的虚拟主机,并使用 Rails 应用程序中的路由通过子域路由到应用程序的不同部分。这非常适合 Engine 架构。

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

编辑 - 我的回答是专门讨论Rails的,而你的问题更通用。

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.

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.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文