forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
51 lines (40 loc) · 793 Bytes
/
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
package admin
import (
"net"
"net/http"
"strings"
"github.com/rakyll/statik/fs"
_ "github.com/influxdb/influxdb/statik"
)
type Server struct {
port string
listener net.Listener
closed bool
}
// port should be a string that looks like ":8083" or whatever port to serve on.
func NewServer(port string) *Server {
return &Server{port: port, closed: true}
}
func (s *Server) ListenAndServe() {
if s.port == "" {
return
}
var err error
s.listener, err = net.Listen("tcp", s.port)
if err != nil {
return
}
s.closed = false
statikFS, _ := fs.New()
err = http.Serve(s.listener, http.FileServer(statikFS))
if !strings.Contains(err.Error(), "closed") {
panic(err)
}
}
func (s *Server) Close() {
if s.closed {
return
}
s.closed = true
s.listener.Close()
}