traefik/web.go

112 lines
4 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))
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))
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))
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
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
}
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{
templatesRenderer.JSON(rw, http.StatusNotFound, nil)
}
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{
templatesRenderer.JSON(rw, http.StatusNotFound, nil)
}
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{
templatesRenderer.JSON(rw, http.StatusNotFound, nil)
}
}
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{
templatesRenderer.JSON(rw, http.StatusNotFound, nil)
}
}else{
templatesRenderer.JSON(rw, http.StatusNotFound, nil)
}
}