Created
November 2, 2023 18:21
-
-
Save ouaziz/1f75ca8d1e332e37d03cfdb8afd5724f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Configure config/application.rb for Subdomain Handling: | |
# config/application.rb | |
module YourAppName | |
class Application < Rails::Application | |
config.middleware.use SubdomainMiddleware | |
# ... other configurations | |
end | |
end | |
2. Create a Custom Subdomain Middleware (lib/subdomain_middleware.rb): | |
Create a new file for your custom subdomain middleware and define it as follows: | |
# lib/subdomain_middleware.rb | |
class SubdomainMiddleware | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = ActionDispatch::Request.new(env) | |
subdomain = request.subdomain | |
if subdomain.present? | |
tenant = find_tenant_by_subdomain(subdomain) | |
if tenant | |
# You can put your logic here to switch users to their subdomain URLs | |
# For example, you can redirect or route them to the appropriate controllers and views | |
case subdomain | |
when tenant.subdomain | |
env['HTTP_HOST'] = "#{tenant.subdomain}.yourapp.com" | |
env['PATH_INFO'] = '/' # Change the path as needed | |
end | |
else | |
# Handle invalid subdomains or show an error page | |
return [404, { 'Content-Type' => 'text/html' }, ['Invalid subdomain']] | |
end | |
else | |
# Redirect to the root URL when no subdomain is present | |
return [302, { 'Location' => 'https://yourapp.com' }, []] | |
end | |
@app.call(env) | |
end | |
def find_tenant_by_subdomain(subdomain) | |
# Look up the tenant in the database table 'tenants' based on the subdomain | |
# Modify this logic according to your actual table and column names | |
Tenant.find_by(subdomain: subdomain) | |
end | |
end | |
In this modified middleware, the find_tenant_by_subdomain method searches the tenants table in the database to find a matching tenant based on the subdomain. | |
3. Configure config/routes.rb for Subdomain Routing: | |
In your config/routes.rb file, you can set up routing for different subdomains. Here's an example: | |
ruby | |
Copy code | |
# config/routes.rb | |
Rails.application.routes.draw do | |
# Define routes for the "public" site | |
root 'public#index' | |
# ... other public routes | |
constraints(SubdomainConstraint.new) do | |
# Define routes for tenant-specific subdomains | |
root 'tenant#index' | |
# ... other tenant-specific routes | |
end | |
end | |
4. Create a Subdomain Constraint (lib/subdomain_constraint.rb): | |
Create a SubdomainConstraint class to handle routing based on subdomains: | |
ruby | |
Copy code | |
# lib/subdomain_constraint.rb | |
class SubdomainConstraint | |
def initialize | |
@tenants = Tenant.pluck(:subdomain) # Retrieve all subdomains from the 'tenants' table | |
end | |
def matches?(request) | |
subdomain = request.subdomain | |
@tenants.include?(subdomain) | |
end | |
end | |
In this example, the SubdomainConstraint class retrieves all subdomains from the tenants table and uses them for routing. You should adjust the table and column names according to your database schema. | |
5. Define Controllers and Views: | |
You should create separate controllers and views for the public site and tenant-specific sites as needed. | |
Remember to update your routes, controllers, and views according to your application's requirements. This setup allows you to handle subdomains and route users to their subdomain-specific content based on the subdomain they access by searching for tenants in the database table. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment