forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
67 lines (56 loc) · 1.27 KB
/
admin.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
package admin
import (
"log"
"net"
"net/http"
"strings"
"sync"
"github.com/rakyll/statik/fs"
// Register static assets via statik.
_ "github.com/influxdb/influxdb/statik"
)
// Server manages InfluxDB's admin web server.
type Server struct {
mu sync.Mutex
addr string
listener net.Listener
closed bool
}
// NewServer constructs a new admin web server. The "addr" argument should be a
// string that looks like ":8083" or whatever addr to serve on.
func NewServer(addr string) *Server {
return &Server{addr: addr, closed: true}
}
// ListenAndServe starts the admin web server and serves requests until
// s.Close() is called.
func (s *Server) ListenAndServe() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.addr == "" {
return nil
}
var err error
s.listener, err = net.Listen("tcp", s.addr)
if err != nil {
return err
}
s.closed = false
statikFS, _ := fs.New()
go func() {
err = http.Serve(s.listener, http.FileServer(statikFS))
if !strings.Contains(err.Error(), "closed") {
log.Fatalf("admin server failed to server on %s: %s", s.addr, err)
}
}()
return err
}
// Close stops the admin web server.
func (s *Server) Close() error {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return nil
}
s.closed = true
return s.listener.Close()
}