Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for CLOUDFLARE_API_HOSTNAME #753

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cloudflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"golang.org/x/time/rate"
)

const apiURL = "https://api.cloudflare.com/client/v4"
var apiURL = GetAPIUrl()

const (
originCARootCertEccURL = "https://developers.cloudflare.com/ssl/0d2cd0f374da0fb6dbf53128b60bbbf7/origin_ca_ecc_root.pem"
Expand Down
12 changes: 12 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package cloudflare

import "os"

// GetAPIUrl Returns CLOUDFLARE_API_HOSTNAME or default api hostname
func GetAPIUrl() (apiURL string) {
var apiHost string
if apiHost = os.Getenv("CLOUDFLARE_API_HOSTNAME"); apiHost == "" {
apiHost = "api.cloudflare.com"
}
return "https://" + apiHost + "/client/v4"
}
20 changes: 20 additions & 0 deletions util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cloudflare

import (
"os"
"testing"
)

func TestGetAPIURLDefault(t *testing.T) {
if GetAPIUrl() != "https://api.cloudflare.com/client/v4" {
t.Error("Expected https://api.cloudflare.com/client/v4, got", GetAPIUrl())
}
}

func TestGetAPIURLEnv(t *testing.T) {
os.Setenv("CLOUDFLARE_API_HOSTNAME", "other.host.cloudflare.com")
defer os.Unsetenv("CLOUDFLARE_API_HOSTNAME")
if GetAPIUrl() != "https://other.host.cloudflare.com/client/v4" {
t.Error("Expected https://other.host.cloudflare.com/client/v4, got", GetAPIUrl())
}
}