-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
131 lines (119 loc) · 2.71 KB
/
main.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"bitbucket.org/kardianos/service"
"encoding/json"
"flag"
"fmt"
"github.com/laslowh/statsgod/server"
"net"
"net/http"
"os"
"strconv"
"sync"
)
var fConfigFile = flag.String("c", "/opt/statsgod/statsgod.config", "path to config file")
var fStatsPort = flag.Int64("p", 16536, "stats listen port (UDP)")
var fHttpPort = flag.Int64("h", 16530, "HTTP listen port")
func main() {
flag.Parse()
var displayName = "StatsGod statistics server"
var desc = "Aggregates and serves statistics"
var ws, err = service.NewService("statsgod", displayName, desc)
if err != nil {
fmt.Fprintf(os.Stderr, "%s unable to start: %s", displayName, err)
return
}
if len(flag.Args()) > 0 {
var err error
verb := flag.Args()[0]
switch verb {
case "install":
err = ws.Install()
if err != nil {
fmt.Fprintf(os.Stderr, "%s: failed to install %q\n", err, displayName)
return
}
ws.LogInfo("%q: service installed.", displayName)
case "remove":
err = ws.Remove()
if err != nil {
fmt.Printf("Failed to remove: %s\n", err)
return
}
ws.LogInfo("%q: service removed.", displayName)
default:
fmt.Fprintf(os.Stderr, "%s: unknown command", verb)
}
return
}
err = ws.Run(func() error {
// start
err := doStart()
if err != nil {
return err
}
ws.LogInfo("%q: running", displayName)
return nil
}, func() error {
// stop
doStop()
ws.LogInfo("%q: stopping", displayName)
return nil
})
if err != nil {
ws.LogError(err.Error())
}
}
func doStart() error {
cf, err := server.ReadConfig(*fConfigFile)
if err != nil {
return err
}
s := server.NewServer(cf, *fConfigFile, *fStatsPort)
http.HandleFunc("/data", func(w http.ResponseWriter, r *http.Request) {
dataHandler(w, r, s.FuncChan)
})
tcpAddress, err := net.ResolveTCPAddr("tcp", ":"+strconv.FormatInt(*fHttpPort, 10))
if err != nil {
return err
}
tcpConn, err := net.ListenTCP("tcp", tcpAddress)
if err != nil {
return err
}
go func() {
http.Serve(tcpConn, nil)
}()
go s.Loop()
return nil
}
func doStop() {
}
func dataHandler(w http.ResponseWriter, r *http.Request, coreChan chan func(c *server.Core)) {
k := r.FormValue("k")
if k == "" {
http.Error(w, "'k' required", http.StatusBadRequest)
return
}
var wg sync.WaitGroup
wg.Add(1)
coreChan <- func(c *server.Core) {
defer wg.Done()
h := w.Header()
h.Set("Access-Control-Allow-Origin", "*")
h.Set("Content-Type", "application/json")
s, ok := c.Stats[k]
if !ok {
http.Error(w, "{}", http.StatusNotFound)
return
}
var values []server.Datum
s.CopyValues(&values)
enc := json.NewEncoder(w)
err := enc.Encode(values)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
wg.Wait()
}