From 403f70480dab70e5e0b954db336d0c0f9b9fd9c2 Mon Sep 17 00:00:00 2001 From: Yan Date: Mon, 23 Apr 2018 16:23:00 +0630 Subject: [PATCH] api cloud update --- app/controllers/api/api_controller.rb | 73 ++++++++++++------------ app/models/license.rb | 4 +- app/models/my_aes_crypt.rb | 2 +- config/initializers/action_controller.rb | 61 ++++++++++++++++++++ config/secrets.yml | 2 +- 5 files changed, 101 insertions(+), 41 deletions(-) diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 2c3c0df1..a5112ffe 100755 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -23,46 +23,45 @@ class Api::ApiController < ActionController::API @employee = Employee.find_by_token_session(current_token) end - def lookup_domain - if request.subdomain.present? && request.subdomain != "www" - from = request.subdomain.downcase + "." + request.domain.downcase - @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase - if (!@license.nil?) - logger.info "Location - " + @license.dbhost - ActiveRecord::Base.establish_connection(website_connection(@license)) - # authenticate_session_token - # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema - else - # reconnect_default_db - logger.info 'License is nil' - # redirect_to root_url(:host => request.domain) + "store_error" - render :json => [{ status: false, message: 'Invalid Access!'}] - end - end - end + # def lookup_domain + # if request.subdomain.present? && request.subdomain != "www" + # from = request.subdomain.downcase + "." + request.domain.downcase + # @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + # if (!@license.nil?) + # logger.info "Location - " + @license.dbhost + # ActiveRecord::Base.establish_connection(website_connection(@license)) + # # authenticate_session_token + # # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + # else + # # reconnect_default_db + # logger.info 'License is nil' + # # redirect_to root_url(:host => request.domain) + "store_error" + # render :json => [{ status: false, message: 'Invalid Access!'}] + # end + # 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) + # def website_connection(license) + # default_connection.dup.update(:host => license.dbhost, :database => license.dbschema.to_s.downcase, + # :username => license.dbusername, :password => license.dbpassword) + # end - end + # def reconnect_default_db + # ActiveRecord::Base.establish_connection(Rails.env) + # 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 - # Regular database.yml configuration hash - def default_connection - @default_config ||= ActiveRecord::Base.connection.instance_variable_get("@config").dup - end + # def cache_license(url, lookup) + # @license = License.new(url, lookup) - def cache_license(url, lookup) - @license = License.new(url, lookup) - - if (@license.detail_with_local_cache(lookup) == true) - return @license - else - return nil - end - end + # if (@license.detail_with_local_cache(lookup) == true) + # return @license + # else + # return nil + # end + # end end diff --git a/app/models/license.rb b/app/models/license.rb index 13931376..9ef7d956 100755 --- a/app/models/license.rb +++ b/app/models/license.rb @@ -60,8 +60,8 @@ class License return true end else - # @license = Marshal.load(cache_license) - # assign(aes_key, aes_iv) + @license = Marshal.load(cache_license) + assign(aes_key, aes_iv) # Rails.logger.info 'API License' return true end diff --git a/app/models/my_aes_crypt.rb b/app/models/my_aes_crypt.rb index 7c86b158..8f3cde09 100644 --- a/app/models/my_aes_crypt.rb +++ b/app/models/my_aes_crypt.rb @@ -64,7 +64,7 @@ class MyAesCrypt shop_json["data"].each do |j| if j["lookup"] == from # add [0...44] for production cloud for remove \n - if(j["value"]["key"][0...44] == token) + if(j["value"]["key"] == token) return true end end diff --git a/config/initializers/action_controller.rb b/config/initializers/action_controller.rb index 8a149cb7..a3b19ebe 100644 --- a/config/initializers/action_controller.rb +++ b/config/initializers/action_controller.rb @@ -86,3 +86,64 @@ class ActionController::Base end end + +class ActionController::API + before_action :lookup_domain + + private + + def lookup_domain + if request.subdomain.present? && request.subdomain != "www" + from = request.subdomain.downcase + "." + request.domain.downcase + @license = cache_license(ENV["SX_PROVISION_URL"], from) # request.subdomain.downcase + if (!@license.nil?) + # logger.info "Location - " + @license.dbhost + ActiveRecord::Base.establish_connection(website_connection(@license)) + # authenticate_session_token + # logger.info "Connecting to - " + @license.subdomain + " - "+ @license.dbhost + "@" + @license.dbschema + else + # reconnect_default_db + logger.info 'License is nil' + # redirect_to root_url(:host => request.domain) + "store_error" + render :json => [{ status: false, message: 'Invalid Access!'}] + end + end + end + + def cache_license(url, lookup) + @license = License.new(url, lookup) + + if (@license.detail_with_local_cache(lookup) == true) + return @license + else + return nil + 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.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 + diff --git a/config/secrets.yml b/config/secrets.yml index 218234f1..b0d7c5c8 100755 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -13,7 +13,7 @@ development: secret_key_base: b61d85f8ed2a1a9e0eeece3443b3e8f838d002cc1d9f32115d8e93db920e2957adfedc57501d44741211538f3108b742cdeada87d5bfae796c53da1f90a3cd61 sx_provision_url: https://connect.pos-myanmar.com/bensai/api #connect.smartsales.dev/api #connect.smartsales.asia/api #provision.zsai.ws/api - server_mode: application + server_mode: cloud cipher_type: AES-256-CBC sx_key: Wh@t1$C2L