From e894ae9f0a9b5ce00821428120075f4075d51d35 Mon Sep 17 00:00:00 2001 From: emile Date: Tue, 8 Sep 2015 13:33:10 +0200 Subject: [PATCH] web provider --- configuration.html | 54 ++++++++++++++++++++++++++++++++++++++++++++++ provider.go | 2 +- tortuous.go | 38 +++++++++----------------------- tortuous.toml | 3 +++ web.go | 45 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 113 insertions(+), 29 deletions(-) create mode 100644 configuration.html create mode 100644 web.go diff --git a/configuration.html b/configuration.html new file mode 100644 index 000000000..780ecdde5 --- /dev/null +++ b/configuration.html @@ -0,0 +1,54 @@ + + + + {{.Title}} + + + + + + + + + + + + + +
+
Backends
+ +
+ {{range $keyBackends, $valueBackends := .Configuration.Backends}} +
+
Backend {{$keyBackends}}
+
+ {{range $keyServers, $valueServers := $valueBackends.Servers}} +
+
Server {{$keyServers}}
+
+ Url: {{$valueServers.Url}} +
+
+ {{end}} +
+
+ {{end}} +
+ +
+ + + \ No newline at end of file diff --git a/provider.go b/provider.go index 304619f5e..1d08df459 100644 --- a/provider.go +++ b/provider.go @@ -1,5 +1,5 @@ package main type Provider interface { - Provide(chan<- *Configuration) + Provide(configurationChan chan<- *Configuration) } diff --git a/tortuous.go b/tortuous.go index 688b34832..f19bce603 100644 --- a/tortuous.go +++ b/tortuous.go @@ -5,7 +5,6 @@ import ( "github.com/mailgun/oxy/forward" "github.com/mailgun/oxy/roundrobin" "github.com/tylerb/graceful" - "github.com/unrolled/render" "net" "net/http" "net/url" @@ -16,16 +15,17 @@ import ( "time" "log" "github.com/BurntSushi/toml" + "github.com/gorilla/handlers" ) type FileConfiguration struct { Docker *DockerProvider File *FileProvider + Web *WebProvider } var srv *graceful.Server var configurationRouter *mux.Router -var renderer = render.New() var currentConfiguration = new(Configuration) var configurationChan = make(chan *Configuration) var providers = []Provider{} @@ -34,11 +34,6 @@ func main() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) - systemRouter := mux.NewRouter() - systemRouter.Methods("POST").Path("/reload").HandlerFunc(ReloadConfigHandler) - systemRouter.Methods("GET").Path("/").HandlerFunc(GetConfigHandler) - go http.ListenAndServe(":8000", systemRouter) - go func() { for { configuration := <-configurationChan @@ -57,7 +52,6 @@ func main() { }() configuration := LoadFileConfig() - log.Println("Configuration loaded", configuration) if(configuration.Docker != nil){ providers = append(providers, configuration.Docker) } @@ -66,9 +60,15 @@ func main() { providers = append(providers, configuration.File) } + if(configuration.Web != nil){ + providers = append(providers, configuration.Web) + } + for _, provider := range providers { + log.Printf("Starting provider %v %+v\n", reflect.TypeOf(provider), provider) + currentProvider := provider go func() { - provider.Provide(configurationChan) + currentProvider.Provide(configurationChan) }() } @@ -127,31 +127,13 @@ func LoadConfig(configuration *Configuration) *mux.Router { rb.UpsertServer(url) } for _, route := range newRoutes { - route.Handler(lb) + route.Handler(handlers.CombinedLoggingHandler(os.Stdout, lb)) } } } return router } -func DeployService() { - configurationRouter = LoadConfig(currentConfiguration) -} - -func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) { - DeployService() - srv.Stop(10 * time.Second) - renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "reloaded"}) -} - -func RestartHandler(rw http.ResponseWriter, r *http.Request) { - renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "restarted"}) -} - -func GetConfigHandler(rw http.ResponseWriter, r *http.Request) { - renderer.JSON(rw, http.StatusOK, currentConfiguration) -} - func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { inputs := make([]reflect.Value, len(args)) for i, _ := range args { diff --git a/tortuous.toml b/tortuous.toml index 3a86e2376..e11e2680c 100644 --- a/tortuous.toml +++ b/tortuous.toml @@ -2,6 +2,9 @@ #endpoint = "unix:///var/run/docker.sock" #watch = true +[web] +address = ":8010" + [file] filename = "tortuous.toml" watch = true diff --git a/web.go b/web.go new file mode 100644 index 000000000..97df37aca --- /dev/null +++ b/web.go @@ -0,0 +1,45 @@ +package main + +import ( + "github.com/gorilla/mux" + "net/http" + "os" + "github.com/gorilla/handlers" + "github.com/unrolled/render" + "fmt" + "html/template" +) + +var renderer = render.New() + +type WebProvider struct { + Address string +} + +type Page struct { + Title string + Configuration Configuration +} + +func (provider *WebProvider) Provide(configurationChan chan<- *Configuration){ + systemRouter := mux.NewRouter() + systemRouter.Methods("GET").PathPrefix("/html/").Handler(handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(GetHtmlConfigHandler))) + systemRouter.Methods("GET").PathPrefix("/json/").Handler(handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(GetConfigHandler))) + systemRouter.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("static/")))) + + 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) { + templates := template.Must(template.ParseFiles("configuration.html")) + response.Header().Set("Content-type", "text/html") + err := request.ParseForm() + if err != nil { + http.Error(response, fmt.Sprintf("error parsing url %v", err), 500) + } + templates.ExecuteTemplate(response, "configuration.html", Page{Title: "Home", Configuration:*currentConfiguration}) +}