oc-lib/tools/api.go

127 lines
2.8 KiB
Go
Raw Normal View History

2024-08-21 10:58:24 +02:00
package tools
import (
2024-08-21 15:32:10 +02:00
"encoding/json"
2024-08-21 11:23:07 +02:00
"errors"
2024-08-23 17:35:29 +02:00
"fmt"
2024-08-21 11:23:07 +02:00
2024-08-21 10:58:24 +02:00
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
)
var UncatchedError = []error{}
type State int
const (
ALIVE State = iota
REDUCED_SERVICE
UNPROCESSABLE_ENTITY
DB_FALLOUT
TEAPOT
DEAD
2024-08-22 15:04:01 +02:00
WAITING
2024-08-21 10:58:24 +02:00
)
2024-08-23 09:01:28 +02:00
func (s State) EnumIndex() int {
return int(s)
}
2024-08-21 15:46:16 +02:00
func ToState(str string) State {
2024-08-22 15:04:01 +02:00
for _, s := range []State{ALIVE, REDUCED_SERVICE, UNPROCESSABLE_ENTITY, DB_FALLOUT, TEAPOT, DEAD, WAITING} {
2024-08-21 15:46:16 +02:00
if s.String() == str {
return s
}
}
return DEAD
}
2024-08-21 10:58:24 +02:00
func (s State) String() string {
return [...]string{"alive", "reduced service", "unprocessable entity", "database fallout",
2024-08-22 15:04:01 +02:00
"some things boils in here, i'm probably a teapot", "dead", "waiting"}[s]
2024-08-21 10:58:24 +02:00
}
2024-08-22 13:11:21 +02:00
type API struct{}
2024-08-21 10:58:24 +02:00
2024-08-21 11:23:07 +02:00
func (a *API) GetState() (State, int, error) {
2024-08-21 10:58:24 +02:00
// Check if the database is up
err := mongo.MONGOService.TestDB(GetConfig())
if err != nil {
2024-08-21 11:23:07 +02:00
return DB_FALLOUT, 200, err
2024-08-21 10:58:24 +02:00
}
err = mongo.MONGOService.TestCollections(GetConfig(), []string{})
if err != nil {
2024-08-21 11:23:07 +02:00
return UNPROCESSABLE_ENTITY, 200, err
2024-08-21 10:58:24 +02:00
}
if len(UncatchedError) > 0 {
2024-08-21 11:23:07 +02:00
errStr := ""
for _, e := range UncatchedError {
errStr += e.Error() + "\n"
}
return TEAPOT, 200, errors.New(errStr)
2024-08-21 10:58:24 +02:00
}
2024-08-21 11:23:07 +02:00
return ALIVE, 200, nil
2024-08-21 10:58:24 +02:00
}
2024-08-21 11:28:13 +02:00
2024-08-23 09:01:28 +02:00
func (a *API) CheckRemotePeer(url string) (State, map[string]int) {
2024-08-21 15:46:16 +02:00
// Check if the database is up
caller := NewHTTPCaller(map[string]map[METHOD]string{})
var resp APIStatusResponse
2024-08-23 22:17:03 +02:00
b, err := caller.CallPost(url, "/status", map[string]interface{}{})
2024-08-23 18:18:29 +02:00
fmt.Println("CheckRemotePeer", b, url, err)
2024-08-21 15:46:16 +02:00
if err != nil {
2024-08-23 09:01:28 +02:00
return DEAD, map[string]int{}
2024-08-21 15:46:16 +02:00
}
json.Unmarshal(b, &resp)
2024-08-23 17:35:29 +02:00
fmt.Println("CheckRemotePeer2", b, err)
2024-08-22 13:19:08 +02:00
if resp.Data == nil {
2024-08-23 09:01:28 +02:00
return DEAD, map[string]int{}
}
new := map[string]int{}
2024-08-23 17:35:29 +02:00
fmt.Println("CheckRemotePeer", resp.Data.Services)
2024-08-23 09:01:28 +02:00
for k, v := range resp.Data.Services {
new[k] = ToState(v).EnumIndex()
2024-08-22 13:19:08 +02:00
}
2024-08-23 09:01:28 +02:00
return ToState(resp.Data.State), new
2024-08-21 15:46:16 +02:00
}
2024-08-23 11:13:01 +02:00
func (a *API) CheckRemoteAPIs(urls map[string]string) (State, map[string]string, error) {
2024-08-21 11:28:13 +02:00
// Check if the database is up
2024-08-23 11:13:01 +02:00
new := map[string]string{}
2024-08-21 11:30:44 +02:00
caller := NewHTTPCaller(map[string]map[METHOD]string{})
2024-08-23 15:43:12 +02:00
code := 0
u := ""
e := ""
2024-08-23 09:03:51 +02:00
for appName, url := range urls {
2024-08-21 15:32:10 +02:00
var resp APIStatusResponse
b, err := caller.CallGet(url, "/version/status")
2024-08-21 11:28:13 +02:00
if err != nil {
2024-08-23 09:01:28 +02:00
return REDUCED_SERVICE, new, err
2024-08-21 11:28:13 +02:00
}
2024-08-21 15:32:10 +02:00
json.Unmarshal(b, &resp)
2024-08-22 10:19:14 +02:00
if resp.Data == nil {
2024-08-23 09:01:28 +02:00
return DEAD, new, errors.New(url + " -> is DEAD")
}
2024-08-23 11:13:01 +02:00
new[appName] = resp.Data.State
2024-08-23 15:43:12 +02:00
if resp.Data.Code > code {
code = resp.Data.Code
u = url
e += resp.Error
2024-08-21 15:32:10 +02:00
}
2024-08-21 11:28:13 +02:00
}
2024-08-23 15:43:12 +02:00
if code > 0 {
return REDUCED_SERVICE, new, errors.New(u + " -> " + e)
}
2024-08-23 09:01:28 +02:00
return ALIVE, new, nil
2024-08-21 11:28:13 +02:00
}
2024-08-21 15:32:10 +02:00
type APIStatusResponse struct {
Data *APIStatus `json:"data"`
Error string `json:"error"`
}
type APIStatus struct {
2024-08-23 09:01:28 +02:00
Code int `json:"code"`
State string `json:"state"`
Services map[string]string `json:"services"`
2024-08-21 15:32:10 +02:00
}