/
ha.go
50 lines (44 loc) · 1.33 KB
/
ha.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
// Copyright (c) 2019 IoTeX Foundation
// This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability
// or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed.
// This source code is governed by Apache License 2.0 that can be found in the LICENSE file.
package ha
import (
"encoding/json"
"net/http"
"strings"
"github.com/iotexproject/iotex-core/consensus"
"github.com/iotexproject/iotex-core/pkg/log"
)
// Controller controls the node high availability status
type Controller struct {
c consensus.Consensus
}
// New constructs a HA controller instance
func New(c consensus.Consensus) *Controller {
return &Controller{
c: c,
}
}
// Handle handles admin request
func (ha *Controller) Handle(w http.ResponseWriter, r *http.Request) {
val := strings.ToLower(r.URL.Query().Get("activate"))
switch val {
case "true":
log.S().Info("Set the node to active mode")
ha.c.Activate(true)
case "false":
log.S().Info("Set the node to stand-by mode")
ha.c.Activate(false)
case "":
type payload struct {
Active bool `json:"active"`
}
enc := json.NewEncoder(w)
if err := enc.Encode(&payload{Active: ha.c.Active()}); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
w.WriteHeader(http.StatusOK)
}