From 2a5b90b530d6fa6104ceebdecd67a2b5ab00fe2f Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 1 Mar 2025 13:22:38 +0800 Subject: [PATCH] :sparkles: Status checking --- pkg/internal/directory/validator.go | 31 ++++++++++++++++++++++++++++- pkg/internal/web/api/index.go | 20 +++++++++---------- pkg/internal/web/api/status.go | 10 ++++++++++ pkg/internal/web/api/watchtower.go | 2 +- pkg/internal/web/server.go | 4 ++-- pkg/main.go | 1 + 6 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 pkg/internal/web/api/status.go diff --git a/pkg/internal/directory/validator.go b/pkg/internal/directory/validator.go index 663a9ea..2710cb4 100644 --- a/pkg/internal/directory/validator.go +++ b/pkg/internal/directory/validator.go @@ -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++ diff --git a/pkg/internal/web/api/index.go b/pkg/internal/web/api/index.go index 40bd0cc..f045014 100644 --- a/pkg/internal/web/api/index.go +++ b/pkg/internal/web/api/index.go @@ -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 diff --git a/pkg/internal/web/api/status.go b/pkg/internal/web/api/status.go new file mode 100644 index 0000000..1e4ea3f --- /dev/null +++ b/pkg/internal/web/api/status.go @@ -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()) +} diff --git a/pkg/internal/web/api/watchtower.go b/pkg/internal/web/api/watchtower.go index d4b6150..685ce77 100644 --- a/pkg/internal/web/api/watchtower.go +++ b/pkg/internal/web/api/watchtower.go @@ -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 } diff --git a/pkg/internal/web/server.go b/pkg/internal/web/server.go index d8286f0..9719f0e 100644 --- a/pkg/internal/web/server.go +++ b/pkg/internal/web/server.go @@ -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, diff --git a/pkg/main.go b/pkg/main.go index 857a9dc..aae8569 100644 --- a/pkg/main.go +++ b/pkg/main.go @@ -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