diff --git a/service.go b/configuration.go similarity index 91% rename from service.go rename to configuration.go index c3a90ec0a..33e0c18a2 100644 --- a/service.go +++ b/configuration.go @@ -19,7 +19,7 @@ type Route struct { Rules map[string]Rule } -type Service struct { +type Configuration struct { Backends map[string]Backend Routes map[string]Route } \ No newline at end of file diff --git a/docker.go b/docker.go index 6db9f9bec..45a44b3e4 100644 --- a/docker.go +++ b/docker.go @@ -1,5 +1,5 @@ package main -import( +import ( "github.com/fsouza/go-dockerclient" "github.com/leekchan/gtf" "bytes" @@ -8,32 +8,34 @@ import( ) type DockerProvider struct { - Watch bool - Endpoint string + Watch bool + Endpoint string dockerClient *docker.Client } -func (provider *DockerProvider) Provide(serviceChan chan<- *Service){ +func (provider *DockerProvider) Provide(configurationChan chan <- *Configuration) { provider.dockerClient, _ = docker.NewClient(provider.Endpoint) dockerEvents := make(chan *docker.APIEvents) - if(provider.Watch) { + if (provider.Watch) { provider.dockerClient.AddEventListener(dockerEvents) } go func() { for { event := <-dockerEvents log.Println("Event receveived", event) - service:= provider.loadDockerConfig() - serviceChan <- service + configuration := provider.loadDockerConfig() + if (configuration != nil) { + configurationChan <- configuration + } } }() - service:= provider.loadDockerConfig() - serviceChan <- service + configuration := provider.loadDockerConfig() + configurationChan <- configuration } -func (provider *DockerProvider) loadDockerConfig() *Service { - service := new(Service) +func (provider *DockerProvider) loadDockerConfig() *Configuration { + configuration := new(Configuration) containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{}) containersInspected := []docker.Container{} for _, container := range containerList { @@ -59,9 +61,9 @@ func (provider *DockerProvider) loadDockerConfig() *Service { return nil } - if _, err := toml.Decode(buffer.String(), service); err != nil { - log.Println("Error creating docker service:", err) + if _, err := toml.Decode(buffer.String(), configuration); err != nil { + log.Println("Error creating docker configuration:", err) return nil } - return service + return configuration } \ No newline at end of file diff --git a/file.go b/file.go index 10bf91a4d..ff1d30484 100644 --- a/file.go +++ b/file.go @@ -14,7 +14,7 @@ type FileProvider struct { Filename string } -func (provider *FileProvider) Provide(serviceChan chan<- *Service){ +func (provider *FileProvider) Provide(configurationChan chan<- *Configuration){ watcher, err := fsnotify.NewWatcher() if err != nil { log.Println(err) @@ -37,9 +37,9 @@ func (provider *FileProvider) Provide(serviceChan chan<- *Service){ case event := <-watcher.Events: if(strings.Contains(event.Name,file.Name())){ log.Println("File event:", event) - service := provider.LoadFileConfig(file.Name()) - if(service != nil) { - serviceChan <- service + configuration := provider.LoadFileConfig(file.Name()) + if(configuration != nil) { + configurationChan <- configuration } } case error := <-watcher.Errors: @@ -58,17 +58,17 @@ func (provider *FileProvider) Provide(serviceChan chan<- *Service){ } - service:= provider.LoadFileConfig(file.Name()) - serviceChan <- service + configuration := provider.LoadFileConfig(file.Name()) + configurationChan <- configuration <-done } -func (provider *FileProvider) LoadFileConfig(filename string) *Service { - service := new(Service) - if _, err := toml.DecodeFile(filename, service); err != nil { +func (provider *FileProvider) LoadFileConfig(filename string) *Configuration { + configuration := new(Configuration) + if _, err := toml.DecodeFile(filename, configuration); err != nil { log.Println("Error reading file:", err) return nil } - return service + return configuration } \ No newline at end of file diff --git a/provider.go b/provider.go index 67f0f20ad..304619f5e 100644 --- a/provider.go +++ b/provider.go @@ -1,5 +1,5 @@ package main type Provider interface { - Provide(chan<- *Service) + Provide(chan<- *Configuration) } diff --git a/tortuous.go b/tortuous.go index fdeebf552..688b34832 100644 --- a/tortuous.go +++ b/tortuous.go @@ -18,16 +18,16 @@ import ( "github.com/BurntSushi/toml" ) -type Configuration struct { +type FileConfiguration struct { Docker *DockerProvider File *FileProvider } var srv *graceful.Server -var serviceRouter *mux.Router +var configurationRouter *mux.Router var renderer = render.New() -var currentService = new(Service) -var serviceChan = make(chan *Service) +var currentConfiguration = new(Configuration) +var configurationChan = make(chan *Configuration) var providers = []Provider{} func main() { @@ -41,15 +41,15 @@ func main() { go func() { for { - service := <-serviceChan - log.Println("Service receveived", service) - if service == nil { - log.Println("Skipping nil service") - } else if(reflect.DeepEqual(currentService, service)){ - log.Println("Skipping same service") + configuration := <-configurationChan + log.Println("Configuration receveived", configuration) + if configuration == nil { + log.Println("Skipping empty configuration") + } else if(reflect.DeepEqual(currentConfiguration, configuration)){ + log.Println("Skipping same configuration") } else{ - currentService = service - serviceRouter = LoadConfig(service) + currentConfiguration = configuration + configurationRouter = LoadConfig(configuration) srv.Stop(10 * time.Second) time.Sleep(3 * time.Second) } @@ -66,11 +66,11 @@ func main() { providers = append(providers, configuration.File) } - go func() { - for _, provider := range providers { - provider.Provide(serviceChan) - } - }() + for _, provider := range providers { + go func() { + provider.Provide(configurationChan) + }() + } goAway := false go func() { @@ -94,7 +94,7 @@ func main() { Server: &http.Server{ Addr: ":8001", - Handler: serviceRouter, + Handler: configurationRouter, }, } @@ -105,9 +105,9 @@ func main() { } } -func LoadConfig(service *Service) *mux.Router { +func LoadConfig(configuration *Configuration) *mux.Router { router := mux.NewRouter() - for routeName, route := range service.Routes { + for routeName, route := range configuration.Routes { log.Println("Creating route", routeName) fwd, _ := forward.New() newRoutes := []*mux.Route{} @@ -121,7 +121,7 @@ func LoadConfig(service *Service) *mux.Router { log.Println("Creating backend", backendName) lb, _ := roundrobin.New(fwd) rb, _ := roundrobin.NewRebalancer(lb) - for serverName, server := range service.Backends[backendName].Servers { + for serverName, server := range configuration.Backends[backendName].Servers { log.Println("Creating server", serverName) url, _ := url.Parse(server.Url) rb.UpsertServer(url) @@ -135,7 +135,7 @@ func LoadConfig(service *Service) *mux.Router { } func DeployService() { - serviceRouter = LoadConfig(currentService) + configurationRouter = LoadConfig(currentConfiguration) } func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) { @@ -149,7 +149,7 @@ func RestartHandler(rw http.ResponseWriter, r *http.Request) { } func GetConfigHandler(rw http.ResponseWriter, r *http.Request) { - renderer.JSON(rw, http.StatusOK, currentService) + renderer.JSON(rw, http.StatusOK, currentConfiguration) } func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { @@ -160,8 +160,8 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { return reflect.ValueOf(any).MethodByName(name).Call(inputs) } -func LoadFileConfig() *Configuration { - configuration := new(Configuration) +func LoadFileConfig() *FileConfiguration { + configuration := new(FileConfiguration) if _, err := toml.DecodeFile("tortuous.toml", configuration); err != nil { log.Fatal("Error reading file:", err) }