oc-lib/tools/api.go
2024-08-21 11:28:13 +02:00

63 lines
1.3 KiB
Go

package tools
import (
"errors"
"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
)
func (s State) String() string {
return [...]string{"alive", "reduced service", "unprocessable entity", "database fallout",
"some things boils in here, i'm probably a teapot", "dead"}[s]
}
type API struct{}
func (a *API) GetState() (State, int, error) {
// Check if the database is up
err := mongo.MONGOService.TestDB(GetConfig())
if err != nil {
return DB_FALLOUT, 200, err
}
err = mongo.MONGOService.TestCollections(GetConfig(), []string{})
if err != nil {
return UNPROCESSABLE_ENTITY, 200, err
}
if len(UncatchedError) > 0 {
errStr := ""
for _, e := range UncatchedError {
errStr += e.Error() + "\n"
}
return TEAPOT, 200, errors.New(errStr)
}
return ALIVE, 200, nil
}
func (a *API) CheckRemoteAPIs(urls map[string]map[METHOD]string) (State, int, error) {
// Check if the database is up
caller := NewHTTPCaller(urls)
for _, methods := range urls {
if k, ok := methods[GET]; !ok {
return REDUCED_SERVICE, 200, errors.New("no get method found for " + k + " API")
}
_, err := caller.CallGet(methods[GET], "")
if err != nil {
return REDUCED_SERVICE, 200, err
}
}
return ALIVE, 200, nil
}