Refactoring REST API to handle multiple providers.

I changed what I think is needed and I have done manual testing on this.

I tried to keep the changes to a minimun.

The changes are approx:

* HTML output now includes the provider name in parenthesis.
  * I'm not versed in bootstrap, should the output group providers in a
  * table?
* PUT is only enabled on /api/web.
* GET on /api returns a map containing all providers configuration
* GET on /api/{provider} will return the config as before on that
* provider.
This commit is contained in:
Thorhallur Sverrisson 2015-09-22 23:17:21 +00:00
parent 55a50c5ab7
commit a07e395181
2 changed files with 34 additions and 40 deletions

View file

@ -27,9 +27,10 @@
<div class="col-md-6">
<!-- <div class="panel-heading">Frontends</div>
<div class="panel-body"> -->
{{range $keyFrontends, $valueFrontends := .Configuration.Frontends}}
{{range $keyProviders, $valueProviders := .Configurations}}
{{range $keyFrontends, $valueFrontends := $valueProviders.Frontends}}
<div class="panel panel-primary">
<div class="panel-heading">{{$keyFrontends}}</div>
<div class="panel-heading">{{$keyFrontends}} - ({{$keyProviders}})</div>
<div class="panel-body">
<a class="btn btn-info" role="button" data-toggle="collapse" href="#{{$valueFrontends.Backend}}" aria-expanded="false">
{{$valueFrontends.Backend}}
@ -51,14 +52,16 @@
</table>
</div>
{{end}}
{{end}}
<!-- </div> -->
</div>
<div class="col-md-6">
<!-- <div class="panel-heading">Backends</div>
<div class="panel-body"> -->
{{range $keyBackends, $valueBackends := .Configuration.Backends}}
{{range $keyProviders, $valueProviders := .Configurations}}
{{range $keyBackends, $valueBackends := $valueProviders.Backends}}
<div class="panel panel-primary" id="{{$keyBackends}}">
<div class="panel-heading">{{$keyBackends}}</div>
<div class="panel-heading">{{$keyBackends}}({{$keyProviders}})</div>
<div class="panel-body">
{{with $valueBackends.LoadBalancer}}
<a class="btn btn-info" role="button">
@ -87,6 +90,7 @@
</table>
</div>
{{end}}
{{end}}
<!-- </div> -->
</div>

62
web.go
View file

