traefik/tortuous.go

169 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"
"github.com/unrolled/render"
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-08-28 16:09:22 +00:00
)
2015-09-07 15:39:22 +00:00
type Configuration struct {
Docker *DockerProvider
File *FileProvider
}
2015-08-28 16:09:22 +00:00
var srv *graceful.Server
2015-09-07 15:39:22 +00:00
var serviceRouter *mux.Router
2015-09-02 19:13:25 +00:00
var renderer = render.New()
2015-09-07 08:38:58 +00:00
var currentService = new(Service)
var serviceChan = make(chan *Service)
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)
systemRouter := mux.NewRouter()
2015-09-01 22:19:27 +00:00
systemRouter.Methods("POST").Path("/reload").HandlerFunc(ReloadConfigHandler)
systemRouter.Methods("GET").Path("/").HandlerFunc(GetConfigHandler)
2015-09-01 20:28:24 +00:00
go http.ListenAndServe(":8000", systemRouter)
2015-08-28 16:09:22 +00:00
2015-09-07 08:38:58 +00:00
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")
} else{
currentService = service
2015-09-07 15:39:22 +00:00
serviceRouter = LoadConfig(service)
srv.Stop(10 * time.Second)
time.Sleep(3 * time.Second)
}
2015-09-07 08:38:58 +00:00
}
}()
2015-09-07 21:25:07 +00:00
configuration := LoadFileConfig()
log.Println("Configuration loaded", configuration)
if(configuration.Docker != nil){
providers = append(providers, configuration.Docker)
}
if(configuration.File != nil){
providers = append(providers, configuration.File)
}
go func() {
for _, provider := range providers {
provider.Provide(serviceChan)
}
}()
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-07 08:38:58 +00:00
Addr: ":8001",
2015-09-07 15:39:22 +00:00
Handler: serviceRouter,
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
}
}
func LoadConfig(service *Service) *mux.Router {
2015-09-01 22:19:27 +00:00
router := mux.NewRouter()
2015-09-07 08:38:58 +00:00
for routeName, route := range service.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
}
for _, backendName := range route.Backends {
log.Println("Creating backend", backendName)
2015-09-01 22:19:27 +00:00
lb, _ := roundrobin.New(fwd)
2015-09-02 19:13:25 +00:00
rb, _ := roundrobin.NewRebalancer(lb)
2015-09-07 08:38:58 +00:00
for serverName, server := range service.Backends[backendName].Servers {
log.Println("Creating server", serverName)
2015-09-02 09:17:03 +00:00
url, _ := url.Parse(server.Url)
2015-09-02 19:13:25 +00:00
rb.UpsertServer(url)
}
for _, route := range newRoutes {
route.Handler(lb)
2015-09-01 22:19:27 +00:00
}
}
}
return router
}
2015-09-07 08:38:58 +00:00
func DeployService() {
2015-09-07 15:39:22 +00:00
serviceRouter = LoadConfig(currentService)
2015-09-07 08:38:58 +00:00
}
2015-09-01 22:19:27 +00:00
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
2015-09-07 08:38:58 +00:00
DeployService()
srv.Stop(10 * time.Second)
2015-09-02 19:13:25 +00:00
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "reloaded"})
2015-09-01 22:19:27 +00:00
}
func RestartHandler(rw http.ResponseWriter, r *http.Request) {
2015-09-02 19:13:25 +00:00
renderer.JSON(rw, http.StatusOK, map[string]interface{}{"status": "restarted"})
2015-08-28 16:09:22 +00:00
}
2015-09-01 22:19:27 +00:00
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
2015-09-07 08:38:58 +00:00
renderer.JSON(rw, http.StatusOK, currentService)
2015-08-28 16:09:22 +00:00
}
2015-09-02 09:17:03 +00:00
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
func LoadFileConfig() *Configuration {
configuration := new(Configuration)
if _, err := toml.DecodeFile("tortuous.toml", configuration); err != nil {
log.Fatal("Error reading file:", err)
}
return configuration
}