oc-lib/tools/api.go

48 lines
925 B
Go
Raw Normal View History

2024-08-21 10:58:24 +02:00
package tools
import (
2024-08-21 11:23:07 +02:00
"errors"
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
)
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{}
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
}