oc-peer/controllers/status.go

60 lines
1.3 KiB
Go
Raw Normal View History

2024-08-21 16:25:24 +02:00
package controllers
import (
"encoding/json"
2024-08-26 12:07:54 +02:00
"fmt"
2024-08-21 16:25:24 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web"
)
type State int
const (
ALIVE State = iota
REDUCED_SERVICE
DB_FALLOUT
DEAD
)
func (s State) String() string {
return [...]string{"alive", "reduced service", "database fallout", "dead"}[s]
}
// Operations about workflow
type StatusController struct {
beego.Controller
}
// @Title Status
// @Description get peer status if it's alive
// @Param body body list of addresses true "The addresses list"
// @Success 200 {status} models.status
// @router / [post]
func (o *StatusController) Status() {
2024-08-26 12:07:54 +02:00
var address map[string]string
2024-08-21 16:25:24 +02:00
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &address)
2024-08-26 12:07:54 +02:00
fmt.Println(len(address))
2024-08-21 16:25:24 +02:00
if len(address) == 0 { // default if nothing is send in body
2024-08-26 12:07:54 +02:00
for _, host := range []string{"oc-datacenter", "oc-workflow", "oc-workspace", "oc-shared"} {
address[host] = "http://" + host + ":8080/oc"
2024-08-21 16:25:24 +02:00
}
}
api := tools.API{}
2024-08-26 12:07:54 +02:00
state, services, err := api.CheckRemoteAPIs(address)
errSTR := ""
if err != nil {
errSTR = err.Error()
}
2024-08-21 16:25:24 +02:00
o.Data["json"] = map[string]interface{}{
2024-08-26 12:07:54 +02:00
"data": map[string]interface{}{
"state": state.String(),
"code": state.EnumIndex(),
"services": services,
2024-08-21 16:25:24 +02:00
},
2024-08-26 12:07:54 +02:00
"code": 200,
"error": errSTR,
2024-08-21 16:25:24 +02:00
}
o.ServeJSON()
}