-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadmin.go
More file actions
51 lines (48 loc) · 900 Bytes
/
admin.go
File metadata and controls
51 lines (48 loc) · 900 Bytes
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 gwork
import (
"fmt"
"io"
"net"
)
// Start admin server.
// Handle admin commands by TCP protocol.
func adminStart() {
go func() {
l, err := net.Listen("tcp", ":"+conf.AdminPort)
if err != nil {
Log(LogLevelError, err.Error())
}
defer l.Close()
for {
conn, err := l.Accept()
if err != nil {
Log(LogLevelError, err)
}
go handleCommand(conn)
}
}()
}
func handleCommand(c net.Conn) {
defer c.Close()
for {
inBuf := make([]byte, 128)
n, err := c.Read(inBuf)
if err != nil {
if err != io.EOF {
fmt.Fprintf(c, "read command error: %s\n", err)
}
continue
}
cmd := string(inBuf[:n-2])
Logf(LogLevelInfo, "admin command: %s", cmd)
switch cmd {
case "stats":
outBuf := StatsReport()
fmt.Fprintln(c, outBuf)
case "quit": // close connection
return
default:
fmt.Fprintf(c, "unknown admin command: %s\n", cmd)
}
}
}