traefik/web.go

128 lines
4.2 KiB
Go
Raw Normal View History

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"
"io/ioutil"
"net/http"
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 {
Address string
2015-09-22 19:00:29 +00:00
CertFile, KeyFile string
2015-09-08 11:33:10 +00:00
}
type Page struct {
Configuration Configuration
}
2015-09-12 13:10:03 +00:00
func (provider *WebProvider) Provide(configurationChan chan<- *Configuration) {
2015-09-08 11:33:10 +00:00
systemRouter := mux.NewRouter()
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-09-22 08:33:37 +00:00
systemRouter.Methods("PUT").Path("/api").Handler(http.HandlerFunc(
2015-09-11 16:47:54 +00:00
func(rw http.ResponseWriter, r *http.Request) {
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-08 22:22:34 +00:00
configurationChan <- configuration
2015-09-09 07:16:56 +00:00
GetConfigHandler(rw, r)
2015-09-12 13:10:03 +00:00
} else {
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-15 16:35:32 +00:00
systemRouter.Methods("GET").Path("/api/backends").Handler(http.HandlerFunc(GetBackendsHandler))
2015-09-16 17:08:01 +00:00
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))
2015-09-15 16:35:32 +00:00
systemRouter.Methods("GET").Path("/api/frontends").Handler(http.HandlerFunc(GetFrontendsHandler))
2015-09-16 17:08:01 +00:00
systemRouter.Methods("GET").Path("/api/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 {
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)
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-09-08 11:33:10 +00:00
}
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
2015-09-11 16:55:38 +00:00
templatesRenderer.JSON(rw, http.StatusOK, currentConfiguration)
2015-09-08 11:33:10 +00:00
}
func GetHTMLConfigHandler(response http.ResponseWriter, request *http.Request) {
2015-09-12 13:10:03 +00:00
templatesRenderer.HTML(response, http.StatusOK, "configuration", Page{Configuration: *currentConfiguration})
2015-09-08 11:33:10 +00:00
}
2015-09-16 17:08:01 +00:00
func GetHealthHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, metrics.Data())
}
2015-09-15 16:35:32 +00:00
func GetBackendsHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, currentConfiguration.Backends)
}
func GetBackendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2015-09-16 17:08:01 +00:00
id := vars["backend"]
if backend, ok := currentConfiguration.Backends[id]; ok {
templatesRenderer.JSON(rw, http.StatusOK, backend)
} else {
2015-09-16 20:41:01 +00:00
http.NotFound(rw, r)
2015-09-16 17:08:01 +00:00
}
2015-09-15 16:35:32 +00:00
}
func GetFrontendsHandler(rw http.ResponseWriter, r *http.Request) {
templatesRenderer.JSON(rw, http.StatusOK, currentConfiguration.Frontends)
}
func GetFrontendHandler(rw http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2015-09-16 17:08:01 +00:00
id := vars["frontend"]
if frontend, ok := currentConfiguration.Frontends[id]; ok {
templatesRenderer.JSON(rw, http.StatusOK, frontend)
} else {
2015-09-16 20:41:01 +00:00
http.NotFound(rw, r)
2015-09-16 17:08:01 +00:00
}
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)
backend := vars["backend"]
if backend, ok := currentConfiguration.Backends[backend]; ok {
templatesRenderer.JSON(rw, http.StatusOK, backend.Servers)
} else {
2015-09-16 20:41:01 +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)
backend := vars["backend"]
server := vars["server"]
if backend, ok := currentConfiguration.Backends[backend]; ok {
if server, ok := backend.Servers[server]; ok {
templatesRenderer.JSON(rw, http.StatusOK, server)
} else {
2015-09-16 20:41:01 +00:00
http.NotFound(rw, r)
2015-09-16 17:08:01 +00:00
}
} else {
2015-09-16 20:41:01 +00:00
http.NotFound(rw, r)
2015-09-16 17:08:01 +00:00
}
2015-09-22 08:33:37 +00:00
}