Make the external url of cache server configurable if necessary
All checks were successful
checks / check and test (push) Successful in 28s

This commit is contained in:
林玮 (Jade Lin)
2024-06-17 23:26:04 +08:00
committed by Matthias Kesler
parent 1656206765
commit def1d0a6f2
4 changed files with 17 additions and 9 deletions

View File

@@ -38,10 +38,11 @@ type Handler struct {
gcing atomic.Bool
gcAt time.Time
outboundIP string
outboundIP string
advertiseURL string
}
func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
func StartHandler(dir, advertiseURL, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
h := &Handler{}
if logger == nil {
@@ -71,6 +72,8 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
}
h.storage = storage
h.advertiseURL = advertiseURL
if outboundIP != "" {
h.outboundIP = outboundIP
} else if ip := common.GetOutboundIP(); ip == nil {
@@ -111,10 +114,13 @@ func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger
}
func (h *Handler) ExternalURL() string {
// TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d",
h.outboundIP,
h.listener.Addr().(*net.TCPAddr).Port)
if h.advertiseURL != "" {
return h.advertiseURL
} else {
return fmt.Sprintf("http://%s:%d",
h.outboundIP,
h.listener.Addr().(*net.TCPAddr).Port)
}
}
func (h *Handler) Close() error {

View File

@@ -20,7 +20,7 @@ import (
func TestHandler(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, err := StartHandler(dir, "", 0, nil)
handler, err := StartHandler(dir, "", "", 0, nil)
require.NoError(t, err)
base := fmt.Sprintf("%s%s", handler.ExternalURL(), urlBase)
@@ -589,7 +589,7 @@ func uploadCacheNormally(t *testing.T, base, key, version string, content []byte
func TestHandler_gcCache(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, err := StartHandler(dir, "", 0, nil)
handler, err := StartHandler(dir, "", "", 0, nil)
require.NoError(t, err)
defer func() {