mirror of
https://github.com/astuto/astuto.git
synced 2025-12-16 11:47:56 +01:00
Add custom domains (#314)
This commit is contained in:
committed by
GitHub
parent
d47c70f576
commit
d17b45c5c4
48
app/workflows/add_custom_domain_workflow.rb
Normal file
48
app/workflows/add_custom_domain_workflow.rb
Normal file
@@ -0,0 +1,48 @@
|
||||
class AddCustomDomainWorkflow
|
||||
include HTTParty
|
||||
|
||||
attr_accessor :new_custom_domain, :current_custom_domain
|
||||
|
||||
def initialize(new_custom_domain: "", current_custom_domain: "")
|
||||
@new_custom_domain = new_custom_domain
|
||||
@current_custom_domain = current_custom_domain
|
||||
end
|
||||
|
||||
def make_request(method, domain)
|
||||
return unless method == "POST" || method == "DELETE"
|
||||
|
||||
HTTParty.send(
|
||||
method.downcase,
|
||||
ENV["ASTUTO_CNAME_API_URL"],
|
||||
headers: {
|
||||
"api_key" => ENV["ASTUTO_CNAME_API_KEY"],
|
||||
"Accept" => "application/json",
|
||||
},
|
||||
query: { "domain" => domain.downcase }
|
||||
)
|
||||
end
|
||||
|
||||
def run
|
||||
return true unless Rails.application.multi_tenancy?
|
||||
return true if @new_custom_domain == @current_custom_domain
|
||||
return true if Tenant.exists?(custom_domain: @new_custom_domain)
|
||||
|
||||
begin
|
||||
# Add new custom domain...
|
||||
if @new_custom_domain.present?
|
||||
response = make_request("POST", @new_custom_domain)
|
||||
|
||||
return false unless response.success?
|
||||
end
|
||||
|
||||
# ... and remove the current one
|
||||
if @current_custom_domain.present?
|
||||
make_request("DELETE", @current_custom_domain)
|
||||
end
|
||||
|
||||
return true
|
||||
rescue => e
|
||||
return false
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user