github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/ha/ha.go (about) 1 // Copyright (c) 2019 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package ha 7 8 import ( 9 "encoding/json" 10 "net/http" 11 "strings" 12 13 "github.com/iotexproject/iotex-core/consensus" 14 "github.com/iotexproject/iotex-core/pkg/log" 15 ) 16 17 // Controller controls the node high availability status 18 type Controller struct { 19 c consensus.Consensus 20 } 21 22 // New constructs a HA controller instance 23 func New(c consensus.Consensus) *Controller { 24 return &Controller{ 25 c: c, 26 } 27 } 28 29 // Handle handles admin request 30 func (ha *Controller) Handle(w http.ResponseWriter, r *http.Request) { 31 val := strings.ToLower(r.URL.Query().Get("activate")) 32 switch val { 33 case "true": 34 log.S().Info("Set the node to active mode") 35 ha.c.Activate(true) 36 case "false": 37 log.S().Info("Set the node to stand-by mode") 38 ha.c.Activate(false) 39 case "": 40 type payload struct { 41 Active bool `json:"active"` 42 } 43 enc := json.NewEncoder(w) 44 if err := enc.Encode(&payload{Active: ha.c.Active()}); err != nil { 45 w.WriteHeader(http.StatusInternalServerError) 46 return 47 } 48 } 49 w.WriteHeader(http.StatusOK) 50 }