A platform for community discussion. Free, open, simple.
DEV: Introduce `maxmind_mirror_url` GlobalSetting (#26458)
Alan Guo Xiang Tan918250136692
diff --git a/config/discourse_defaults.conf b/config/discourse_defaults.confindex 9da80716163..c2584052ae4 100644--- a/config/discourse_defaults.conf+++ b/config/discourse_defaults.conf@@ -295,7 +295,13 @@ maxmind_backup_path =# register an account at: https://www.maxmind.com/en/geolite2/signup# then head to profile and get your license key-maxmind_license_key=+maxmind_license_key =++# Configures a URL mirror to download the MaxMind databases from.+# When set, the file path will be appended to the mirror's URL.+# If the mirror URL is https://some.url.com/maxmind/mirror for example, the+# GeoLite2-City database file will be downloaded from https://some.url.com/maxmind/mirror/GeoLite2-City.tar.gz+maxmind_mirror_url =# when enabled the following headers will be added to every response:# (note, if measurements do not exist for the header they will be omitted)diff --git a/lib/discourse_ip_info.rb b/lib/discourse_ip_info.rbindex d7a9352087b..72236e90436 100644--- a/lib/discourse_ip_info.rb+++ b/lib/discourse_ip_info.rb@@ -25,16 +25,18 @@ class DiscourseIpInfoenddef self.mmdb_download(name)- if GlobalSetting.maxmind_license_key.blank?- STDERR.puts "MaxMind IP database updates require a license"- STDERR.puts "Please set DISCOURSE_MAXMIND_LICENSE_KEY to one you generated at https://www.maxmind.com"- return- end-- FileUtils.mkdir_p(path)-url =- "https://download.maxmind.com/app/geoip_download?license_key=#{GlobalSetting.maxmind_license_key}&edition_id=#{name}&suffix=tar.gz"+ if GlobalSetting.maxmind_mirror_url.present?+ URI.join(GlobalSetting.maxmind_mirror_url, "#{name}.tar.gz").to_s+ else+ if GlobalSetting.maxmind_license_key.blank?+ STDERR.puts "MaxMind IP database updates require a license"+ STDERR.puts "Please set DISCOURSE_MAXMIND_LICENSE_KEY to one you generated at https://www.maxmind.com"+ return+ end++ "https://download.maxmind.com/app/geoip_download?license_key=#{GlobalSetting.maxmind_license_key}&edition_id=#{name}&suffix=tar.gz"+ endgz_file =FileHelper.download(diff --git a/lib/discourse_ip_info_spec.rb b/lib/discourse_ip_info_spec.rbnew file mode 100644index 00000000000..89b654e7dcd--- /dev/null+++ b/lib/discourse_ip_info_spec.rb@@ -0,0 +1,16 @@+# frozen_string_literal: true++RSpec.describe DiscourseIpInfo do+ describe ".mmdb_download" do+ it "should download the MaxMind databases from the right URL when `maxmind_mirror_url` global setting has been configured" do+ global_setting :maxmind_mirror_url, "https://example.com/mirror/"++ stub_request(:get, "https://example.com/mirror/GeoLite2-City.tar.gz").to_return(+ status: 200,+ body: "",+ )++ described_class.mmdb_download("GeoLite2-City")+ end+ end+end