traefik/web.go

78 lines
2.8 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"
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"
"io/ioutil"
"net/http"
2015-09-08 11:33:10 +00:00
)
type WebProvider struct {
Address string
}
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()
2015-09-15 16:35:32 +00:00
systemRouter.Methods("GET").Path("/").Handler(http.HandlerFunc(GetHtmlConfigHandler))
systemRouter.Methods("GET").Path("/metrics").Handler(http.HandlerFunc(GetStatsHandler))
systemRouter.Methods("GET").Path("/api").Handler(http.HandlerFunc(GetConfigHandler))
systemRouter.Methods("POST").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 {
2015-09-11 14:37:13 +00:00
log.Error("Error parsing configuration %+v\n", 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))
systemRouter.Methods("GET").Path("/api/backends/{id}").Handler(http.HandlerFunc(GetBackendHandler))
systemRouter.Methods("GET").Path("/api/frontends").Handler(http.HandlerFunc(GetFrontendsHandler))
systemRouter.Methods("GET").Path("/api/frontends/{id}").Handler(http.HandlerFunc(GetFrontendHandler))
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
go http.ListenAndServe(provider.Address, systemRouter)
}
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
}
func GetStatsHandler(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)
id := vars["id"]
templatesRenderer.JSON(rw, http.StatusOK, currentConfiguration.Backends[id])
}
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)
id := vars["id"]
templatesRenderer.JSON(rw, http.StatusOK, currentConfiguration.Frontends[id])
}