Skip to content
Merged
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
59 changes: 58 additions & 1 deletion pkg/artifactcache/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
gcing atomic.Bool
gcAt time.Time

outboundIP string
outboundIP string
externalAddress string
}

func StartHandler(dir, outboundIP string, port uint16, logger logrus.FieldLogger) (*Handler, error) {
Expand Down Expand Up @@ -110,7 +111,63 @@
return h, nil
}

func CreateHandler(dir, externalAddress string, logger logrus.FieldLogger) (*Handler, http.Handler, error) {
h := &Handler{}

if logger == nil {
discard := logrus.New()
discard.Out = io.Discard
logger = discard
}
logger = logger.WithField("module", "artifactcache")
h.logger = logger

if dir == "" {
home, err := os.UserHomeDir()
if err != nil {
return nil, nil, err
}
dir = filepath.Join(home, ".cache", "actcache")

Check warning on line 130 in pkg/artifactcache/handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/artifactcache/handler.go#L126-L130

Added lines #L126 - L130 were not covered by tests
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return nil, nil, err
}

Check warning on line 134 in pkg/artifactcache/handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/artifactcache/handler.go#L133-L134

Added lines #L133 - L134 were not covered by tests

h.dir = dir

storage, err := NewStorage(filepath.Join(dir, "cache"))
if err != nil {
return nil, nil, err
}

Check warning on line 141 in pkg/artifactcache/handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/artifactcache/handler.go#L140-L141

Added lines #L140 - L141 were not covered by tests
h.storage = storage

if externalAddress != "" {
h.externalAddress = externalAddress
} else if ip := common.GetOutboundIP(); ip == nil {
return nil, nil, fmt.Errorf("unable to determine outbound IP address")
} else {
h.outboundIP = ip.String()
}

Check warning on line 150 in pkg/artifactcache/handler.go

View check run for this annotation

Codecov / codecov/patch

pkg/artifactcache/handler.go#L147-L150

Added lines #L147 - L150 were not covered by tests

router := httprouter.New()
router.GET(urlBase+"/cache", h.middleware(h.find))
router.POST(urlBase+"/caches", h.middleware(h.reserve))
router.PATCH(urlBase+"/caches/:id", h.middleware(h.upload))
router.POST(urlBase+"/caches/:id", h.middleware(h.commit))
router.GET(urlBase+"/artifacts/:id", h.middleware(h.get))
router.POST(urlBase+"/clean", h.middleware(h.clean))

h.router = router

h.gcCache()

return h, router, nil
}

func (h *Handler) ExternalURL() string {
if h.externalAddress != "" {
return h.externalAddress
}
// TODO: make the external url configurable if necessary
return fmt.Sprintf("http://%s:%d",
h.outboundIP,
Expand Down
10 changes: 10 additions & 0 deletions pkg/artifactcache/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,3 +695,13 @@ func TestHandler_gcCache(t *testing.T) {
}
require.NoError(t, db.Close())
}

func TestCreateHandler(t *testing.T) {
dir := filepath.Join(t.TempDir(), "artifactcache")
handler, router, err := CreateHandler(dir, "http://localhost:8080", nil)
require.NoError(t, err)
require.NotNil(t, handler)
require.NotNil(t, router)

require.Equal(t, "http://localhost:8080", handler.ExternalURL())
}