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"
|
|
|
|
"github.com/mailgun/oxy/testutils"
|
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-08-28 16:09:22 +00:00
|
|
|
)
|
|
|
|
|
2015-09-01 20:28:24 +00:00
|
|
|
type Backend struct {
|
|
|
|
Servers []string
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2015-09-01 20:28:24 +00:00
|
|
|
|
2015-08-28 16:09:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
fmt.Println("Started")
|
|
|
|
srv = &graceful.Server{
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
NoSignalHandling: true,
|
|
|
|
|
|
|
|
ConnState: func(conn net.Conn, state http.ConnState) {
|
|
|
|
fmt.Println( "Connection ", state)
|
|
|
|
},
|
|
|
|
|
|
|
|
Server: &http.Server{
|
|
|
|
Addr: ":8001",
|
|
|
|
Handler: userRouter,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
go srv.ListenAndServe()
|
|
|
|
<- 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)
|
|
|
|
newRoute = newRoute.Host(rule.Value)
|
|
|
|
}
|
|
|
|
for _, backendName := range route.Backends {
|
|
|
|
fmt.Println("Creating backend", backendName)
|
|
|
|
lb, _ := roundrobin.New(fwd)
|
|
|
|
for _, serverName := range config.Backends[backendName].Servers {
|
|
|
|
fmt.Println("Creating server", serverName)
|
|
|
|
lb.UpsertServer(testutils.ParseURI(config.Servers[serverName].Url))
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|