mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-17 23:50:13 +01:00
Added dashboard
This commit is contained in:
parent
d744c34886
commit
130429b10a
22 changed files with 2986 additions and 14 deletions
93
pkg/api/check/check.go
Normal file
93
pkg/api/check/check.go
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
package check
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"github.com/containrrr/watchtower/pkg/container"
|
||||
"github.com/containrrr/watchtower/pkg/types"
|
||||
)
|
||||
|
||||
// Handler is an HTTP handle for serving list data
|
||||
type Handler struct {
|
||||
Path string
|
||||
Client container.Client
|
||||
}
|
||||
|
||||
type CheckRequest struct {
|
||||
ContainerId string
|
||||
}
|
||||
|
||||
type CheckResponse struct {
|
||||
ContainerId string
|
||||
HasUpdate bool
|
||||
NewVersion string
|
||||
NewVersionCreated string
|
||||
}
|
||||
|
||||
// New is a factory function creating a new List instance
|
||||
func New(client container.Client) *Handler {
|
||||
return &Handler{
|
||||
Path: "/v1/check",
|
||||
Client: client,
|
||||
}
|
||||
}
|
||||
|
||||
// HandlePost is the actual http.HandlePost function doing all the heavy lifting
|
||||
func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
log.Info("Calling Check API with unsupported method")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
log.Info("Check for update triggered by HTTP API request.")
|
||||
|
||||
var request CheckRequest
|
||||
err := json.NewDecoder(r.Body).Decode(&request)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
client := handle.Client
|
||||
container, err := client.GetContainer(types.ContainerID(request.ContainerId))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
stale, newestImage, created, err := client.IsContainerStale(container)
|
||||
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
data := CheckResponse{
|
||||
ContainerId: request.ContainerId,
|
||||
HasUpdate: stale,
|
||||
NewVersion: newestImage.ShortID(),
|
||||
NewVersionCreated: created,
|
||||
}
|
||||
|
||||
jsonData, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
log.Fatalf("Error happened in JSON marshal. Err: %s", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(jsonData)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue