Files
sx-fc/config/initializers/action_controller.rb
Infra Bot d79a6c5929 fix: Include aes_iv in cache key and fix subdomain parsing for cloud mode
1. license.rb: Add aes_iv to Redis cache key to prevent CipherError after
   container restart. New random IVs after restart were causing stale cache
   decryption failures.

2. action_controller.rb:
   - Use request.subdomains.first instead of .last to correctly parse shop
     code from tenant.fc.smartsales.asia domains
   - Add localhost bypass (127.0.0.1 / ::1) for Docker health checks in
     cloud mode, returning 200 OK instead of 404 not_found

Refs: infra-fix-2026-05-06
2026-05-06 12:16:04 +08:00

170 lines
4.6 KiB
Ruby

class ActionController::Base
before_action :lookup_domain if Rails.env.production?
before_action :set_locale
helper_method :current_license,
def not_found
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.json { head :not_found }
end
end
private
def lookup_domain
if ENV["SERVER_MODE"] == "cloud" && request.subdomains.first && request.subdomains.first != "www"
if license = cache_license # request.subdomain.downcase
logger.info "Location - " + license.dbschema
ActiveRecord::Base.establish_connection(website_connection(license))
else
# reconnect_default_db
logger.info 'License is nil'
not_found
end
elsif ENV["SERVER_MODE"] == "application" || request.subdomains.first && request.subdomains.first != "www"
# check for license file
if !current_license.exists?
redirect_to activate_path
elsif current_license.expired?
redirect_to review_license_path
end
else
if ENV["SERVER_MODE"] == "cloud" && (request.remote_ip == "127.0.0.1" || request.remote_ip == "::1")
respond_to do |format|
format.html { render plain: "OK", status: :ok }
format.json { render json: { status: "ok" } }
end
else
not_found
end
end
end
def current_license
@license ||= License.new(ENV["SX_PROVISION_URL"], request.host)
end
def cache_license
if (current_license.detail_with_local_cache == true)
return current_license
else
return nil
end
end
def check_subdomain
current_license.check_license_subdomain
end
def check_installation
if current_company.nil?
redirect_to install_path
end
end
def website_connection(license)
default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase,
:username => license.dbusername, :password => license.dbpassword)
end
def reconnect_default_db
ActiveRecord::Base.establish_connection(Rails.env)
end
# Regular database.yml configuration hash
def default_connection
@default_config ||= ActiveRecord::Base.connection_config
end
#change locallization
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
# RESTful url for localize
def default_url_options
{ locale: I18n.locale }
end
end
class ActionController::API
before_action :lookup_domain if Rails.env.production?
def not_found
respond_to do |format|
format.html { render :file => "#{Rails.root}/public/404", :layout => false, :status => :not_found }
format.json { head :not_found }
end
end
private
def lookup_domain
if ENV["SERVER_MODE"] == "cloud" && request.subdomains.first && request.subdomains.first != "www"
if license = cache_license # request.subdomain.downcase
logger.info "Location - " + license.dbschema
ActiveRecord::Base.establish_connection(website_connection(license))
else
# reconnect_default_db
logger.info 'License is nil'
not_found
end
elsif ENV["SERVER_MODE"] == "application" || (request.subdomains.first && request.subdomains.first != "www")
not_found unless current_license.exists? && !current_license.expired?
else
if ENV["SERVER_MODE"] == "cloud" && (request.remote_ip == "127.0.0.1" || request.remote_ip == "::1")
respond_to do |format|
format.html { render plain: "OK", status: :ok }
format.json { render json: { status: "ok" } }
end
else
not_found
end
end
end
def current_license
@license ||= License.new(ENV["SX_PROVISION_URL"], request.host)
end
def cache_license
if (current_license.detail_with_local_cache == true)
return current_license
else
return nil
end
end
def check_license(lookup)
License.check_license_file(lookup)
end
def website_connection(license)
default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase,
:username => license.dbusername, :password => license.dbpassword)
end
def reconnect_default_db
ActiveRecord::Base.establish_connection(Rails.env)
end
# Regular database.yml configuration hash
def default_connection
@default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup
end
#change locallization
def set_locale
I18n.locale = params[:locale] || I18n.default_locale
end
# RESTful url for localize
def default_url_options
{ locale: I18n.locale }
end
end