traefik/tortuous.go

115 lines
2.1 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"
"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-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
func main() {
2015-09-01 20:28:24 +00:00
var config Config
if _, err := toml.DecodeFile("tortuous.toml", &config); err != nil {
fmt.Println(err)
return
}
fmt.Printf("%+v\n", config )
2015-08-28 16:09:22 +00:00
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
systemRouter := mux.NewRouter()
systemRouter.Methods("POST").Path("/").HandlerFunc(ReloadHandler)
systemRouter.Methods("GET").Path("/").HandlerFunc(GetPidHandler)
2015-09-01 20:28:24 +00:00
go http.ListenAndServe(":8000", systemRouter)
2015-08-28 16:09:22 +00:00
2015-09-01 20:56:43 +00:00
userRouter := mux.NewRouter()
2015-08-28 16:09:22 +00:00
2015-09-01 20:56:43 +00:00
/*for i := range config.Routes {
fmt.Printf("%+v\n", config.Routes[i] )
}*/
2015-09-01 20:28:24 +00:00
fwd, _ := forward.New()
lb, _ := roundrobin.New(fwd)
lb.UpsertServer(testutils.ParseURI("http://172.17.0.2:80"))
lb.UpsertServer(testutils.ParseURI("http://172.17.0.3:80"))
userRouter.Host("test.zenika.fr").Handler(lb)
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")
}
}
func ReloadHandler(rw http.ResponseWriter, r *http.Request) {
srv.Stop(10 * time.Second)
}
func GetPidHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "%d", os.Getpid())
}