Add /api/providers API

This commit is contained in:
emile 2015-10-03 16:50:53 +02:00
parent 93b5410987
commit 0b3928dafb
2 changed files with 28 additions and 24 deletions

46
file.go
View file

@ -21,7 +21,6 @@ func (provider *FileProvider) Provide(configurationChan chan<- configMessage) er
log.Error("Error creating file watcher", err) log.Error("Error creating file watcher", err)
return err return err
} }
defer watcher.Close()
file, err := os.Open(provider.Filename) file, err := os.Open(provider.Filename)
if err != nil { if err != nil {
@ -30,31 +29,30 @@ func (provider *FileProvider) Provide(configurationChan chan<- configMessage) er
} }
defer file.Close() 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 { 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())) err = watcher.Add(filepath.Dir(file.Name()))
} if err != nil {
log.Error("Error adding file watcher", err)
if err != nil { return err
log.Error("Error adding file watcher", err) }
return err
} }
configuration := provider.LoadFileConfig(file.Name()) configuration := provider.LoadFileConfig(file.Name())

6
web.go
View file

@ -6,6 +6,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"github.com/BurntSushi/ty/fun"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/elazarl/go-bindata-assetfs" "github.com/elazarl/go-bindata-assetfs"
"github.com/gorilla/mux" "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("/").Handler(http.HandlerFunc(GetHTMLConfigHandler))
systemRouter.Methods("GET").Path("/health").Handler(http.HandlerFunc(GetHealthHandler)) systemRouter.Methods("GET").Path("/health").Handler(http.HandlerFunc(GetHealthHandler))
systemRouter.Methods("GET").Path("/api").Handler(http.HandlerFunc(GetConfigHandler)) 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("GET").Path("/api/{provider}").Handler(http.HandlerFunc(GetConfigHandler))
systemRouter.Methods("PUT").Path("/api/{provider}").Handler(http.HandlerFunc( systemRouter.Methods("PUT").Path("/api/{provider}").Handler(http.HandlerFunc(
func(rw http.ResponseWriter, r *http.Request) { 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()) 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) { func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)
providerId := vars["provider"] providerId := vars["provider"]