DEV: Introduce maxmind_mirror_url GlobalSetting (#26458)

Why this change?

This allows downloading the MaxMind databases from a mirror in cases
where downloading directly from MaxMind's API endpoint is problematic
due to API limits.
This commit is contained in:
Alan Guo Xiang Tan 2024-04-02 14:53:53 +08:00 committed by GitHub
parent 6c1b320e2e
commit 9182501366
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 34 additions and 10 deletions

View File

@ -297,6 +297,12 @@ maxmind_backup_path =
# then head to profile and get your 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)
#

View File

@ -25,16 +25,18 @@ class DiscourseIpInfo
end
def self.mmdb_download(name)
url =
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
FileUtils.mkdir_p(path)
url =
"https://download.maxmind.com/app/geoip_download?license_key=#{GlobalSetting.maxmind_license_key}&edition_id=#{name}&suffix=tar.gz"
end
gz_file =
FileHelper.download(

View File

@ -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