traefik/tortuous.go

188 lines
4.2 KiB
Go
Raw Normal View History

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-02 09:17:03 +00:00
"reflect"
"net/url"
2015-09-02 19:13:25 +00:00
"github.com/fsouza/go-dockerclient"
"github.com/leekchan/gtf"
"bytes"
"github.com/unrolled/render"
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
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-09-02 19:13:25 +00:00
var renderer = render.New()
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-02 19:13:25 +00:00
func LoadDockerConfig(){
endpoint := "unix:///var/run/docker.sock"
client, _ := docker.NewClient(endpoint)
containerList, _ := client.ListContainers(docker.ListContainersOptions{})
containersInspected := []docker.Container{}
for _, container := range containerList {
containerInspected, _ := client.InspectContainer(container.ID)
containersInspected = append(containersInspected, *containerInspected)
}
containers := struct {
Containers []docker.Container
}{
containersInspected,
}
tmpl, err := gtf.New("docker.tmpl").ParseFiles("docker.tmpl")
if err != nil { panic(err) }
var buffer bytes.Buffer
err = tmpl.Execute(&buffer, containers)
if err != nil { panic(err) }
fmt.Println(buffer.String())
if _, err := toml.Decode(buffer.String(), config); err != nil {
fmt.Println(err)
return
}
}
func LoadFileConfig(){
if _, err := toml.DecodeFile("tortuous.toml", config); err != nil {
2015-09-01 22:19:27 +00:00
fmt.Println(err)
2015-09-02 19:13:25 +00:00
return
2015-09-01 22:19:27 +00:00
}
2015-09-02 19:13:25 +00:00
}
func LoadConfig() *mux.Router{
//LoadDockerConfig()
LoadFileConfig()
2015-09-01 22:19:27 +00:00
router := mux.NewRouter()
for routeName, route := range config.Routes {
fmt.Println("Creating route", routeName)
fwd, _ := forward.New()
2015-09-02 19:13:25 +00:00
newRoutes:= []*mux.Route{}
2015-09-01 22:19:27 +00:00
for ruleName, rule := range route.Rules{
fmt.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 {
fmt.Println("Creating backend", backendName)
lb, _ := roundrobin.New(fwd)
2015-09-02 19:13:25 +00:00
rb, _ := roundrobin.NewRebalancer(lb)
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)
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
}
func ReloadConfigHandler(rw http.ResponseWriter, r *http.Request) {
userRouter = LoadConfig()
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-08-28 16:09:22 +00:00
srv.Stop(10 * time.Second)
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-02 19:13:25 +00:00
renderer.JSON(rw, http.StatusOK, config)
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)
}