From 6ff68cf50a56f409eb033f2a511e73c9ff7a74ce Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Mon, 21 Oct 2024 09:20:49 -0700 Subject: [PATCH 1/4] feat: adds a debug api --- internal/web/debug.go | 18 ++++++++++++++++++ internal/web/routes.go | 29 ++++++++++++++++------------- 2 files changed, 34 insertions(+), 13 deletions(-) create mode 100644 internal/web/debug.go diff --git a/internal/web/debug.go b/internal/web/debug.go new file mode 100644 index 00000000000..f72400d02fc --- /dev/null +++ b/internal/web/debug.go @@ -0,0 +1,18 @@ +package web + +import ( + "encoding/json" + "net/http" +) + +func (h *handler) debugStore(w http.ResponseWriter, r *http.Request) { + respone := make(map[string]interface{}) + respone["hosts"] = h.multiHostService.Hosts() + containers, errors := h.multiHostService.ListAllContainers() + respone["containers"] = containers + respone["errors"] = errors + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(respone) +} diff --git a/internal/web/routes.go b/internal/web/routes.go index b6d5c11718d..f886108cd22 100644 --- a/internal/web/routes.go +++ b/internal/web/routes.go @@ -83,25 +83,28 @@ func createRouter(h *handler) *chi.Mux { r.Use(h.config.Authorization.Authorizer.AuthMiddleware) } r.Group(func(r chi.Router) { - r.Group(func(r chi.Router) { + r.Route("/api", func(r chi.Router) { if h.config.Authorization.Provider != NONE { r.Use(auth.RequireAuthentication) } - r.Get("/api/hosts/{host}/containers/{id}/logs/stream", h.streamContainerLogs) - r.Get("/api/hosts/{host}/containers/{id}/logs/download", h.downloadLogs) - r.Get("/api/hosts/{host}/containers/{id}/logs", h.fetchLogsBetweenDates) - r.Get("/api/hosts/{host}/logs/mergedStream/{ids}", h.streamLogsMerged) - r.Get("/api/stacks/{stack}/logs/stream", h.streamStackLogs) - r.Get("/api/services/{service}/logs/stream", h.streamServiceLogs) - r.Get("/api/groups/{group}/logs/stream", h.streamGroupedLogs) - r.Get("/api/events/stream", h.streamEvents) + r.Get("/hosts/{host}/containers/{id}/logs/stream", h.streamContainerLogs) + r.Get("/hosts/{host}/containers/{id}/logs/download", h.downloadLogs) + r.Get("/hosts/{host}/containers/{id}/logs", h.fetchLogsBetweenDates) + r.Get("/hosts/{host}/logs/mergedStream/{ids}", h.streamLogsMerged) + r.Get("/stacks/{stack}/logs/stream", h.streamStackLogs) + r.Get("/services/{service}/logs/stream", h.streamServiceLogs) + r.Get("/groups/{group}/logs/stream", h.streamGroupedLogs) + r.Get("/events/stream", h.streamEvents) if h.config.EnableActions { - r.Post("/api/hosts/{host}/containers/{id}/actions/{action}", h.containerActions) + r.Post("/hosts/{host}/containers/{id}/actions/{action}", h.containerActions) } - r.Get("/api/releases", h.releases) - r.Get("/api/profile/avatar", h.avatar) - r.Patch("/api/profile", h.updateProfile) + r.Get("/releases", h.releases) + r.Get("/profile/avatar", h.avatar) + r.Patch("/profile", h.updateProfile) r.Get("/version", h.version) + if log.Debug().Enabled() { + r.Get("/debug/store", h.debugStore) + } }) defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index)) From 95a4ad28c692b794bd6ce416e6eb8fd736a0c5f0 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Mon, 21 Oct 2024 09:29:19 -0700 Subject: [PATCH 2/4] fixes tests --- internal/web/auth_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/web/auth_test.go b/internal/web/auth_test.go index 9609434ebed..77f1a67f64f 100644 --- a/internal/web/auth_test.go +++ b/internal/web/auth_test.go @@ -71,7 +71,7 @@ func Test_createRoutes_version(t *testing.T) { fs := afero.NewMemMapFs() require.NoError(t, afero.WriteFile(fs, "index.html", []byte("index page"), 0644), "WriteFile should have no error.") handler := createHandler(nil, afero.NewIOFS(fs), Config{Base: "/", Version: "dev", Authorization: Authorization{Provider: NONE}}) - req, err := http.NewRequest("GET", "/version", nil) + req, err := http.NewRequest("GET", "/api/version", nil) require.NoError(t, err, "NewRequest should not return an error.") rr := httptest.NewRecorder() From e5537ce66ea6316cf157ec9c8c669a48309a5e27 Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Mon, 21 Oct 2024 10:01:52 -0700 Subject: [PATCH 3/4] more clean up --- internal/web/routes.go | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/internal/web/routes.go b/internal/web/routes.go index f886108cd22..de292bafeee 100644 --- a/internal/web/routes.go +++ b/internal/web/routes.go @@ -67,6 +67,7 @@ func CreateServer(multiHostService *MultiHostService, content fs.FS, config Conf var fileServer http.Handler func createRouter(h *handler) *chi.Mux { + fileServer = http.FileServer(http.FS(h.content)) base := h.config.Base r := chi.NewRouter() @@ -82,8 +83,10 @@ func createRouter(h *handler) *chi.Mux { if h.config.Authorization.Provider != NONE { r.Use(h.config.Authorization.Authorizer.AuthMiddleware) } - r.Group(func(r chi.Router) { - r.Route("/api", func(r chi.Router) { + + r.Route("/api", func(r chi.Router) { + // Authenticated routes + r.Group(func(r chi.Router) { if h.config.Authorization.Provider != NONE { r.Use(auth.RequireAuthentication) } @@ -107,19 +110,19 @@ func createRouter(h *handler) *chi.Mux { } }) - defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index)) - r.Get("/*", func(w http.ResponseWriter, req *http.Request) { - defaultHandler.ServeHTTP(w, req) - }) + // Public API routes + if h.config.Authorization.Provider == SIMPLE { + r.Post("/token", h.createToken) + r.Delete("/token", h.deleteToken) + } }) - if h.config.Authorization.Provider == SIMPLE { - r.Post("/api/token", h.createToken) - r.Delete("/api/token", h.deleteToken) - } - r.Get("/healthcheck", h.healthcheck) + defaultHandler := http.StripPrefix(strings.Replace(base+"/", "//", "/", 1), http.HandlerFunc(h.index)) + r.Get("/*", func(w http.ResponseWriter, req *http.Request) { + defaultHandler.ServeHTTP(w, req) + }) }) if base != "/" { @@ -128,8 +131,6 @@ func createRouter(h *handler) *chi.Mux { }) } - fileServer = http.FileServer(http.FS(h.content)) - // r.Mount("/debug", middleware.Profiler()) return r From ddf9a1815616442f769a7c4baf827cc931b9e86f Mon Sep 17 00:00:00 2001 From: Amir Raminfar Date: Mon, 21 Oct 2024 12:08:35 -0700 Subject: [PATCH 4/4] fix: checks also created containers --- internal/docker/container_store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/docker/container_store.go b/internal/docker/container_store.go index ab244a99e5e..da07b0fc2b5 100644 --- a/internal/docker/container_store.go +++ b/internal/docker/container_store.go @@ -72,7 +72,7 @@ func (s *ContainerStore) checkConnectivity() error { } running := lo.Filter(containers, func(item Container, index int) bool { - return item.State == "running" + return item.State != "exited" }) sem := semaphore.NewWeighted(maxFetchParallelism)