oc-peer/controllers/status.go

58 lines
1.3 KiB
Go
Raw Normal View History

2024-08-21 16:25:24 +02:00
package controllers
import (
"encoding/json"
"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)
if len(address) == 0 { // default if nothing is send in body
2024-08-30 10:44:47 +02:00
for _, host := range []string{"oc-datacenter", "oc-workflow", "oc-workspace", "oc-shared", "oc-workspace"} {
2024-08-26 12:07:54 +02:00
address[host] = "http://" + host + ":8080/oc"
2024-08-21 16:25:24 +02:00
}
}
api := tools.API{}
2024-08-30 10:44:47 +02:00
state, services, err := api.CheckRemoteAPIs(address) // check if the services are alive
2024-08-26 12:07:54 +02:00
errSTR := ""
if err != nil {
errSTR = err.Error()
}
2024-08-30 10:44:47 +02:00
o.Data["json"] = map[string]interface{}{ // return the status
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()
}