traefik/web.go

54 lines
1.7 KiB
Go
Raw Normal View History

2015-09-08 11:33:10 +00:00
package main
import (
"github.com/gorilla/mux"
"net/http"
"fmt"
2015-09-08 22:22:34 +00:00
"io/ioutil"
"encoding/json"
2015-09-11 16:47:54 +00:00
"github.com/elazarl/go-bindata-assetfs"
2015-09-08 11:33:10 +00:00
)
type WebProvider struct {
Address string
}
type Page struct {
Configuration Configuration
}
2015-09-11 16:47:54 +00:00
func (provider *WebProvider) Provide(configurationChan chan <- *Configuration) {
2015-09-08 11:33:10 +00:00
systemRouter := mux.NewRouter()
2015-09-11 14:37:13 +00:00
systemRouter.Methods("GET").PathPrefix("/web/").Handler(http.HandlerFunc(GetHtmlConfigHandler))
systemRouter.Methods("GET").PathPrefix("/metrics/").Handler(http.HandlerFunc(GetStatsHandler))
2015-09-11 14:37:13 +00:00
systemRouter.Methods("GET").PathPrefix("/api/").Handler(http.HandlerFunc(GetConfigHandler))
systemRouter.Methods("POST").PathPrefix("/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-09 07:16:56 +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-11 16:47:54 +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
}))
systemRouter.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-11 16:55:38 +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())
}