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"
|
2015-09-07 13:25:13 +00:00
|
|
|
"log"
|
2015-09-07 15:39:22 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-08-28 16:09:22 +00:00
|
|
|
)
|
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
type FileConfiguration struct {
|
2015-09-07 15:39:22 +00:00
|
|
|
Docker *DockerProvider
|
|
|
|
File *FileProvider
|
|
|
|
}
|
|
|
|
|
2015-08-28 16:09:22 +00:00
|
|
|
var srv *graceful.Server
|
2015-09-07 22:15:14 +00:00
|
|
|
var configurationRouter *mux.Router
|
2015-09-02 19:13:25 +00:00
|
|
|
var renderer = render.New()
|
2015-09-07 22:15:14 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
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 {
|
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)){
|
|
|
|
log.Println("Skipping same configuration")
|
2015-09-07 13:25:13 +00:00
|
|
|
} else{
|
2015-09-07 22:15:14 +00:00
|
|
|
currentConfiguration = configuration
|
|
|
|
configurationRouter = LoadConfig(configuration)
|
2015-09-07 13:25:13 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
for _, provider := range providers {
|
|
|
|
go func() {
|
|
|
|
provider.Provide(configurationChan)
|
|
|
|
}()
|
|
|
|
}
|
2015-08-28 16:09:22 +00:00
|
|
|
|
|
|
|
goAway := false
|
|
|
|
go func() {
|
|
|
|
sig := <-sigs
|
2015-09-07 13:25:13 +00:00
|
|
|
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 22:15:14 +00:00
|
|
|
Handler: configurationRouter,
|
2015-08-28 16:09:22 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go srv.ListenAndServe()
|
2015-09-07 13:25:13 +00:00
|
|
|
log.Println("Started")
|
2015-09-07 08:38:58 +00:00
|
|
|
<-srv.StopChan()
|
2015-09-07 13:25:13 +00:00
|
|
|
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()
|
2015-09-07 22:15:14 +00:00
|
|
|
for routeName, route := range configuration.Routes {
|
2015-09-07 13:25:13 +00:00
|
|
|
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 {
|
2015-09-07 13:25:13 +00:00
|
|
|
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 {
|
2015-09-07 13:25:13 +00:00
|
|
|
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 22:15:14 +00:00
|
|
|
for serverName, server := range configuration.Backends[backendName].Servers {
|
2015-09-07 13:25:13 +00:00
|
|
|
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 22:15:14 +00:00
|
|
|
configurationRouter = LoadConfig(currentConfiguration)
|
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 22:15:14 +00:00
|
|
|
renderer.JSON(rw, http.StatusOK, currentConfiguration)
|
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
|
|
|
|
2015-09-07 22:15:14 +00:00
|
|
|
func LoadFileConfig() *FileConfiguration {
|
|
|
|
configuration := new(FileConfiguration)
|
2015-09-07 15:39:22 +00:00
|
|
|
if _, err := toml.DecodeFile("tortuous.toml", configuration); err != nil {
|
|
|
|
log.Fatal("Error reading file:", err)
|
|
|
|
}
|
|
|
|
return configuration
|
|
|
|
}
|