From dddad58b161e27f84b34c8a174c1c605bd354897 Mon Sep 17 00:00:00 2001 From: Yann Bizeul Date: Sun, 13 Oct 2024 09:33:56 +0200 Subject: [PATCH] add dump secret feature --- internal/feed/feedmanager.go | 22 ++++++++++++++++++++++ internal/handlers/handlers.go | 18 ++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/internal/feed/feedmanager.go b/internal/feed/feedmanager.go index f85a73a..8e7d02e 100644 --- a/internal/feed/feedmanager.go +++ b/internal/feed/feedmanager.go @@ -2,6 +2,7 @@ package feed import ( "fmt" + "os" "path" ) @@ -60,3 +61,24 @@ func (m *FeedManager) GetFeedWithAuth(feedName string, secret string) (*Feed, er return result, nil } + +func (m *FeedManager) DumpSecrets() { + d, err := os.ReadDir(m.path) + if err != nil { + // TODO: log error + return + } + for _, entry := range d { + if !entry.IsDir() { + continue + } + feedPath := path.Join(m.path, entry.Name()) + + result, err := GetFeed(feedPath) + if err != nil { + // TODO Log error + return + } + fmt.Printf("Feed %s: %s\n", result.Name(), result.Config.Secret) + } +} diff --git a/internal/handlers/handlers.go b/internal/handlers/handlers.go index ba76bca..4ae9ad2 100644 --- a/internal/handlers/handlers.go +++ b/internal/handlers/handlers.go @@ -188,6 +188,7 @@ func (api *ApiHandler) GetServer() *chi.Mux { } }) + r.Post("/api/secrets", api.postSecretsHandler) r.Route("/api/feeds", func(r chi.Router) { r.Get("/{feedName}", api.feedGetFunc) r.Post("/{feedName}", api.feedPostFunc) @@ -224,23 +225,20 @@ func (api *ApiHandler) feedWSHandler(w http.ResponseWriter, r *http.Request) { _, err := api.FeedManager.GetFeedWithAuth(feedName, secret) if err != nil { + // A web socket doesn't have a standard http status code, so we need + // to open it and close it with a relevant code var upgrader = ws.Upgrader{} c, err := upgrader.Upgrade(w, r, nil) if err != nil { return - // utils.CloseWithCodeAndMessage(w, 500, "Unable to upgrade WebSocket") } switch { case errors.Is(err, feed.FeedErrorNotFound): - //WriteError(w, http.StatusNotFound, "feed '%s' not found", feedName) - c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusNotFound+4000, ""), time.Now().Add(time.Second)) - + _ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusNotFound+4000, ""), time.Now().Add(time.Second)) case errors.Is(err, feed.FeedErrorInvalidSecret): - //WriteError(w, http.StatusUnauthorized, "Invalid secret") - c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusUnauthorized+4000, ""), time.Now().Add(time.Second)) + _ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusUnauthorized+4000, ""), time.Now().Add(time.Second)) default: - //WriteError(w, http.StatusInternalServerError, "Error while getting feed: %s", err.Error()) - c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusInternalServerError+4000, ""), time.Now().Add(time.Second)) + _ = c.WriteControl(ws.CloseMessage, ws.FormatCloseMessage(http.StatusInternalServerError+4000, ""), time.Now().Add(time.Second)) } c.Close() return @@ -624,3 +622,7 @@ func (api *ApiHandler) subscriptionDeleteFunc(w http.ResponseWriter, r *http.Req return } } + +func (api *ApiHandler) postSecretsHandler(w http.ResponseWriter, r *http.Request) { + api.FeedManager.DumpSecrets() +}