formidable/internal/data/updater.go

39 lines
652 B
Go
Raw Normal View History

2022-05-10 22:31:17 +02:00
package data
import (
"io"
"net/url"
"github.com/pkg/errors"
)
type UpdaterHandler interface {
URLMatcher
Update(url *url.URL) (io.WriteCloser, error)
}
type Updater struct {
handlers []UpdaterHandler
}
func (u *Updater) Update(url *url.URL) (io.WriteCloser, error) {
for _, h := range u.handlers {
if !h.Match(url) {
continue
}
wr, err := h.Update(url)
if err != nil {
return nil, errors.WithStack(err)
}
return wr, nil
}
return nil, errors.Wrapf(ErrHandlerNotFound, "could not find matching handler for url '%s'", url.String())
}
func NewUpdater(handlers ...UpdaterHandler) *Updater {
return &Updater{handlers}
}