Config loaded and working
This commit is contained in:
parent
b5828f89e2
commit
788fff4803
1 changed files with 49 additions and 25 deletions
74
tortuous.go
74
tortuous.go
|
@ -13,6 +13,7 @@ import (
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
|
"encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Backend struct {
|
type Backend struct {
|
||||||
|
@ -40,37 +41,21 @@ type Config struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var srv *graceful.Server
|
var srv *graceful.Server
|
||||||
|
var userRouter *mux.Router
|
||||||
|
var config = new(Config)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
var config Config
|
|
||||||
if _, err := toml.DecodeFile("tortuous.toml", &config); err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Printf("%+v\n", config )
|
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
systemRouter := mux.NewRouter()
|
systemRouter := mux.NewRouter()
|
||||||
systemRouter.Methods("POST").Path("/").HandlerFunc(ReloadHandler)
|
systemRouter.Methods("POST").Path("/restart").HandlerFunc(RestartHandler)
|
||||||
systemRouter.Methods("GET").Path("/").HandlerFunc(GetPidHandler)
|
systemRouter.Methods("POST").Path("/reload").HandlerFunc(ReloadConfigHandler)
|
||||||
|
systemRouter.Methods("GET").Path("/").HandlerFunc(GetConfigHandler)
|
||||||
go http.ListenAndServe(":8000", systemRouter)
|
go http.ListenAndServe(":8000", systemRouter)
|
||||||
|
|
||||||
userRouter := mux.NewRouter()
|
userRouter = LoadConfig()
|
||||||
|
|
||||||
/*for i := range config.Routes {
|
|
||||||
fmt.Printf("%+v\n", config.Routes[i] )
|
|
||||||
}*/
|
|
||||||
|
|
||||||
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)
|
|
||||||
|
|
||||||
goAway := false
|
goAway := false
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -105,10 +90,49 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReloadHandler(rw http.ResponseWriter, r *http.Request) {
|
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) {
|
||||||
srv.Stop(10 * time.Second)
|
srv.Stop(10 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetPidHandler(rw http.ResponseWriter, r *http.Request) {
|
func GetConfigHandler(rw http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(rw, "%d", os.Getpid())
|
if jsonRes, err := json.Marshal(config); err != nil {
|
||||||
|
fmt.Println(err)
|
||||||
|
return
|
||||||
|
}else{
|
||||||
|
fmt.Fprintf(rw, "%s", jsonRes)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue