Skip to content

Commit

Permalink
helper method for external URL (influxdata#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathaniel Cook authored Jul 18, 2016
1 parent 7890a7d commit 10f53df
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion integrations/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func Bench(b *testing.B, tasksCount, pointCount int, db, rp, measurement, tickSc
// Setup HTTPD service
config := httpd.NewConfig()
config.BindAddress = ":0" // Choose port dynamically
httpdService := httpd.NewService(config, logService.NewLogger("[http] ", log.LstdFlags), logService)
httpdService := httpd.NewService(config, "localhost", logService.NewLogger("[http] ", log.LstdFlags), logService)

httpdService.Handler.AuthService = noauth.NewService(logService.NewLogger("[noauth] ", log.LstdFlags))
err := httpdService.Open()
Expand Down
2 changes: 1 addition & 1 deletion integrations/streamer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {
// create API server
config := httpd.NewConfig()
config.BindAddress = ":0" // Choose port dynamically
httpService = httpd.NewService(config, logService.NewLogger("[http] ", log.LstdFlags), logService)
httpService = httpd.NewService(config, "localhost", logService.NewLogger("[http] ", log.LstdFlags), logService)
err := httpService.Open()
if err != nil {
panic(err)
Expand Down
14 changes: 7 additions & 7 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ type BuildInfo struct {
// It is built using a Config and it manages the startup and shutdown of all
// services in the proper order.
type Server struct {
buildInfo BuildInfo
dataDir string
hostname string
dataDir string
hostname string

config *Config

Expand All @@ -81,6 +80,7 @@ type Server struct {

Services []Service

BuildInfo BuildInfo
ClusterID string
ServerID string

Expand All @@ -101,7 +101,7 @@ func New(c *Config, buildInfo BuildInfo, logService logging.Interface) (*Server,
l := logService.NewLogger("[srv] ", log.LstdFlags)
s := &Server{
config: c,
buildInfo: buildInfo,
BuildInfo: buildInfo,
dataDir: c.DataDir,
hostname: c.Hostname,
err: make(chan error),
Expand All @@ -122,7 +122,7 @@ func New(c *Config, buildInfo BuildInfo, logService logging.Interface) (*Server,
kapacitor.ServerIDVar.Set(s.ServerID)
kapacitor.HostVar.Set(s.hostname)
kapacitor.ProductVar.Set(kapacitor.Product)
kapacitor.VersionVar.Set(s.buildInfo.Version)
kapacitor.VersionVar.Set(s.BuildInfo.Version)
s.Logger.Printf("I! ClusterID: %s ServerID: %s", s.ClusterID, s.ServerID)

// Start Task Master
Expand Down Expand Up @@ -175,10 +175,10 @@ func (s *Server) AppendInfluxDBService() error {

func (s *Server) InitHTTPDService() {
l := s.LogService.NewLogger("[httpd] ", log.LstdFlags)
srv := httpd.NewService(s.config.HTTP, l, s.LogService)
srv := httpd.NewService(s.config.HTTP, s.hostname, l, s.LogService)

srv.Handler.PointsWriter = s.TaskMaster
srv.Handler.Version = s.buildInfo.Version
srv.Handler.Version = s.BuildInfo.Version

s.HTTPDService = srv
s.TaskMaster.HTTPDService = srv
Expand Down
23 changes: 21 additions & 2 deletions services/httpd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"log"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
Expand All @@ -21,6 +22,8 @@ type Service struct {
cert string
err chan error

externalURL string

server *http.Server
mu sync.Mutex
wg sync.WaitGroup
Expand All @@ -37,14 +40,23 @@ type Service struct {
logger *log.Logger
}

func NewService(c Config, l *log.Logger, li logging.Interface) *Service {
func NewService(c Config, hostname string, l *log.Logger, li logging.Interface) *Service {
statMap := &expvar.Map{}
statMap.Init()
port, _ := c.Port()
u := url.URL{
Host: fmt.Sprintf("%s:%d", hostname, port),
Scheme: "http",
}
if c.HttpsEnabled {
u.Scheme = "https"
}
s := &Service{
addr: c.BindAddress,
https: c.HttpsEnabled,
cert: c.HttpsCertificate,
err: make(chan error),
externalURL: u.String(),
err: make(chan error, 1),
shutdownTimeout: time.Duration(c.ShutdownTimeout),
Handler: NewHandler(
c.AuthEnabled,
Expand Down Expand Up @@ -252,6 +264,13 @@ func (s *Service) URL() string {
}
return ""
}

// URL that should resolve externally to the server HTTP endpoint.
// It is possible that the URL does not resolve correctly if the hostname config setting is incorrect.
func (s *Service) ExternalURL() string {
return s.externalURL
}

func (s *Service) AddRoutes(routes []Route) error {
return s.Handler.AddRoutes(routes)
}
Expand Down

0 comments on commit 10f53df

Please sign in to comment.