Fix Codacy issues

This commit is contained in:
Anders Roos 2022-11-15 13:34:40 +01:00
parent 5645cd23fe
commit 741f315f14
19 changed files with 163 additions and 135 deletions

View file

@ -23,6 +23,7 @@ func New(token string) *API {
}
}
// EnableCors is a middleware that enables CORS for the API
func (api *API) EnableCors(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Access-Control-Allow-Origin", "*")

View file

@ -10,20 +10,18 @@ import (
"github.com/containrrr/watchtower/pkg/types"
)
// Handler is an HTTP handle for serving list data
// Handler is an HTTP handle for serving check data
type Handler struct {
Path string
Client container.Client
}
// CheckRequest defines the type for the request data of the Check endpoint
type CheckRequest struct {
ContainerId string
type checkRequest struct {
ContainerID string
}
// CheckResponse defines the type for the response data of the Check endpoint
type CheckResponse struct {
ContainerId string
type checkResponse struct {
ContainerID string
HasUpdate bool
NewVersion string
NewVersionCreated string
@ -47,7 +45,7 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
log.Info("Check for update triggered by HTTP API request.")
var request CheckRequest
var request checkRequest
err := json.NewDecoder(r.Body).Decode(&request)
if err != nil {
log.Error(err)
@ -57,7 +55,7 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
}
client := handle.Client
container, err := client.GetContainer(types.ContainerID(request.ContainerId))
container, err := client.GetContainer(types.ContainerID(request.ContainerID))
if err != nil {
log.Error(err)
w.WriteHeader(http.StatusInternalServerError)
@ -74,8 +72,8 @@ func (handle *Handler) HandlePost(w http.ResponseWriter, r *http.Request) {
return
}
data := CheckResponse{
ContainerId: request.ContainerId,
data := checkResponse{
ContainerID: request.ContainerID,
HasUpdate: stale,
NewVersion: newestImage.ShortID(),
NewVersionCreated: created,

View file

@ -17,9 +17,8 @@ type Handler struct {
Client container.Client
}
// ContainerListEntry defines the type of each container in the response
type ContainerListEntry struct {
ContainerId string
type containerListEntry struct {
ContainerID string
ContainerName string
ImageName string
ImageNameShort string
@ -27,9 +26,8 @@ type ContainerListEntry struct {
ImageCreatedDate string
}
// ListResponse defines the return type of the List endpoint
type ListResponse struct {
Containers []ContainerListEntry
type listResponse struct {
Containers []containerListEntry
}
// New is a factory function creating a new List instance
@ -59,11 +57,11 @@ func (handle *Handler) HandleGet(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(err.Error()))
}
data := ListResponse{Containers: []ContainerListEntry{}}
data := listResponse{Containers: []containerListEntry{}}
for _, c := range containers {
data.Containers = append(data.Containers, ContainerListEntry{
ContainerId: c.ID().ShortID(),
data.Containers = append(data.Containers, containerListEntry{
ContainerID: c.ID().ShortID(),
ContainerName: c.Name()[1:],
ImageName: c.ImageName(),
ImageNameShort: strings.Split(c.ImageName(), ":")[0],

View file

@ -41,8 +41,8 @@ func (d *Dashboard) Start() error {
return nil
}
func (d *Dashboard) templatedHttpHandler(h http.Handler) http.HandlerFunc {
const apiUrlTemplate = "%s://%s:%s/%s/"
func (d *Dashboard) templatedHTTPHandler(h http.Handler) http.HandlerFunc {
const apiURLTemplate = "%s://%s:%s/%s/"
indexTemplate, err := template.ParseFiles(d.rootDir + "/index.html")
if err != nil {
log.Error("Error when parsing index template")
@ -53,9 +53,9 @@ func (d *Dashboard) templatedHttpHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
hostName := strings.Split(r.Host, ":")[0]
apiUrl := fmt.Sprintf(apiUrlTemplate, d.apiScheme, hostName, d.apiPort, d.apiVersion)
err = indexTemplate.Execute(w, struct{ ApiUrl string }{
ApiUrl: apiUrl,
apiURL := fmt.Sprintf(apiURLTemplate, d.apiScheme, hostName, d.apiPort, d.apiVersion)
err = indexTemplate.Execute(w, struct{ APIURL string }{
APIURL: apiURL,
})
if err != nil {
log.Error("Error when executing index template")
@ -70,7 +70,7 @@ func (d *Dashboard) templatedHttpHandler(h http.Handler) http.HandlerFunc {
}
func (d *Dashboard) getHandler() http.Handler {
return d.templatedHttpHandler(http.FileServer(http.Dir(d.rootDir)))
return d.templatedHTTPHandler(http.FileServer(http.Dir(d.rootDir)))
}
func (d *Dashboard) runHTTPServer() {