traefik/træfik.go

166 lines
4.1 KiB
Go
Raw Normal View History

2015-08-28 16:09:22 +00:00
package main
2015-09-07 08:38:58 +00:00
2015-08-28 16:09:22 +00:00
import (
2015-09-07 08:38:58 +00:00
"github.com/gorilla/mux"
2015-09-01 20:28:24 +00:00
"github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin"
2015-09-07 08:38:58 +00:00
"github.com/tylerb/graceful"
2015-08-28 16:09:22 +00:00
"net"
2015-09-07 08:38:58 +00:00
"net/http"
"net/url"
"os"
2015-08-28 16:09:22 +00:00
"os/signal"
2015-09-02 09:17:03 +00:00
"reflect"
2015-09-07 08:38:58 +00:00
"syscall"
"time"
"log"
2015-09-07 15:39:22 +00:00
"github.com/BurntSushi/toml"
2015-09-08 11:33:10 +00:00
"github.com/gorilla/handlers"
2015-08-28 16:09:22 +00:00
)
2015-09-07 22:15:14 +00:00
type FileConfiguration struct {
2015-09-09 15:50:02 +00:00
Port string
2015-09-07 15:39:22 +00:00
Docker *DockerProvider
File *FileProvider
Web *WebProvider
2015-09-09 20:39:08 +00:00
Marathon *MarathonProvider
2015-09-07 15:39:22 +00:00
}
2015-08-28 16:09:22 +00:00
var srv *graceful.Server
2015-09-07 22:15:14 +00:00
var configurationRouter *mux.Router
var currentConfiguration = new(Configuration)
var configurationChan = make(chan *Configuration)
2015-09-07 08:38:58 +00:00
var providers = []Provider{}
2015-08-28 16:09:22 +00:00
func main() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
2015-09-09 15:41:33 +00:00
globalConfigFile := "træfik.toml"
2015-08-28 16:09:22 +00:00
2015-09-07 08:38:58 +00:00
go func() {
for {
2015-09-07 22:15:14 +00:00
configuration := <-configurationChan
log.Println("Configuration receveived", configuration)
if configuration == nil {
log.Println("Skipping empty configuration")
} else if (reflect.DeepEqual(currentConfiguration, configuration)) {
2015-09-07 22:15:14 +00:00
log.Println("Skipping same configuration")
} else {
2015-09-07 22:15:14 +00:00
currentConfiguration = configuration
configurationRouter = LoadConfig(configuration)
srv.Stop(10 * time.Second)
time.Sleep(3 * time.Second)
}
2015-09-07 08:38:58 +00:00
}
}()
2015-09-09 15:41:33 +00:00
configuration := LoadFileConfig(globalConfigFile)
if (configuration.Docker != nil) {
2015-09-07 21:25:07 +00:00
providers = append(providers, configuration.Docker)
}
2015-09-09 20:39:08 +00:00
if (configuration.Marathon != nil) {
providers = append(providers, configuration.Marathon)
}
if (configuration.File != nil) {
2015-09-09 15:41:33 +00:00
if (len(configuration.File.Filename) == 0) {
// no filename, setting to global config file
configuration.File.Filename = globalConfigFile
}
2015-09-07 21:25:07 +00:00
providers = append(providers, configuration.File)
}
if (configuration.Web != nil) {
2015-09-08 11:33:10 +00:00
providers = append(providers, configuration.Web)
}
2015-09-07 22:15:14 +00:00
for _, provider := range providers {
2015-09-08 11:33:10 +00:00
log.Printf("Starting provider %v %+v\n", reflect.TypeOf(provider), provider)
currentProvider := provider
2015-09-07 22:15:14 +00:00
go func() {
2015-09-08 11:33:10 +00:00
currentProvider.Provide(configurationChan)
2015-09-07 22:15:14 +00:00
}()
}
2015-08-28 16:09:22 +00:00
goAway := false
go func() {
sig := <-sigs
log.Println("I have to go...", sig)
2015-08-28 16:09:22 +00:00
goAway = true
srv.Stop(10 * time.Second)
}()
2015-09-07 08:38:58 +00:00
for {
if goAway {
2015-08-28 16:09:22 +00:00
break
}
srv = &graceful.Server{
2015-09-07 08:38:58 +00:00
Timeout: 10 * time.Second,
2015-08-28 16:09:22 +00:00
NoSignalHandling: true,
ConnState: func(conn net.Conn, state http.ConnState) {
2015-09-02 09:17:03 +00:00
// conn has a new state
2015-08-28 16:09:22 +00:00
},
Server: &http.Server{
2015-09-09 15:50:02 +00:00
Addr: configuration.Port,
2015-09-07 22:15:14 +00:00
Handler: configurationRouter,
2015-08-28 16:09:22 +00:00
},
}
go srv.ListenAndServe()
log.Println("Started")
2015-09-07 08:38:58 +00:00
<-srv.StopChan()
log.Println("Stopped")
2015-08-28 16:09:22 +00:00
}
}
2015-09-07 22:15:14 +00:00
func LoadConfig(configuration *Configuration) *mux.Router {
2015-09-01 22:19:27 +00:00
router := mux.NewRouter()
backends := map[string]http.Handler{}
2015-09-07 22:15:14 +00:00
for routeName, route := range configuration.Routes {
log.Println("Creating route", routeName)
2015-09-01 22:19:27 +00:00
fwd, _ := forward.New()
2015-09-07 08:38:58 +00:00
newRoutes := []*mux.Route{}
for ruleName, rule := range route.Rules {
log.Println("Creating rule", ruleName)
2015-09-02 19:13:25 +00:00
newRouteReflect := Invoke(router.NewRoute(), rule.Category, rule.Value)
newRoute := newRouteReflect[0].Interface().(*mux.Route)
newRoutes = append(newRoutes, newRoute)
2015-09-01 22:19:27 +00:00
}
2015-09-09 15:41:33 +00:00
if (backends[route.Backend] ==nil) {
log.Println("Creating backend", route.Backend)
lb, _ := roundrobin.New(fwd)
rb, _ := roundrobin.NewRebalancer(lb)
for serverName, server := range configuration.Backends[route.Backend].Servers {
log.Println("Creating server", serverName)
url, _ := url.Parse(server.Url)
rb.UpsertServer(url)
2015-09-01 22:19:27 +00:00
}
2015-09-09 15:41:33 +00:00
backends[route.Backend]=lb
}else {
log.Println("Reusing backend", route.Backend)
}
for _, muxRoute := range newRoutes {
muxRoute.Handler(handlers.CombinedLoggingHandler(os.Stdout, backends[route.Backend]))
2015-09-01 22:19:27 +00:00
}
}
return router
}
2015-09-07 08:38:58 +00:00
func Invoke(any interface{}, name string, args ...interface{}) []reflect.Value {
2015-09-02 09:17:03 +00:00
inputs := make([]reflect.Value, len(args))
for i, _ := range args {
inputs[i] = reflect.ValueOf(args[i])
}
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
}
2015-09-07 15:39:22 +00:00
2015-09-09 15:41:33 +00:00
func LoadFileConfig(file string) *FileConfiguration {
2015-09-07 22:15:14 +00:00
configuration := new(FileConfiguration)
2015-09-09 15:41:33 +00:00
if _, err := toml.DecodeFile(file, configuration); err != nil {
2015-09-07 15:39:22 +00:00
log.Fatal("Error reading file:", err)
}
return configuration
}