From 24d17e67c1941c950e4dd327d76f64268bc96d47 Mon Sep 17 00:00:00 2001
From: Guillem Jover <guillem@hadrons.org>
Date: Fri, 17 Aug 2018 16:57:12 +0200
Subject: [PATCH] influxd: Add support for multiple http services

We switch from http config stanza to support an array of http entries,
which is a backwards incompatible change.

We remove the http members from the Server struct, as they are unused,
and we are tracking those per HTTPD instance.

FIXME: The pprof-enabled setting is taken from the first HTTPD instance,
because that one is global, this needs a better fix, probably moving it
into another config section.
---
 cmd/influxd/run/command.go             |  2 +-
 cmd/influxd/run/config.go              |  4 ++--
 cmd/influxd/run/config_test.go         | 15 +++++++++++----
 cmd/influxd/run/server.go              | 12 +++---------
 cmd/influxd/run/server_helpers_test.go |  6 +++---
 5 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/cmd/influxd/run/command.go b/cmd/influxd/run/command.go
index 51036f146..f6d04b39e 100644
--- a/cmd/influxd/run/command.go
+++ b/cmd/influxd/run/command.go
@@ -97,7 +97,7 @@ func (cmd *Command) Run(args ...string) error {
 		return fmt.Errorf("%s. To generate a valid configuration file run `influxd config > influxdb.generated.conf`", err)
 	}
 
-	if config.HTTPD.PprofEnabled {
+	if config.HTTPD[0].PprofEnabled {
 		// Turn on block profiling to debug stuck databases
 		runtime.SetBlockProfileRate(int(1 * time.Second))
 	}
diff --git a/cmd/influxd/run/config.go b/cmd/influxd/run/config.go
index 36e4f14e1..ff6526a4d 100644
--- a/cmd/influxd/run/config.go
+++ b/cmd/influxd/run/config.go
@@ -47,7 +47,7 @@ type Config struct {
 	Admin          admin.Config      `toml:"admin"`
 	Monitor        monitor.Config    `toml:"monitor"`
 	Subscriber     subscriber.Config `toml:"subscriber"`
-	HTTPD          httpd.Config      `toml:"http"`
+	HTTPD          []httpd.Config    `toml:"http"`
 	GraphiteInputs []graphite.Config `toml:"graphite"`
 	CollectdInputs []collectd.Config `toml:"collectd"`
 	OpenTSDBInputs []opentsdb.Config `toml:"opentsdb"`
@@ -73,8 +73,8 @@ func NewConfig() *Config {
 	c.Admin = admin.NewConfig()
 	c.Monitor = monitor.NewConfig()
 	c.Subscriber = subscriber.NewConfig()
-	c.HTTPD = httpd.NewConfig()
 
+	c.HTTPD = []httpd.Config{httpd.NewConfig()}
 	c.GraphiteInputs = []graphite.Config{graphite.NewConfig()}
 	c.CollectdInputs = []collectd.Config{collectd.NewConfig()}
 	c.OpenTSDBInputs = []opentsdb.Config{opentsdb.NewConfig()}
diff --git a/cmd/influxd/run/config_test.go b/cmd/influxd/run/config_test.go
index ef261859c..50c664f72 100644
--- a/cmd/influxd/run/config_test.go
+++ b/cmd/influxd/run/config_test.go
@@ -24,9 +24,12 @@ dir = "/tmp/data"
 [admin]
 bind-address = ":8083"
 
-[http]
+[[http]]
 bind-address = ":8087"
 
+[[http]]
+bind-address = ":8187"
+
 [[graphite]]
 protocol = "udp"
 
@@ -70,8 +73,12 @@ enabled = true
 		t.Fatalf("unexpected data dir: %s", c.Data.Dir)
 	} else if c.Admin.BindAddress != ":8083" {
 		t.Fatalf("unexpected admin bind address: %s", c.Admin.BindAddress)
-	} else if c.HTTPD.BindAddress != ":8087" {
-		t.Fatalf("unexpected api bind address: %s", c.HTTPD.BindAddress)
+	} else if len(c.HTTPD) != 2 {
+		t.Fatalf("unexpected HTTPD count: %d", len(c.HTTPD))
+	} else if c.HTTPD[0].BindAddress != ":8087" {
+		t.Fatalf("unexpected api bind address: %s", c.HTTPD[0].BindAddress)
+	} else if c.HTTPD[1].BindAddress != ":8187" {
+		t.Fatalf("unexpected api bind address: %s", c.HTTPD[1].BindAddress)
 	} else if len(c.GraphiteInputs) != 2 {
 		t.Fatalf("unexpected graphiteInputs count: %d", len(c.GraphiteInputs))
 	} else if c.GraphiteInputs[0].Protocol != "udp" {
@@ -113,7 +120,7 @@ dir = "/tmp/data"
 [admin]
 bind-address = ":8083"
 
-[http]
+[[http]]
 bind-address = ":8087"
 
 [[graphite]]
diff --git a/cmd/influxd/run/server.go b/cmd/influxd/run/server.go
index 225aab804..b038ced89 100644
--- a/cmd/influxd/run/server.go
+++ b/cmd/influxd/run/server.go
@@ -84,12 +84,6 @@ type Server struct {
 	CPUProfile string
 	MemProfile string
 
-	// httpAPIAddr is the host:port combination for the main HTTP API for querying and writing data
-	httpAPIAddr string
-
-	// httpUseTLS specifies if we should use a TLS connection to the http servers
-	httpUseTLS bool
-
 	// tcpAddr is the host:port combination for the TCP listener that services mux onto
 	tcpAddr string
 
@@ -149,8 +143,6 @@ func NewServer(c *Config, buildInfo *BuildInfo) (*Server, error) {
 
 		reportingDisabled: c.ReportingDisabled,
 
-		httpAPIAddr: c.HTTPD.BindAddress,
-		httpUseTLS:  c.HTTPD.HTTPSEnabled,
 		tcpAddr:     bind,
 
 		config:    c,
@@ -369,7 +361,9 @@ func (s *Server) Open() error {
 	s.appendSnapshotterService()
 	s.appendAdminService(s.config.Admin)
 	s.appendContinuousQueryService(s.config.ContinuousQuery)
-	s.appendHTTPDService(s.config.HTTPD)
+	for _, i := range s.config.HTTD {
+		s.appendHTTPDService(i)
+	}
 	s.appendRetentionPolicyService(s.config.Retention)
 	for _, i := range s.config.GraphiteInputs {
 		if err := s.appendGraphiteService(i); err != nil {
diff --git a/cmd/influxd/run/server_helpers_test.go b/cmd/influxd/run/server_helpers_test.go
index e46548f38..c30ecd1ac 100644
--- a/cmd/influxd/run/server_helpers_test.go
+++ b/cmd/influxd/run/server_helpers_test.go
@@ -240,9 +240,9 @@ func NewConfig() *run.Config {
 	c.Data.Dir = MustTempFile()
 	c.Data.WALDir = MustTempFile()
 
-	c.HTTPD.Enabled = true
-	c.HTTPD.BindAddress = "127.0.0.1:0"
-	c.HTTPD.LogEnabled = testing.Verbose()
+	c.HTTPD[0].Enabled = true
+	c.HTTPD[0].BindAddress = "127.0.0.1:0"
+	c.HTTPD[0].LogEnabled = testing.Verbose()
 
 	c.Monitor.StoreEnabled = false
 
-- 
2.18.0

