summaryrefslogtreecommitdiff
path: root/internal/server/handlers/healthhandler.go
blob: 5abb3f49e7d925b828bf14b4c84b1cef82e93f6d (plain)
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
package handlers

import (
	"context"
	"strings"

	"github.com/mimecast/dtail/internal"
	"github.com/mimecast/dtail/internal/config"
	"github.com/mimecast/dtail/internal/io/dlog"
	"github.com/mimecast/dtail/internal/io/line"
	"github.com/mimecast/dtail/internal/lcontext"
	user "github.com/mimecast/dtail/internal/user/server"
)

// HealthHandler is for the remote health check.
type HealthHandler struct {
	baseHandler
}

// NewHealthHandler returns the server handler.
func NewHealthHandler(user *user.User) *HealthHandler {
	dlog.Server.Debug(user, "Creating new server health handler")
	h := HealthHandler{
		baseHandler: baseHandler{
			done:             internal.NewDone(),
			lines:            make(chan *line.Line, 100),
			serverMessages:   make(chan string, 10),
			maprMessages:     make(chan string, 10),
			ackCloseReceived: make(chan struct{}),
			user:             user,
			codec:            newProtocolCodec(user),
		},
	}
	h.handleCommandCb = h.handleHealthCommand

	fqdn, err := config.Hostname()
	if err != nil {
		dlog.Server.FatalPanic(err)
	}
	s := strings.Split(fqdn, ".")
	h.hostname = s[0]
	return &h
}

func (h *HealthHandler) handleHealthCommand(ctx context.Context,
	ltx lcontext.LContext, argc int, args []string, commandName string) {

	dlog.Server.Debug(h.user, "Handling health command", argc, args)
	switch commandName {
	case "health":
		h.send(h.serverMessages, "OK")
	case ".ack":
		h.handleAckCommand(argc, args)
	default:
		h.send(h.serverMessages, dlog.Server.Error(h.user,
			"Received unknown health command", commandName, argc, args))
	}
	h.shutdown()
}