You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
887 B
Go
51 lines
887 B
Go
4 years ago
|
package update
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
|
||
|
log "github.com/sirupsen/logrus"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
lock chan bool
|
||
|
)
|
||
|
|
||
|
// New is a factory function creating a new Handler instance
|
||
|
func New(updateFn func()) *Handler {
|
||
|
lock = make(chan bool, 1)
|
||
|
lock <- true
|
||
|
|
||
|
return &Handler{
|
||
|
fn: updateFn,
|
||
|
Path: "/v1/update",
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Handler is an API handler used for triggering container update scans
|
||
|
type Handler struct {
|
||
|
fn func()
|
||
|
Path string
|
||
|
}
|
||
|
|
||
|
// Handle is the actual http.Handle function doing all the heavy lifting
|
||
|
func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {
|
||
|
log.Info("Updates triggered by HTTP API request.")
|
||
|
|
||
|
_, err := io.Copy(os.Stdout, r.Body)
|
||
|
if err != nil {
|
||
|
log.Println(err)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
select {
|
||
|
case chanValue := <-lock:
|
||
|
defer func() { lock <- chanValue }()
|
||
|
handle.fn()
|
||
|
default:
|
||
|
log.Debug("Skipped. Another update already running.")
|
||
|
}
|
||
|
|
||
|
}
|