diff --git a/ROADMAP.md b/ROADMAP.md index e336e9f75..ad7d8a0c5 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -1,3 +1,4 @@ +* Default configuration values * Godoc * Weights * Licence diff --git a/configuration.go b/configuration.go index 7b4e3ea8a..fda2f8150 100644 --- a/configuration.go +++ b/configuration.go @@ -1,5 +1,22 @@ package main +type GlobalConfiguration struct { + Port string + GraceTimeOut int64 + Docker *DockerProvider + File *FileProvider + Web *WebProvider + Marathon *MarathonProvider +} + +func NewGlobalConfiguration() *GlobalConfiguration { + globalConfiguration := new(GlobalConfiguration) + // default values + globalConfiguration.Port = ":8080" + globalConfiguration.GraceTimeOut = 10 + + return globalConfiguration +} type Backend struct { Servers map[string]Server diff --git a/marathon.go b/marathon.go index d39fddc00..a03ed3321 100644 --- a/marathon.go +++ b/marathon.go @@ -11,11 +11,12 @@ import ( ) type MarathonProvider struct { - Watch bool - Endpoint string - marathonClient marathon.Marathon - Domain string - Filename string + Watch bool + Endpoint string + marathonClient marathon.Marathon + Domain string + Filename string + NetworkInterface string } var MarathonFuncMap = template.FuncMap{ @@ -40,7 +41,7 @@ var MarathonFuncMap = template.FuncMap{ func (provider *MarathonProvider) Provide(configurationChan chan <- *Configuration) { config := marathon.NewDefaultConfig() config.URL = provider.Endpoint - config.EventsInterface = "docker0" + config.EventsInterface = provider.NetworkInterface if client, err := marathon.NewClient(config); err != nil { log.Println("Failed to create a client for marathon, error: %s", err) return diff --git a/templates/notFound.tmpl b/templates/notFound.tmpl new file mode 100644 index 000000000..c8e98b03c --- /dev/null +++ b/templates/notFound.tmpl @@ -0,0 +1,9 @@ + + + + træfik + + + Ohhhh man, this is bad... + + \ No newline at end of file diff --git a/træfik.go b/træfik.go index 0609490aa..b763f01e1 100644 --- a/træfik.go +++ b/træfik.go @@ -5,7 +5,6 @@ import ( "github.com/mailgun/oxy/forward" "github.com/mailgun/oxy/roundrobin" "github.com/tylerb/graceful" - "net" "net/http" "net/url" "os" @@ -18,14 +17,6 @@ import ( "github.com/gorilla/handlers" ) -type FileConfiguration struct { - Port string - Docker *DockerProvider - File *FileProvider - Web *WebProvider - Marathon *MarathonProvider -} - var srv *graceful.Server var configurationRouter *mux.Router var currentConfiguration = new(Configuration) @@ -33,10 +24,11 @@ var configurationChan = make(chan *Configuration) var providers = []Provider{} func main() { + log.SetFlags(log.LstdFlags | log.Lshortfile) sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) globalConfigFile := "træfik.toml" - + configurationRouter = LoadDefaultConfig() go func() { for { configuration := <-configurationChan @@ -88,7 +80,7 @@ func main() { sig := <-sigs log.Println("I have to go...", sig) goAway = true - srv.Stop(10 * time.Second) + srv.Stop(time.Duration(configuration.GraceTimeOut) * time.Second) }() for { @@ -96,28 +88,37 @@ func main() { break } srv = &graceful.Server{ - Timeout: 10 * time.Second, + Timeout: time.Duration(configuration.GraceTimeOut) * time.Second, NoSignalHandling: true, - ConnState: func(conn net.Conn, state http.ConnState) { - // conn has a new state - }, - Server: &http.Server{ Addr: configuration.Port, Handler: configurationRouter, }, } - go srv.ListenAndServe() + go func() { + srv.ListenAndServe() + }() log.Println("Started") <-srv.StopChan() log.Println("Stopped") } } +func notFoundHandler(w http.ResponseWriter, r *http.Request) { + renderer.HTML(w, http.StatusNotFound, "notFound", nil) +} + +func LoadDefaultConfig() *mux.Router { + router := mux.NewRouter() + router.NotFoundHandler = handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(notFoundHandler)) + return router +} + func LoadConfig(configuration *Configuration) *mux.Router { router := mux.NewRouter() + router.NotFoundHandler = handlers.CombinedLoggingHandler(os.Stdout, http.HandlerFunc(notFoundHandler)) backends := map[string]http.Handler{} for routeName, route := range configuration.Routes { log.Println("Creating route", routeName) @@ -144,6 +145,10 @@ func LoadConfig(configuration *Configuration) *mux.Router { } for _, muxRoute := range newRoutes { muxRoute.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[route.Backend])) + err := muxRoute.GetError() + if err != nil { + log.Println("Error building route", err) + } } } return router @@ -157,10 +162,11 @@ func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value { return reflect.ValueOf(any).MethodByName(name).Call(inputs) } -func LoadFileConfig(file string) *FileConfiguration { - configuration := new(FileConfiguration) +func LoadFileConfig(file string) *GlobalConfiguration { + configuration := NewGlobalConfiguration() if _, err := toml.DecodeFile(file, configuration); err != nil { log.Fatal("Error reading file:", err) } + log.Printf("Global configuration loaded %+v\n", configuration) return configuration } \ No newline at end of file diff --git a/træfik.toml b/træfik.toml index cb9a899da..906770c03 100644 --- a/træfik.toml +++ b/træfik.toml @@ -1,4 +1,5 @@ port = ":8001" +graceTimeOut = 10 #[docker] #endpoint = "unix:///var/run/docker.sock" @@ -11,6 +12,7 @@ endpoint = "http://127.0.0.1:8080" watch = true domain = "toto.fr" filename = "marathon.tmpl" +networkInterface = "eth0" [web] address = ":8010"