-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
http_server.go
54 lines (43 loc) · 919 Bytes
/
http_server.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
package admin
import (
"net"
"net/http"
"strings"
"github.com/rakyll/statik/fs"
_ "github.com/influxdb/influxdb/statik"
)
type HttpServer struct {
port string
listener net.Listener
closed bool
}
/*
homeDir is the directory that is the root of the admin site.
port should be a string that looks like ":8080" or whatever port to serve on.
*/
func NewHttpServer(port string) *HttpServer {
return &HttpServer{port: port, closed: true}
}
func (self *HttpServer) ListenAndServe() {
if self.port == "" {
return
}
self.closed = false
var err error
self.listener, err = net.Listen("tcp", self.port)
if err != nil {
panic(err)
}
statikFS, _ := fs.New()
err = http.Serve(self.listener, http.FileServer(statikFS))
if !strings.Contains(err.Error(), "closed") {
panic(err)
}
}
func (self *HttpServer) Close() {
if self.closed {
return
}
self.closed = true
self.listener.Close()
}