forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
111 lines (94 loc) · 2.35 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package admin
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
// Register static assets via statik.
_ "github.com/influxdb/influxdb/statik"
"github.com/rakyll/statik/fs"
)
// Service manages the listener for an admin endpoint.
type Service struct {
listener net.Listener
addr string
https bool
cert string
err chan error
logger *log.Logger
}
// NewService returns a new instance of Service.
func NewService(c Config) *Service {
return &Service{
addr: c.BindAddress,
https: c.HTTPSEnabled,
cert: c.HTTPSCertificate,
err: make(chan error),
logger: log.New(os.Stderr, "[admin] ", log.LstdFlags),
}
}
// Open starts the service
func (s *Service) Open() error {
s.logger.Printf("Starting admin service")
// Open listener.
if s.https {
cert, err := tls.LoadX509KeyPair(s.cert, s.cert)
if err != nil {
return err
}
listener, err := tls.Listen("tcp", s.addr, &tls.Config{
Certificates: []tls.Certificate{cert},
})
if err != nil {
return err
}
s.logger.Println("Listening on HTTPS:", listener.Addr().String())
s.listener = listener
} 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
}
// Begin listening for requests in a separate goroutine.
go s.serve()
return nil
}
// Close closes the underlying listener.
func (s *Service) Close() error {
if s.listener != nil {
return s.listener.Close()
}
return nil
}
// SetLogger sets the internal logger to the logger passed in.
func (s *Service) SetLogger(l *log.Logger) {
s.logger = l
}
// Err returns a channel for fatal errors that occur on the listener.
func (s *Service) Err() <-chan error { return s.err }
// 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
}
// serve serves the handler from the listener.
func (s *Service) serve() {
// Instantiate file system from embedded admin.
statikFS, err := fs.New()
if err != nil {
panic(err)
}
// Run file system handler on listener.
err = http.Serve(s.listener, http.FileServer(statikFS))
if err != nil && !strings.Contains(err.Error(), "closed") {
s.err <- fmt.Errorf("listener error: addr=%s, err=%s", s.Addr(), err)
}
}