From cf64fdb9f06fa614cb9c57701f4136be2a0c6034 Mon Sep 17 00:00:00 2001 From: Anirudh M Date: Wed, 1 Jul 2020 15:41:26 +0530 Subject: [PATCH] Use middleware, and resolve some comments --- cmd/service/service.go | 15 +++++++++++---- pkg/api/list.go | 19 +++++++++---------- pkg/api/ping/pinghandler.go | 2 -- pkg/api/service.go | 10 +++++----- pkg/api/service_test.go | 3 ++- pkg/api/upgrade.go | 1 - 6 files changed, 27 insertions(+), 23 deletions(-) diff --git a/cmd/service/service.go b/cmd/service/service.go index db1ad8c73..46f146888 100644 --- a/cmd/service/service.go +++ b/cmd/service/service.go @@ -16,6 +16,13 @@ func main() { startServer() } +func setContentType(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + next.ServeHTTP(w, r) + }) +} + func startServer() { router := http.NewServeMux() @@ -35,10 +42,10 @@ func startServer() { api.NewUpgrader(actionUpgrade), api.NewHistory(actionHistory)) - router.Handle("/ping", ping.Handler()) - router.Handle("/list", api.List(service)) - router.Handle("/install", api.Install(service)) - router.Handle("/upgrade", api.Upgrade(service)) + router.Handle("/ping", setContentType(ping.Handler())) + router.Handle("/list", setContentType(api.List(service))) + router.Handle("/install", setContentType(api.Install(service))) + router.Handle("/upgrade", setContentType(api.Upgrade(service))) err := http.ListenAndServe(fmt.Sprintf(":%d", 8080), router) if err != nil { diff --git a/pkg/api/list.go b/pkg/api/list.go index c7d35ed9e..eeeaf03f6 100644 --- a/pkg/api/list.go +++ b/pkg/api/list.go @@ -2,10 +2,11 @@ package api import ( "encoding/json" + "net/http" + "helm.sh/helm/v3/pkg/api/logger" "helm.sh/helm/v3/pkg/release" "helm.sh/helm/v3/pkg/time" - "net/http" ) type ListRequest struct { @@ -19,19 +20,17 @@ type ListResponse struct { } type Release struct { - Name string `json:"name"` - Namespace string `json:"namespace"` - Revision int `json:"revision"` - Updated time.Time `json:"updated_at,omitempty"` - Status release.Status `json:"status"` - Chart string `json:"chart"` - AppVersion string `json:"app_version"` + Name string `json:"name"` + Namespace string `json:"namespace"` + Revision int `json:"revision"` + Updated time.Time `json:"updated_at,omitempty"` + Status release.Status `json:"status"` + Chart string `json:"chart"` + AppVersion string `json:"app_version"` } func List(svc Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - var response ListResponse var request ListRequest decoder := json.NewDecoder(r.Body) diff --git a/pkg/api/ping/pinghandler.go b/pkg/api/ping/pinghandler.go index 2d4cf5f3a..ba08c3914 100644 --- a/pkg/api/ping/pinghandler.go +++ b/pkg/api/ping/pinghandler.go @@ -8,8 +8,6 @@ import ( func Handler() http.Handler { return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { - - res.Header().Set("Content-Type", "application/json") defer req.Body.Close() var request Req diff --git a/pkg/api/service.go b/pkg/api/service.go index 3ad1859a7..6cca4ec5f 100644 --- a/pkg/api/service.go +++ b/pkg/api/service.go @@ -85,7 +85,7 @@ func (s Service) Upgrade(ctx context.Context, cfg ReleaseConfig, values ChartVal if s.upgrader.GetInstall() { if _, err := s.history.Run(cfg.Name); err == driver.ErrReleaseNotFound { - fmt.Printf("Release %q does not exist. Installing it now.\n", cfg.Name) + logger.Debugf("release %q does not exist. Installing it now.\n", cfg.Name) return s.installChart(cfg, chart, vals) } } @@ -162,10 +162,10 @@ func (s Service) List(releaseStatus string) ([]Release, error) { var helmReleases []Release for _, eachRes := range releases { r := Release{Name: eachRes.Name, - Namespace: eachRes.Namespace, - Revision: eachRes.Version, - Updated: eachRes.Info.LastDeployed, - Status: eachRes.Info.Status, + Namespace: eachRes.Namespace, + Revision: eachRes.Version, + Updated: eachRes.Info.LastDeployed, + Status: eachRes.Info.Status, Chart: fmt.Sprintf("%s-%s", eachRes.Chart.Metadata.Name, eachRes.Chart.Metadata.Version), AppVersion: eachRes.Chart.Metadata.AppVersion, } diff --git a/pkg/api/service_test.go b/pkg/api/service_test.go index eca340128..e1568f051 100644 --- a/pkg/api/service_test.go +++ b/pkg/api/service_test.go @@ -3,9 +3,10 @@ package api_test import ( "context" "errors" - "helm.sh/helm/v3/pkg/time" "testing" + "helm.sh/helm/v3/pkg/time" + "helm.sh/helm/v3/pkg/action" "helm.sh/helm/v3/pkg/api" diff --git a/pkg/api/upgrade.go b/pkg/api/upgrade.go index 3ab0c8b05..762c15b24 100644 --- a/pkg/api/upgrade.go +++ b/pkg/api/upgrade.go @@ -22,7 +22,6 @@ type UpgradeResponse struct { func Upgrade(svc Service) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") defer r.Body.Close() var req UpgradeRequest