Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
409 views
in Technique[技术] by (71.8m points)

multi tenant - Multiple applications using a single code base in ruby

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...