influxdb/services/admin/service.go

123 lines
2.8 KiB
Go
Raw Normal View History

2016-02-10 18:30:52 +00:00
package admin // import "github.com/influxdata/influxdb/services/admin"
2015-05-28 21:47:47 +00:00
2015-05-29 19:50:05 +00:00
import (
"crypto/tls"
2015-05-29 19:50:05 +00:00
"fmt"
"io"
"log"
2015-05-29 19:50:05 +00:00
"net"
"net/http"
"os"
2015-05-29 19:50:05 +00:00
"strings"
2015-05-28 21:47:47 +00:00
2015-05-29 19:50:05 +00:00
// Register static assets via statik.
_ "github.com/influxdata/influxdb/services/admin/statik"
2015-05-29 19:50:05 +00:00
"github.com/rakyll/statik/fs"
)
// Service manages the listener for an admin endpoint.
2015-05-28 21:47:47 +00:00
type Service struct {
2015-05-29 19:50:05 +00:00
listener net.Listener
addr string
https bool
cert string
2015-05-29 19:50:05 +00:00
err chan error
2016-02-20 02:36:47 +00:00
version string
logger *log.Logger
2015-05-28 21:47:47 +00:00
}
2015-05-29 19:50:05 +00:00
// NewService returns a new instance of Service.
func NewService(c Config) *Service {
return &Service{
2016-02-20 02:36:47 +00:00
addr: c.BindAddress,
https: c.HTTPSEnabled,
cert: c.HTTPSCertificate,
err: make(chan error),
version: c.Version,
logger: log.New(os.Stderr, "[admin] ", log.LstdFlags),
2015-05-29 19:50:05 +00:00
}
2015-05-28 21:47:47 +00:00
}
2015-05-29 19:50:05 +00:00
// Open starts the service
2015-05-28 21:47:47 +00:00
func (s *Service) Open() error {
s.logger.Printf("Starting admin service")
2015-05-29 19:50:05 +00:00
// Open listener.
if s.https {
cert, err := tls.LoadX509KeyPair(s.cert, s.cert)
if err != nil {
2015-07-23 22:00:33 +00:00
return err
}
listener, err := tls.Listen("tcp", s.addr, &tls.Config{
Certificates: []tls.Certificate{cert},
})
if err != nil {
2015-07-23 22:00:33 +00:00
return err
}
s.logger.Println("Listening on HTTPS:", listener.Addr().String())
s.listener = listener
2015-07-23 22:00:33 +00:00
} else {
listener, err := net.Listen("tcp", s.addr)
if err != nil {
return err
}
s.logger.Println("Listening on HTTP:", listener.Addr().String())
s.listener = listener
2015-05-29 19:50:05 +00:00
}
// Begin listening for requests in a separate goroutine.
go s.serve()
return nil
}
2015-05-28 21:47:47 +00:00
2015-05-29 19:50:05 +00:00
// Close closes the underlying listener.
func (s *Service) Close() error {
if s.listener != nil {
return s.listener.Close()
2015-05-28 21:47:47 +00:00
}
2015-05-29 19:50:05 +00:00
return nil
2015-05-28 21:47:47 +00:00
}
// SetLogOutput sets the writer to which all logs are written. It must not be
// called after Open is called.
func (s *Service) SetLogOutput(w io.Writer) {
s.logger = log.New(w, "[admin] ", log.LstdFlags)
}
2015-05-29 19:50:05 +00:00
// Err returns a channel for fatal errors that occur on the listener.
func (s *Service) Err() <-chan error { return s.err }
2015-05-28 21:47:47 +00:00
2015-05-29 19:50:05 +00:00
// Addr returns the listener's address. Returns nil if listener is closed.
func (s *Service) Addr() net.Addr {
if s.listener != nil {
return s.listener.Addr()
}
return nil
2015-05-28 21:47:47 +00:00
}
2015-05-29 19:50:05 +00:00
// serve serves the handler from the listener.
func (s *Service) serve() {
2016-02-20 02:36:47 +00:00
addVersionHeaderThenServe := func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("X-InfluxDB-Version", s.version)
h.ServeHTTP(w, r)
}
}
2015-05-29 19:50:05 +00:00
// Instantiate file system from embedded admin.
statikFS, err := fs.New()
if err != nil {
panic(err)
2015-05-28 21:47:47 +00:00
}
2015-05-29 19:50:05 +00:00
// Run file system handler on listener.
2016-02-20 02:36:47 +00:00
err = http.Serve(s.listener, addVersionHeaderThenServe(http.FileServer(statikFS)))
2015-05-29 19:50:05 +00:00
if err != nil && !strings.Contains(err.Error(), "closed") {
2015-05-30 14:57:27 +00:00
s.err <- fmt.Errorf("listener error: addr=%s, err=%s", s.Addr(), err)
2015-05-29 19:50:05 +00:00
}
}