2015-09-08 11:33:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-09-08 22:22:34 +00:00
|
|
|
"encoding/json"
|
2015-09-12 13:10:03 +00:00
|
|
|
"fmt"
|
2015-09-24 15:16:13 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
2015-10-03 14:50:53 +00:00
|
|
|
"github.com/BurntSushi/ty/fun"
|
2015-09-24 12:32:37 +00:00
|
|
|
log "github.com/Sirupsen/logrus"
|
2015-09-11 16:47:54 +00:00
|
|
|
"github.com/elazarl/go-bindata-assetfs"
|
2015-09-12 13:10:03 +00:00
|
|
|
"github.com/gorilla/mux"
|
2015-09-08 11:33:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type WebProvider struct {
|
2015-09-24 12:32:37 +00:00
|
|
|
Address string
|
2015-09-22 19:00:29 +00:00
|
|
|
CertFile, KeyFile string
|
2015-09-08 11:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Page struct {
|
2015-09-22 23:17:21 +00:00
|
|
|
Configurations configs
|
2015-09-08 11:33:10 +00:00
|
|
|
}
|
|
|
|
|
2015-10-01 10:04:25 +00:00
|
|
|
func (provider *WebProvider) Provide(configurationChan chan<- configMessage) error {
|
2015-09-08 11:33:10 +00:00
|
|
|
systemRouter := mux.NewRouter()
|
2015-09-24 15:16:13 +00:00
|
|
|
systemRouter.Methods("GET").Path("/").Handler(http.HandlerFunc(GetHTMLConfigHandler))
|
2015-09-16 17:08:01 +00:00
|
|
|
systemRouter.Methods("GET").Path("/health").Handler(http.HandlerFunc(GetHealthHandler))
|
2015-09-15 16:35:32 +00:00
|
|
|
systemRouter.Methods("GET").Path("/api").Handler(http.HandlerFunc(GetConfigHandler))
|
2015-10-03 14:50:53 +00:00
|
|
|
systemRouter.Methods("GET").Path("/api/providers").Handler(http.HandlerFunc(GetProvidersHandler))
|
2015-09-22 23:17:21 +00:00
|
|
|
systemRouter.Methods("GET").Path("/api/{provider}").Handler(http.HandlerFunc(GetConfigHandler))
|
|
|
|
systemRouter.Methods("PUT").Path("/api/{provider}").Handler(http.HandlerFunc(
|
2015-09-11 16:47:54 +00:00
|
|
|
func(rw http.ResponseWriter, r *http.Request) {
|
2015-09-22 23:17:21 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-08 22:22:34 +00:00
|
|
|
configuration := new(Configuration)
|
|
|
|
b, _ := ioutil.ReadAll(r.Body)
|
2015-09-11 16:47:54 +00:00
|
|
|
err := json.Unmarshal(b, configuration)
|
2015-09-12 13:10:03 +00:00
|
|
|
if err == nil {
|
2015-09-22 04:16:21 +00:00
|
|
|
configurationChan <- configMessage{"web", configuration}
|
2015-09-09 07:16:56 +00:00
|
|
|
GetConfigHandler(rw, r)
|
2015-09-12 13:10:03 +00:00
|
|
|
} else {
|
2015-09-24 12:32:37 +00:00
|
|
|
log.Errorf("Error parsing configuration %+v", err)
|
2015-09-09 07:16:56 +00:00
|
|
|
http.Error(rw, fmt.Sprintf("%+v", err), http.StatusBadRequest)
|
2015-09-08 22:22:34 +00:00
|
|
|
}
|
2015-09-11 16:47:54 +00:00
|
|
|
}))
|
2015-09-22 23:17:21 +00:00
|
|
|
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))
|
2015-09-15 16:35:32 +00:00
|
|
|
systemRouter.Methods("GET").PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(&assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: "static"})))
|
2015-09-08 11:33:10 +00:00
|
|
|
|
2015-09-22 19:00:29 +00:00
|
|
|
go func() {
|
|
|
|
if len(provider.CertFile) > 0 && len(provider.KeyFile) > 0 {
|
2015-09-24 12:32:37 +00:00
|
|
|
err := http.ListenAndServeTLS(provider.Address, provider.CertFile, provider.KeyFile, systemRouter)
|
|
|
|
if err != nil {
|
2015-09-24 09:39:18 +00:00
|
|
|
log.Fatal("Error creating server: ", err)
|
|
|
|
}
|
2015-09-22 19:00:29 +00:00
|
|
|
} else {
|
2015-09-24 09:39:18 +00:00
|
|
|
err := http.ListenAndServe(provider.Address, systemRouter)
|
2015-09-24 12:32:37 +00:00
|
|
|
if err != nil {
|
2015-09-24 09:39:18 +00:00
|
|
|
log.Fatal("Error creating server: ", err)
|
|
|
|
}
|
2015-09-22 19:00:29 +00:00
|
|
|
}
|
|
|
|
}()
|
2015-10-01 10:04:25 +00:00
|
|
|
return nil
|
2015-09-08 11:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
2015-09-22 23:17:21 +00:00
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, currentConfigurations)
|
2015-09-08 11:33:10 +00:00
|
|
|
}
|
|
|
|
|
2015-09-24 15:16:13 +00:00
|
|
|
func GetHTMLConfigHandler(response http.ResponseWriter, request *http.Request) {
|
2015-09-22 23:17:21 +00:00
|
|
|
templatesRenderer.HTML(response, http.StatusOK, "configuration", Page{Configurations: currentConfigurations})
|
2015-09-08 11:33:10 +00:00
|
|
|
}
|
2015-09-12 11:20:54 +00:00
|
|
|
|
2015-09-16 17:08:01 +00:00
|
|
|
func GetHealthHandler(rw http.ResponseWriter, r *http.Request) {
|
2015-09-12 11:20:54 +00:00
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, metrics.Data())
|
|
|
|
}
|
2015-09-15 16:35:32 +00:00
|
|
|
|
2015-10-03 14:50:53 +00:00
|
|
|
func GetProvidersHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, fun.Keys(currentConfigurations))
|
|
|
|
}
|
|
|
|
|
2015-09-15 16:35:32 +00:00
|
|
|
func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) {
|
2015-09-22 23:17:21 +00:00
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, provider.Backends)
|
|
|
|
} else {
|
|
|
|
http.NotFound(rw, r)
|
|
|
|
}
|
2015-09-15 16:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
backendId := vars["backend"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
if backend, ok := provider.Backends[backendId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, backend)
|
|
|
|
return
|
|
|
|
}
|
2015-09-16 17:08:01 +00:00
|
|
|
}
|
2015-09-23 05:48:32 +00:00
|
|
|
http.NotFound(rw, r)
|
2015-09-15 16:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetFrontendsHandler(rw http.ResponseWriter, r *http.Request) {
|
2015-09-22 23:17:21 +00:00
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, provider.Frontends)
|
|
|
|
} else {
|
|
|
|
http.NotFound(rw, r)
|
|
|
|
}
|
2015-09-15 16:35:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
frontendId := vars["frontend"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
if frontend, ok := provider.Frontends[frontendId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, frontend)
|
|
|
|
return
|
|
|
|
}
|
2015-09-16 17:08:01 +00:00
|
|
|
}
|
2015-09-23 05:48:32 +00:00
|
|
|
http.NotFound(rw, r)
|
2015-09-15 16:35:32 +00:00
|
|
|
}
|
2015-09-16 17:08:01 +00:00
|
|
|
|
|
|
|
func GetServersHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
backendId := vars["backend"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
if backend, ok := provider.Backends[backendId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
|
|
|
|
return
|
|
|
|
}
|
2015-09-16 17:08:01 +00:00
|
|
|
}
|
2015-09-23 05:48:32 +00:00
|
|
|
http.NotFound(rw, r)
|
2015-09-16 17:08:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetServerHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2015-09-23 05:48:32 +00:00
|
|
|
providerId := vars["provider"]
|
|
|
|
backendId := vars["backend"]
|
|
|
|
serverId := vars["server"]
|
|
|
|
if provider, ok := currentConfigurations[providerId]; ok {
|
|
|
|
if backend, ok := provider.Backends[backendId]; ok {
|
|
|
|
if server, ok := backend.Servers[serverId]; ok {
|
|
|
|
templatesRenderer.JSON(rw, http.StatusOK, server)
|
|
|
|
return
|
|
|
|
}
|
2015-09-16 17:08:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-23 05:48:32 +00:00
|
|
|
http.NotFound(rw, r)
|
2015-09-22 08:33:37 +00:00
|
|
|
}
|