@ -11,17 +11,13 @@ import (
"github.com/gorilla/mux"
)
var (
webConfiguration *Configuration
)
type WebProvider struct {
Address string
CertFile, KeyFile string
}
type Page struct {
Configuration Configuration
Configurations configs
}
func (provider *WebProvider) Provide(configurationChan chan<- configMessage) {
@ -29,13 +25,20 @@ func (provider *WebProvider) Provide(configurationChan chan<- configMessage) {
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("PUT").Path("/api").Handler(http.HandlerFunc(
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) {
vars := mux.Vars(r)
if vars["provider"] != "web" {
rw.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(rw, "Only 'web' provider can be updated through the REST API")
return
}
configuration := new(Configuration)
b, _ := ioutil.ReadAll(r.Body)
err := json.Unmarshal(b, configuration)
if err == nil {
webConfiguration = configuration
configurationChan <- configMessage{"web", configuration}
GetConfigHandler(rw, r)
} else {
@ -43,12 +46,12 @@ func (provider *WebProvider) Provide(configurationChan chan<- configMessage) {
http.Error(rw, fmt.Sprintf("%+v", err), http.StatusBadRequest)
}
}))
systemRouter.Methods("GET").Path("/api/backends").Handler(http.HandlerFunc(GetBackendsHandler))
systemRouter.Methods("GET").Path("/api/backends/{backend}").Handler(http.HandlerFunc(GetBackendHandler))
systemRouter.Methods("GET").Path("/api/backends/{backend}/servers").Handler(http.HandlerFunc(GetServersHandler))
systemRouter.Methods("GET").Path("/api/backends/{backend}/servers/{server}").Handler(http.HandlerFunc(GetServerHandler))
systemRouter.Methods("GET").Path("/api/frontends").Handler(http.HandlerFunc(GetFrontendsHandler))
systemRouter.Methods("GET").Path("/api/frontends/{frontend}").Handler(http.HandlerFunc(GetFrontendHandler))
systemRouter.Methods("GET").Path("/api/{provider}/backends").Handler(http.HandlerFunc(GetBackendsHandler))
systemRouter.Methods("GET").Path("/api/{provider}/backends/{backend}").Handler(http.HandlerFunc(GetBackendHandler))
systemRouter.Methods("GET").Path("/api/{provider}/backends/{backend}/servers").Handler(http.HandlerFunc(GetServersHandler))
systemRouter.Methods("GET").Path("/api/{provider}/backends/{backend}/servers/{server}").Handler(http.HandlerFunc(GetServerHandler))
systemRouter.Methods("GET").Path("/api/{provider}/frontends").Handler(http.HandlerFunc(GetFrontendsHandler))
systemRouter.Methods("GET").Path("/api/{provider}/frontends/{frontend}").Handler(http.HandlerFunc(GetFrontendHandler))
systemRouter.Methods("GET").PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"})))
go func() {
@ -67,26 +70,11 @@ func (provider *WebProvider) Provide(configurationChan chan<- configMessage) {
}
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, webConfiguration)
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations)
}
func GetHTMLConfigHandler(response http.ResponseWriter, request *http.Request) {
var cfg Configuration
cfg.Backends = make(map[string]*Backend)
cfg.Frontends = make(map[string]*Frontend)
// Quick and dirty merge of config for display
for _, config := range currentConfigurations {
for name, config := range config.Backends {
cfg.Backends[name] = config
}
for name, config := range config.Frontends {
cfg.Frontends[name] = config
}
}
templatesRenderer.HTML(response, http.StatusOK, "configuration", Page{Configuration: cfg})
templatesRenderer.HTML(response, http.StatusOK, "configuration", Page{Configurations: currentConfigurations})
}
func GetHealthHandler(rw http.ResponseWriter, r *http.Request) {
@ -94,13 +82,14 @@ func GetHealthHandler(rw http.ResponseWriter, r *http.Request) {
}
func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, webConfiguration.Backends)
vars := mux.Vars(r)
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations[vars["provider"]].Backends)
}
func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["backend"]
if backend, ok := webConfiguration.Backends[id]; ok {
if backend, ok := currentConfigurations[vars["provider"]].Backends[id]; ok {
templatesRenderer.JSON(rw, http.StatusOK, backend)
} else {
http.NotFound(rw, r)
@ -108,13 +97,14 @@ func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
}
func GetFrontendsHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, webConfiguration.Frontends)
vars := mux.Vars(r)
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations[vars["provider"]].Frontends)
}
func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
id := vars["frontend"]
if frontend, ok := webConfiguration.Frontends[id]; ok {
if frontend, ok := currentConfigurations[vars["provider"]].Frontends[id]; ok {
templatesRenderer.JSON(rw, http.StatusOK, frontend)
} else {
http.NotFound(rw, r)
@ -124,7 +114,7 @@ func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
func GetServersHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
backend := vars["backend"]
if backend, ok := webConfiguration.Backends[backend]; ok {
if backend, ok := currentConfigurations[vars["provider"]].Backends[backend]; ok {
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
} else {
http.NotFound(rw, r)
@ -135,7 +125,7 @@ func GetServerHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
backend := vars["backend"]
server := vars["server"]
if backend, ok := webConfiguration.Backends[backend]; ok {
if backend, ok := currentConfigurations[vars["provider"]].Backends[backend]; ok {
if server, ok := backend.Servers[server]; ok {
templatesRenderer.JSON(rw, http.StatusOK, server)
} else {