Browse Source

proxy: add proxy-mode functionality to etcd daemon

Brian Waldon 11 years ago
parent
commit
7415d53020
1 changed files with 21 additions and 5 deletions
  1. 21 5
      main.go

+ 21 - 5
main.go

@@ -12,6 +12,7 @@ import (
 
 	"github.com/coreos/etcd/etcdserver"
 	"github.com/coreos/etcd/etcdserver/etcdhttp"
+	"github.com/coreos/etcd/proxy"
 	"github.com/coreos/etcd/raft"
 	"github.com/coreos/etcd/store"
 	"github.com/coreos/etcd/wal"
@@ -23,10 +24,11 @@ const (
 )
 
 var (
-	fid     = flag.String("id", "0x1", "ID of this server")
-	timeout = flag.Duration("timeout", 10*time.Second, "Request Timeout")
-	laddr   = flag.String("l", ":8080", "HTTP service address (e.g., ':8080')")
-	dir     = flag.String("data-dir", "", "Path to the data directory")
+	fid       = flag.String("id", "0x1", "ID of this server")
+	timeout   = flag.Duration("timeout", 10*time.Second, "Request Timeout")
+	laddr     = flag.String("l", ":8080", "HTTP service address (e.g., ':8080')")
+	dir       = flag.String("data-dir", "", "Path to the data directory")
+	proxyMode = flag.Bool("proxy-mode", false, "Forward HTTP requests to peers, do not participate in raft.")
 
 	peers = &etcdhttp.Peers{}
 )
@@ -39,7 +41,12 @@ func init() {
 func main() {
 	flag.Parse()
 
-	h := startEtcd()
+	var h http.Handler
+	if *proxyMode {
+		h = startProxy()
+	} else {
+		h = startEtcd()
+	}
 
 	http.Handle("/", h)
 	log.Fatal(http.ListenAndServe(*laddr, nil))
@@ -115,3 +122,12 @@ func startRaft(id int64, peerIDs []int64, waldir string) (raft.Node, *wal.WAL) {
 	n := raft.Restart(id, peerIDs, 10, 1, st, ents)
 	return n, w
 }
+
+func startProxy() http.Handler {
+	h, err := proxy.NewHandler((*peers).Endpoints())
+	if err != nil {
+		log.Fatal(err)
+	}
+
+	return h
+}