diff --git a/ROADMAP.md b/ROADMAP.md index 47bda7e68..fb6dde2f6 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,7 +1,6 @@ # /træfɪk/ * Providers recovery -* Use Negroni middlewares for metrics, grace, logs * tls client verification * Default configuration values * Retry with streams @@ -28,3 +27,5 @@ * ~~SSL frontend support~~ * ~~Static files~~ * ~~Metrics~~ +* ~~Dockerfile~~ +* ~~Use Negroni middlewares for metrics, grace, logs~~ diff --git a/docker.go b/docker.go index 9f8f12325..1265e7e37 100644 --- a/docker.go +++ b/docker.go @@ -6,15 +6,17 @@ import ( "github.com/BurntSushi/ty/fun" "github.com/fsouza/go-dockerclient" "github.com/leekchan/gtf" + "github.com/cenkalti/backoff" "strconv" "strings" "text/template" + "errors" + "time" ) type DockerProvider struct { Watch bool Endpoint string - dockerClient *docker.Client Filename string Domain string } @@ -62,50 +64,60 @@ var DockerFuncMap = template.FuncMap{ "getHost": getHost, } -func (provider *DockerProvider) Provide(configurationChan chan<- *Configuration) { - if client, err := docker.NewClient(provider.Endpoint); err != nil { +func (provider *DockerProvider) Provide(configurationChan chan <- *Configuration) { + if dockerClient, err := docker.NewClient(provider.Endpoint); err != nil { log.Fatalf("Failed to create a client for docker, error: %s", err) } else { - provider.dockerClient = client - _, err := provider.dockerClient.Info() + err := dockerClient.Ping() if err != nil { log.Fatalf("Docker connection error %+v", err) } log.Debug("Docker connection established") dockerEvents := make(chan *docker.APIEvents) if provider.Watch { - provider.dockerClient.AddEventListener(dockerEvents) + dockerClient.AddEventListener(dockerEvents) + log.Debug("Docker listening") go func() { - for { - event := <-dockerEvents - if event == nil { - log.Fatalf("Docker connection error %+v", err) - } - if event.Status == "start" || event.Status == "die" { - log.Debug("Docker event receveived %+v", event) - configuration := provider.loadDockerConfig() - if configuration != nil { - configurationChan <- configuration + operation := func() error { + for { + event := <-dockerEvents + if event == nil { + return errors.New("Docker event nil") +// log.Fatalf("Docker connection error") + } + if (event.Status == "start" || event.Status == "die" ) { + log.Debug("Docker event receveived %+v", event) + configuration := provider.loadDockerConfig(dockerClient) + if configuration != nil { + configurationChan <- configuration + } } } } + notify := func(err error, time time.Duration) { + log.Error("Docker connection error %+v, retrying in %s", err, time) + } + err := backoff.RetryNotify(operation, backoff.NewExponentialBackOff(), notify) + if err != nil { + log.Fatalf("Cannot connect to docker server %+v", err) + } }() } - configuration := provider.loadDockerConfig() + configuration := provider.loadDockerConfig(dockerClient) configurationChan <- configuration } } -func (provider *DockerProvider) loadDockerConfig() *Configuration { +func (provider *DockerProvider) loadDockerConfig(dockerClient *docker.Client) *Configuration { configuration := new(Configuration) - containerList, _ := provider.dockerClient.ListContainers(docker.ListContainersOptions{}) + containerList, _ := dockerClient.ListContainers(docker.ListContainersOptions{}) containersInspected := []docker.Container{} hosts := map[string][]docker.Container{} // get inspect containers for _, container := range containerList { - containerInspected, _ := provider.dockerClient.InspectContainer(container.ID) + containerInspected, _ := dockerClient.InspectContainer(container.ID) containersInspected = append(containersInspected, *containerInspected) } diff --git a/middlewares/routes.go b/middlewares/routes.go new file mode 100644 index 000000000..85889938a --- /dev/null +++ b/middlewares/routes.go @@ -0,0 +1,28 @@ +/* +Copyright +*/ +package middlewares + +import ( + "log" + "net/http" + "github.com/gorilla/mux" + "encoding/json" +) + +type Routes struct { + router *mux.Router +} + +func NewRoutes(router *mux.Router) *Routes { + return &Routes{router} +} + +func (router *Routes) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { + routeMatch :=mux.RouteMatch{} + if(router.router.Match(r, &routeMatch)){ + json, _ := json.Marshal(routeMatch.Handler) + log.Println("Request match route ", json) + } + next(rw, r) +} diff --git a/traefik.go b/traefik.go index 962104a9b..91030f2e5 100644 --- a/traefik.go +++ b/traefik.go @@ -142,6 +142,7 @@ func main() { var negroni = negroni.New() negroni.Use(metrics) negroni.Use(loggerMiddleware) + //negroni.Use(middlewares.NewRoutes(configurationRouter)) negroni.UseHandler(configurationRouter) srv = &graceful.Server{