traefik/web.go

56 lines
1.6 KiB
Go
Raw Normal View History

2015-09-08 11:33:10 +00:00
package main
import (
"github.com/gorilla/mux"
"net/http"
"github.com/unrolled/render"
"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
)
2015-09-11 16:47:54 +00:00
var renderer = render.New(render.Options{
Directory: "templates",
Asset: Asset,
AssetNames: AssetNames,
})
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("/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) {
renderer.JSON(rw, http.StatusOK, currentConfiguration)
}
func GetHtmlConfigHandler(response http.ResponseWriter, request *http.Request) {
2015-09-09 08:29:49 +00:00
renderer.HTML(response, http.StatusOK, "configuration", Page{Configuration:*currentConfiguration})
2015-09-08 11:33:10 +00:00
}