2015-08-28 16:09:22 +00:00
|
|
|
package main
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/tylerb/graceful"
|
|
|
|
"net/http"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2015-09-01 20:28:24 +00:00
|
|
|
"github.com/mailgun/oxy/forward"
|
|
|
|
"github.com/mailgun/oxy/roundrobin"
|
2015-08-28 16:09:22 +00:00
|
|
|
"time"
|
|
|
|
"net"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2015-09-01 20:28:24 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2015-09-01 22:19:27 +00:00
|
|
|
"encoding/json"
|
2015-09-02 09:17:03 +00:00
|
|
|
"reflect"
|
|
|
|
"net/url"
|
2015-08-28 16:09:22 +00:00
|
|
|
)
|
|
|
|
|
2015-09-01 20:28:24 +00:00
|
|
|
type Backend struct {
|
2015-09-02 09:17:03 +00:00
|
|
|
Servers map[string]Server
|
2015-09-01 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Server struct {
|
|
|
|
Url string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Rule struct {
|
|
|
|
Category string
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Route struct {
|
2015-09-01 20:56:43 +00:00
|
|
|
Backends []string
|
2015-09-01 20:28:24 +00:00
|
|
|
Rules map[string]Rule
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
2015-09-01 20:56:43 +00:00
|
|
|
Backends map[string]Backend
|
|
|
|
Servers map[string]Server
|
|
|
|
Routes map[string]Route
|
2015-09-01 20:28:24 +00:00
|
|
|
}
|
|
|
|
|
2015-08-28 16:09:22 +00:00
|
|
|
var srv *graceful.Server
|
2015-09-01 22:19:27 +00:00
|
|
|
var userRouter *mux.Router
|
|
|
|
var config = new(Config)
|
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("/restart").HandlerFunc(RestartHandler)
|
|
|
|
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-01 22:19:27 +00:00
|
|
|
userRouter = LoadConfig()
|
2015-08-28 16:09:22 +00:00
|
|
|
|
|
|
|
goAway := false
|
|
|
|
go func() {
|
|
|
|
sig := <-sigs
|
|
|
|
fmt.Println("I have to go...", sig)
|
|
|
|
goAway = true
|
|
|
|
srv.Stop(10 * time.Second)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for{
|
|
|
|
if (goAway){
|
|
|
|
break
|
|
|
|
}
|
|
|
|
srv = &graceful.Server{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
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{
|
|
|
|
Addr: ":8001",
|
|
|
|
Handler: userRouter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go srv.ListenAndServe()
|
2015-09-02 09:17:03 +00:00
|
|
|
fmt.Println("Started")
|
2015-08-28 16:09:22 +00:00
|
|
|
<- srv.StopChan()
|
|
|
|
fmt.Println("Stopped")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:19:27 +00:00
|
|
|
func LoadConfig() *mux.Router{
|
|
|
|
if metadata, err := toml.DecodeFile("tortuous.toml", config); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return nil
|
|
|
|
}else{
|
|
|
|
fmt.Printf("Loaded config: %+v\n", metadata )
|
|
|
|
}
|
|
|
|
|
|
|
|
router := mux.NewRouter()
|
|
|
|
for routeName, route := range config.Routes {
|
|
|
|
fmt.Println("Creating route", routeName)
|
|
|
|
fwd, _ := forward.New()
|
|
|
|
newRoute:= router.NewRoute()
|
|
|
|
for ruleName, rule := range route.Rules{
|
|
|
|
fmt.Println("Creating rule", ruleName)
|
2015-09-02 09:17:03 +00:00
|
|
|
newRoutes := Invoke(newRoute, rule.Category, rule.Value)
|
|
|
|
newRoute = newRoutes[0].Interface().(*mux.Route)
|
2015-09-01 22:19:27 +00:00
|
|
|
}
|
|
|
|
for _, backendName := range route.Backends {
|
|
|
|
fmt.Println("Creating backend", backendName)
|
|
|
|
lb, _ := roundrobin.New(fwd)
|
2015-09-02 09:17:03 +00:00
|
|
|
for serverName, server := range config.Backends[backendName].Servers {
|
2015-09-01 22:19:27 +00:00
|
|
|
fmt.Println("Creating server", serverName)
|
2015-09-02 09:17:03 +00:00
|
|
|
url, _ := url.Parse(server.Url)
|
|
|
|
lb.UpsertServer(url)
|
2015-09-01 22:19:27 +00:00
|
|
|
}
|
|
|
|
newRoute.Handler(lb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return router
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
userRouter = LoadConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
func RestartHandler(rw http.ResponseWriter, r *http.Request) {
|
2015-08-28 16:09:22 +00:00
|
|
|
srv.Stop(10 * time.Second)
|
|
|
|
}
|
|
|
|
|
2015-09-01 22:19:27 +00:00
|
|
|
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if jsonRes, err := json.Marshal(config); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}else{
|
|
|
|
fmt.Fprintf(rw, "%s", jsonRes)
|
|
|
|
}
|
2015-08-28 16:09:22 +00:00
|
|
|
}
|
2015-09-02 09:17:03 +00:00
|
|
|
|
|
|
|
func Invoke(any interface{}, name string, args... interface{}) []reflect.Value {
|
|
|
|
inputs := make([]reflect.Value, len(args))
|
|
|
|
for i, _ := range args {
|
|
|
|
inputs[i] = reflect.ValueOf(args[i])
|
|
|
|
}
|
|
|
|
return reflect.ValueOf(any).MethodByName(name).Call(inputs)
|
|
|
|
}
|
|
|
|
|