From 0b3928dafbd147339343f13ab0bc8c99d486b31e Mon Sep 17 00:00:00 2001 From: emile Date: Sat, 3 Oct 2015 16:50:53 +0200 Subject: [PATCH] Add /api/providers API --- file.go | 46 ++++++++++++++++++++++------------------------ web.go | 6 ++++++ 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/file.go b/file.go index c567a5038..11a12cb0c 100644 --- a/file.go +++ b/file.go @@ -21,7 +21,6 @@ func (provider *FileProvider) Provide(configurationChan chan<- configMessage) er log.Error("Error creating file watcher", err) return err } - defer watcher.Close() file, err := os.Open(provider.Filename) if err != nil { @@ -30,31 +29,30 @@ func (provider *FileProvider) Provide(configurationChan chan<- configMessage) er } defer file.Close() - // Process events - go func() { - for { - select { - case event := <-watcher.Events: - if strings.Contains(event.Name, file.Name()) { - log.Debug("File event:", event) - configuration := provider.LoadFileConfig(file.Name()) - if configuration != nil { - configurationChan <- configMessage{"file", configuration} - } - } - case error := <-watcher.Errors: - log.Error("Watcher event error", error) - } - } - }() - if provider.Watch { + // Process events + go func() { + defer watcher.Close() + for { + select { + case event := <-watcher.Events: + if strings.Contains(event.Name, file.Name()) { + log.Debug("File event:", event) + configuration := provider.LoadFileConfig(file.Name()) + if configuration != nil { + configurationChan <- configMessage{"file", configuration} + } + } + case error := <-watcher.Errors: + log.Error("Watcher event error", error) + } + } + }() err = watcher.Add(filepath.Dir(file.Name())) - } - - if err != nil { - log.Error("Error adding file watcher", err) - return err + if err != nil { + log.Error("Error adding file watcher", err) + return err + } } configuration := provider.LoadFileConfig(file.Name()) diff --git a/web.go b/web.go index fb21eece4..f5a0d26c7 100644 --- a/web.go +++ b/web.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" + "github.com/BurntSushi/ty/fun" log "github.com/Sirupsen/logrus" "github.com/elazarl/go-bindata-assetfs" "github.com/gorilla/mux" @@ -25,6 +26,7 @@ func (provider *WebProvider) Provide(configurationChan chan<- configMessage) err systemRouter.Methods("GET").Path("/").Handler(http.HandlerFunc(GetHTMLConfigHandler)) systemRouter.Methods("GET").Path("/health").Handler(http.HandlerFunc(GetHealthHandler)) systemRouter.Methods("GET").Path("/api").Handler(http.HandlerFunc(GetConfigHandler)) + systemRouter.Methods("GET").Path("/api/providers").Handler(http.HandlerFunc(GetProvidersHandler)) systemRouter.Methods("GET").Path("/api/{provider}").Handler(http.HandlerFunc(GetConfigHandler)) systemRouter.Methods("PUT").Path("/api/{provider}").Handler(http.HandlerFunc( func(rw http.ResponseWriter, r *http.Request) { @@ -82,6 +84,10 @@ func GetHealthHandler(rw http.ResponseWriter, r *http.Request) { templatesRenderer.JSON(rw, http.StatusOK, metrics.Data()) } +func GetProvidersHandler(rw http.ResponseWriter, r *http.Request) { + templatesRenderer.JSON(rw, http.StatusOK, fun.Keys(currentConfigurations)) +} + func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) providerId := vars["provider"]