コード例 #1
0
ファイル: hprctl.go プロジェクト: ncode/hot-potato-router
func main() {
	usage := `Hot Potato Router Control

Usage:
  hprctl add <vhost> <backend_ip:port> [--weight=<n>]
  hprctl del <vhost> <backend_ip:port> [--weight=<n>]
  hprctl show <vhost>
  hprctl list

Args:
  add           add a new vhost and backend
  dell          del a vhost and a backend
  show          show all backends from a given vhost
  list          list all vhosts

Options:
  -h --help     Show this screen.
  --version     Show version.
  --weight=<n>  Weight in wrr [default: 1].
`

	arguments, _ := docopt.Parse(usage, nil, true, "Hot Potato Router 0.3.0", false)
	if arguments["add"] == true {
		_, err := rc.ZAdd(fmt.Sprintf("hpr-backends::%s", arguments["<vhost>"]), arguments["--weight"], arguments["<backend_ip:port>"])
		hpr_utils.CheckPanic(err, "Unable to write on hpr database")
		return
	}

	if arguments["del"] == true {
		_, err := rc.ZRem(fmt.Sprintf("hpr-backends::%s", arguments["<vhost>"]), arguments["--weight"], arguments["<backend_ip:port>"])
		hpr_utils.CheckPanic(err, "Unable to write on hpr database")
		return
	}

	if arguments["show"] == true {
		bes, err := rc.ZRange(fmt.Sprintf("hpr-backends::%s", arguments["<vhost>"]), 0, -1, true)
		hpr_utils.CheckPanic(err, "Unable to write on hpr database")
		fmt.Printf(":: vhost [ %s ]\n", arguments["<vhost>"])
		var url string
		for _, be := range bes {
			count, err := strconv.Atoi(be)
			if err != nil {
				url = be
				continue
			}
			fmt.Printf("-- backend %s weight=%d\n", url, count)
		}
		return
	}

	if arguments["list"] == true {
		keys, err := rc.Keys("hpr-backends::*")
		hpr_utils.CheckPanic(err, "Unable to write on hpr database")
		for _, k := range keys {
			fmt.Printf(":: vhost [ %s ]\n", strings.TrimPrefix(k, "hpr-backends::"))
		}
		return
	}
}
コード例 #2
0
ファイル: hpr.go プロジェクト: ncode/hot-potato-router
func main() {
	utils.Log("Starting Hot Potato Router...")
	probe_interval, _ := strconv.Atoi(cfg.Options["hpr"]["probe_interval"])
	if probe_interval == 0 {
		probe_interval = 10
	}
	s, err := http_server.NewServer(time.Duration(probe_interval) * time.Second)
	utils.CheckPanic(err, "Unable to spawn")

	if cfg.Options["hpr"]["https_addr"] != "" {
		cert, err := tls.LoadX509KeyPair(cfg.Options["hpr"]["cert_file"], cfg.Options["hpr"]["key_file"])
		utils.CheckPanic(err, "Unable to load certificate")

		c := &tls.Config{Certificates: []tls.Certificate{cert}}
		l := tls.NewListener(http_server.Listen(cfg.Options["hpr"]["https_addr"]), c)
		go func() {
			utils.CheckPanic(http.Serve(l, s), "Problem with https server")
		}()
	}
	utils.CheckPanic(
		http.Serve(http_server.Listen(cfg.Options["hpr"]["http_addr"]), s),
		"Problem with http server")
}