forked from adjust/redismq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
55 lines (47 loc) · 1.25 KB
/
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
55
package redismq
import (
"fmt"
"log"
"net/http"
)
// Server is the web server API for monitoring via JSON
type Server struct {
port string
observer *Observer
}
// NewServer returns a Server that can be started with Start()
func NewServer(redisHost, redisPort, redisPassword string, redisDb int64, port string) *Server {
observer := NewObserver(redisHost, redisPort, redisPassword, redisDb)
s := &Server{
port: port,
observer: observer,
}
return s
}
func (server *Server) setUpRoutes() {
http.Handle("/stats", newStatisticsHandler(server.observer))
}
// Start enables the Server to listen on his port
func (server *Server) Start() {
go func() {
server.setUpRoutes()
log.Printf("STARTING REDISMQ SERVER ON PORT %s", server.port)
err := http.ListenAndServe(":"+server.port, nil)
if err != nil {
log.Fatalf("REDISMQ SERVER SHUTTING DOWN [%s]\n\n", err.Error())
}
}()
}
type statisticsHandler struct {
*Observer
}
func newStatisticsHandler(observer *Observer) *statisticsHandler {
handler := &statisticsHandler{
Observer: observer,
}
return handler
}
func (handler *statisticsHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
handler.Observer.UpdateAllStats()
fmt.Fprintln(writer, handler.Observer.ToJSON())
}