feat(api): implement new api handler

This commit is contained in:
nils måsén 2023-10-21 19:35:41 +02:00
parent 72e437f173
commit 47091761a5
17 changed files with 571 additions and 294 deletions

View file

@ -0,0 +1,22 @@
package updates
import (
"github.com/containrrr/watchtower/pkg/types"
"net/url"
"strings"
)
type ModifyParamsFunc func(up *types.UpdateParams)
type InvokedFunc func(ModifyParamsFunc) types.Report
func parseImages(u *url.URL) []string {
var images []string
imageQueries, found := u.Query()["image"]
if found {
for _, image := range imageQueries {
images = append(images, strings.Split(image, ",")...)
}
}
return images
}

View file

@ -0,0 +1,37 @@
package updates
import (
. "github.com/containrrr/watchtower/pkg/api/prelude"
"github.com/containrrr/watchtower/pkg/filters"
"github.com/containrrr/watchtower/pkg/types"
"sync"
log "github.com/sirupsen/logrus"
)
// PostV1 creates an API http.HandlerFunc for V1 of updates
func PostV1(updateFn InvokedFunc, updateLock *sync.Mutex) HandlerFunc {
return func(c *Context) Response {
log.Info("Updates triggered by HTTP API request.")
images := parseImages(c.Request.URL)
if !updateLock.TryLock() {
if len(images) > 0 {
// If images have been passed, wait until the current updates are done
updateLock.Lock()
} else {
// If a full update is running (no explicit image filter), skip this update
log.Debug("Skipped. Another updates already running.")
return OK(nil) // For backwards compatibility
}
}
defer updateLock.Unlock()
_ = updateFn(func(up *types.UpdateParams) {
up.Filter = filters.FilterByImage(images, up.Filter)
})
return OK(nil)
}
}