Config loaded and working

This commit is contained in:
emile 2015-09-02 11:17:03 +02:00
parent 788fff4803
commit f184d62953
2 changed files with 30 additions and 16 deletions

View file

@ -7,17 +7,18 @@ import (
"os" "os"
"github.com/mailgun/oxy/forward" "github.com/mailgun/oxy/forward"
"github.com/mailgun/oxy/roundrobin" "github.com/mailgun/oxy/roundrobin"
"github.com/mailgun/oxy/testutils"
"time" "time"
"net" "net"
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
"encoding/json" "encoding/json"
"reflect"
"net/url"
) )
type Backend struct { type Backend struct {
Servers []string Servers map[string]Server
} }
type Server struct { type Server struct {
@ -45,7 +46,6 @@ var userRouter *mux.Router
var config = new(Config) var config = new(Config)
func main() { func main() {
sigs := make(chan os.Signal, 1) sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
@ -69,13 +69,12 @@ func main() {
if (goAway){ if (goAway){
break break
} }
fmt.Println("Started")
srv = &graceful.Server{ srv = &graceful.Server{
Timeout: 10 * time.Second, Timeout: 10 * time.Second,
NoSignalHandling: true, NoSignalHandling: true,
ConnState: func(conn net.Conn, state http.ConnState) { ConnState: func(conn net.Conn, state http.ConnState) {
fmt.Println( "Connection ", state) // conn has a new state
}, },
Server: &http.Server{ Server: &http.Server{
@ -85,6 +84,7 @@ func main() {
} }
go srv.ListenAndServe() go srv.ListenAndServe()
fmt.Println("Started")
<- srv.StopChan() <- srv.StopChan()
fmt.Println("Stopped") fmt.Println("Stopped")
} }
@ -105,14 +105,16 @@ func LoadConfig() *mux.Router{
newRoute:= router.NewRoute() newRoute:= router.NewRoute()
for ruleName, rule := range route.Rules{ for ruleName, rule := range route.Rules{
fmt.Println("Creating rule", ruleName) fmt.Println("Creating rule", ruleName)
newRoute = newRoute.Host(rule.Value) newRoutes := Invoke(newRoute, rule.Category, rule.Value)
newRoute = newRoutes[0].Interface().(*mux.Route)
} }
for _, backendName := range route.Backends { for _, backendName := range route.Backends {
fmt.Println("Creating backend", backendName) fmt.Println("Creating backend", backendName)
lb, _ := roundrobin.New(fwd) lb, _ := roundrobin.New(fwd)
for _, serverName := range config.Backends[backendName].Servers { for serverName, server := range config.Backends[backendName].Servers {
fmt.Println("Creating server", serverName) fmt.Println("Creating server", serverName)
lb.UpsertServer(testutils.ParseURI(config.Servers[serverName].Url)) url, _ := url.Parse(server.Url)
lb.UpsertServer(url)
} }
newRoute.Handler(lb) newRoute.Handler(lb)
} }
@ -136,3 +138,12 @@ func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
fmt.Fprintf(rw, "%s", jsonRes) fmt.Fprintf(rw, "%s", jsonRes)
} }
} }
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)
}

View file

@ -1,16 +1,19 @@
[backends] [backends]
[backends.backend1] [backends.backend1]
servers = ["server1", "server2"] [backends.backend1.servers.server1]
url = "http://172.17.0.3:80"
[servers] [backends.backend1.servers.server2]
[servers.server1] url = "http://172.17.0.3:80"
url = "http://172.17.0.2:80"
[servers.server2]
url = "http://172.17.0.3:80"
[routes] [routes]
[routes.route1] [routes.route1]
backends = ["backend1"] backends = ["backend1"]
[routes.route1.rules.test_zenika] [routes.route1.rules.test_zenika_1]
category = "Host" category = "Host"
value = "test.zenika.fr" value = "test.zenika.fr"
[routes.route2]
backends = ["backend1"]
[routes.route2.rules.test_zenika_2]
category = "Path"
value = "/test"