From d577368c65eceec34ef51818aae0431323364ccf Mon Sep 17 00:00:00 2001 From: bubbajoe Date: Sun, 2 Jun 2024 16:15:22 +0900 Subject: [PATCH] route module missing from admin api, code cleaning, new line ofor dgate-cli, etc. --- cmd/dgate-cli/main.go | 1 + internal/admin/admin_routes.go | 10 +++------- internal/admin/routes/misc_routes.go | 9 +-------- pkg/dgclient/common.go | 8 ++++---- pkg/spec/transformers.go | 2 +- 5 files changed, 10 insertions(+), 20 deletions(-) diff --git a/cmd/dgate-cli/main.go b/cmd/dgate-cli/main.go index e778297..871f763 100644 --- a/cmd/dgate-cli/main.go +++ b/cmd/dgate-cli/main.go @@ -14,6 +14,7 @@ func main() { err := commands.Run(client, version) if err != nil { os.Stderr.WriteString(err.Error()) + os.Stderr.WriteString("\n") os.Exit(1) } } diff --git a/internal/admin/admin_routes.go b/internal/admin/admin_routes.go index f20d625..871bdac 100644 --- a/internal/admin/admin_routes.go +++ b/internal/admin/admin_routes.go @@ -35,13 +35,11 @@ func configureRoutes( ipList := iplist.NewIPList() for _, address := range adminConfig.AllowList { if strings.Contains(address, "/") { - err := ipList.AddCIDRString(address) - if err != nil { + if err := ipList.AddCIDRString(address); err != nil { panic(fmt.Sprintf("invalid cidr address in admin.allow_list: %s", address)) } } else { - err := ipList.AddIPString(address) - if err != nil { + if err := ipList.AddIPString(address); err != nil { panic(fmt.Sprintf("invalid ip address in admin.allow_list: %s", address)) } } @@ -181,9 +179,7 @@ func configureRoutes( if adminConfig != nil { server.Route("/api/v1", func(api chi.Router) { apiLogger := logger.Named("api") - routes.ConfigureRouteAPI( - api, - apiLogger, cs, conf) + routes.ConfigureRouteAPI(api, apiLogger, cs, conf) routes.ConfigureModuleAPI(api, apiLogger, cs, conf) routes.ConfigureServiceAPI(api, apiLogger, cs, conf) routes.ConfigureNamespaceAPI(api, apiLogger, cs, conf) diff --git a/internal/admin/routes/misc_routes.go b/internal/admin/routes/misc_routes.go index a471bbe..4b236ce 100644 --- a/internal/admin/routes/misc_routes.go +++ b/internal/admin/routes/misc_routes.go @@ -32,14 +32,6 @@ func ConfigureChangeLogAPI(server chi.Router, cs changestate.ChangeState, appCon w.Write([]byte(b)) } }) - server.Get("/changelog/rm", func(w http.ResponseWriter, r *http.Request) { - if b, err := json.Marshal(cs.ResourceManager()); err != nil { - util.JsonError(w, http.StatusInternalServerError, err.Error()) - } else { - w.Header().Set("Content-Type", "application/json") - w.Write([]byte(b)) - } - }) } func ConfigureHealthAPI(server chi.Router, version string, cs changestate.ChangeState) { @@ -54,6 +46,7 @@ func ConfigureHealthAPI(server chi.Router, version string, cs changestate.Change server.Get("/readyz", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") if r := cs.Raft(); r != nil { + w.Header().Set("X-Raft-State", r.State().String()) if r.Leader() == "" { w.WriteHeader(http.StatusServiceUnavailable) w.Write([]byte(`{"status":"no leader"}`)) diff --git a/pkg/dgclient/common.go b/pkg/dgclient/common.go index 8b22b09..bfe9b1f 100644 --- a/pkg/dgclient/common.go +++ b/pkg/dgclient/common.go @@ -140,15 +140,15 @@ func validateStatusCode(code int) error { } else if code < 400 { return errors.New("redirect from server; retry with the --follow flag") } - return fmt.Errorf("error code %d: %s", code, http.StatusText(code)) + return fmt.Errorf("%d error from server", code) } -func parseApiError(body io.Reader, defaultErr error) error { +func parseApiError(body io.Reader, wrapErr error) error { var apiError struct { Error string `json:"error"` } if err := json.NewDecoder(body).Decode(&apiError); err != nil || apiError.Error == "" { - return defaultErr + return wrapErr } - return errors.New("api error: " + apiError.Error) + return fmt.Errorf("%d: %s", wrapErr, apiError.Error) } diff --git a/pkg/spec/transformers.go b/pkg/spec/transformers.go index 700c38a..878a6e5 100644 --- a/pkg/spec/transformers.go +++ b/pkg/spec/transformers.go @@ -27,7 +27,7 @@ func TransformDGateRoute(r *DGateRoute) *Route { } var modules []string if r.Modules != nil && len(r.Modules) > 0 { - sliceutil.SliceMapper(r.Modules, func(m *DGateModule) string { return m.Name }) + modules = sliceutil.SliceMapper(r.Modules, func(m *DGateModule) string { return m.Name }) } return &Route{ Name: r.Name,