Status checking

This commit is contained in:
LittleSheep 2025-03-01 13:22:38 +08:00
parent fe2e682267
commit 2a5b90b530
6 changed files with 54 additions and 14 deletions

View File

@ -1,8 +1,34 @@
package directory
import "github.com/rs/zerolog/log"
import (
"sync"
"github.com/rs/zerolog/log"
)
var statusOfServices = make(map[string]bool)
var statusLock sync.Mutex
func GetServiceStatus() map[string]bool {
out := make(map[string]bool)
for k, v := range statusOfServices {
out[k] = v
}
services := ListServiceInstance()
for _, service := range services {
if _, ok := out[service.Type]; !ok {
out[service.Type] = false
}
}
return out
}
func ValidateServices() {
statusLock.Lock()
defer statusLock.Unlock()
services := ListServiceInstance()
if len(services) == 0 {
return
@ -19,9 +45,12 @@ func ValidateServices() {
}
// Directly use the connect method to skip cache
if _, err := ConnectService(service); err != nil {
statusOfServices[service.Type] = false
_ = RemoveServiceInstance(service.ID)
log.Warn().Err(err).Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Unable connect to service, dropped...")
continue
} else {
statusOfServices[service.Type] = true
}
successCount++

View File

@ -13,19 +13,19 @@ import (
func MapControllers(app *fiber.App) {
app.Get("/check-ip", getClientIP)
app.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"api_level": pkg.ApiLevel,
"version": pkg.AppVersion,
"status": true,
})
})
app.Get("/directory/status", getServicesStatus)
app.Get("/directory/services", listExistsService)
// Some built-in public-accessible APIs
wellKnown := app.Group("/.well-known").Name("Well Known")
{
wellKnown.Get("/", func(c *fiber.Ctx) error {
return c.JSON(fiber.Map{
"api_level": pkg.ApiLevel,
"version": pkg.AppVersion,
"status": true,
})
})
wellKnown.Get("/directory/services", listExistsService)
wellKnown.Get("/openid-configuration", func(c *fiber.Ctx) error {
service := directory.GetServiceInstanceByType(nex.ServiceTypeAuth)
if service == nil || service.HttpAddr == nil {
@ -45,7 +45,7 @@ func MapControllers(app *fiber.App) {
// WatchTower administration APIs
wt := app.Group("/wt").Name("WatchTower").Use(auth.ValidatorMiddleware)
{
wt.Post("/maintenance/database", wtRunDbMaintenance)
wt.Post("/maintenance/database", watchRunDbMaintenance)
}
// Common websocket gateway

View File

@ -0,0 +1,10 @@
package api
import (
"git.solsynth.dev/hypernet/nexus/pkg/internal/directory"
"github.com/gofiber/fiber/v2"
)
func getServicesStatus(c *fiber.Ctx) error {
return c.JSON(directory.GetServiceStatus())
}

View File

@ -6,7 +6,7 @@ import (
"github.com/gofiber/fiber/v2"
)
func wtRunDbMaintenance(c *fiber.Ctx) error {
func watchRunDbMaintenance(c *fiber.Ctx) error {
if err := sec.EnsureGrantedPerm(c, "AdminOperateWatchTower", true); err != nil {
return err
}

View File

@ -23,8 +23,8 @@ func NewServer() *WebApp {
app := fiber.New(fiber.Config{
DisableStartupMessage: true,
EnableIPValidation: true,
ServerHeader: "Hypernet.Nexus",
AppName: "Hypernet.Nexus",
ServerHeader: "HyperNet.Nexus",
AppName: "HyperNet.Nexus",
ProxyHeader: fiber.HeaderXForwardedFor,
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,

View File

@ -117,6 +117,7 @@ func main() {
// Configure timed tasks
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
quartz.AddFunc("@midnight", watchtower.RunDbMaintenance)
quartz.AddFunc("@every 5m", directory.ValidateServices)
quartz.Start()
// Messages