provide ability to override baseurl of APIs

This commit is contained in:
bobo liu
2024-06-22 20:16:29 +08:00
parent 0c44e39a20
commit 3260753a66
5 changed files with 13 additions and 4 deletions

View File

@@ -14,7 +14,7 @@ import (
)
func IPApiCom(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
url := "http://ip-api.com/json/" + ip + "?fields=status,message,country,regionName,city,isp,district,as,lat,lon"
url := token.BaseOrDefault("http://ip-api.com/json/") + ip + "?fields=status,message,country,regionName,city,isp,district,as,lat,lon"
client := &http.Client{
// 2 秒超时
Timeout: timeout,

View File

@@ -12,7 +12,7 @@ import (
)
func IPInfo(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
url := "http://ipinfo.io/" + ip + "?token=" + token.ipinfo
url := token.BaseOrDefault("http://ipinfo.io/") + ip + "?token=" + token.ipinfo
client := &http.Client{
// 2 秒超时
Timeout: timeout,

View File

@@ -13,7 +13,7 @@ func IPInSight(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData,
// 2 秒超时
Timeout: timeout,
}
resp, err := client.Get("https://api.ipinsight.io/ip/" + ip + "?token=" + token.ipinsight)
resp, err := client.Get(token.BaseOrDefault("https://api.ipinsight.io/ip/") + ip + "?token=" + token.ipinsight)
if err != nil {
return nil, err
}

View File

@@ -11,7 +11,7 @@ import (
)
func IPSB(ip string, timeout time.Duration, _ string, _ bool) (*IPGeoData, error) {
url := "https://api.ip.sb/geoip/" + ip
url := token.BaseOrDefault("https://api.ip.sb/geoip/") + ip
client := &http.Client{
// 2 秒超时
Timeout: timeout,

View File

@@ -6,10 +6,19 @@ type tokenData struct {
ipinsight string
ipinfo string
ipleo string
baseUrl string
}
func (t *tokenData) BaseOrDefault(def string) string {
if t.baseUrl == "" {
return def
}
return t.baseUrl
}
var token = tokenData{
ipinsight: util.GetenvDefault("NEXTTRACE_IPINSIGHT_TOKEN", ""),
ipinfo: util.GetenvDefault("NEXTTRACE_IPINFO_TOKEN", ""),
baseUrl: util.GetenvDefault("NEXTTRACE_IPAPI_BASE", ""),
ipleo: "NextTraceDemo",
}