✨ Status checking
This commit is contained in:
parent
fe2e682267
commit
2a5b90b530
@ -1,8 +1,34 @@
|
|||||||
package directory
|
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() {
|
func ValidateServices() {
|
||||||
|
statusLock.Lock()
|
||||||
|
defer statusLock.Unlock()
|
||||||
|
|
||||||
services := ListServiceInstance()
|
services := ListServiceInstance()
|
||||||
if len(services) == 0 {
|
if len(services) == 0 {
|
||||||
return
|
return
|
||||||
@ -19,9 +45,12 @@ func ValidateServices() {
|
|||||||
}
|
}
|
||||||
// Directly use the connect method to skip cache
|
// Directly use the connect method to skip cache
|
||||||
if _, err := ConnectService(service); err != nil {
|
if _, err := ConnectService(service); err != nil {
|
||||||
|
statusOfServices[service.Type] = false
|
||||||
_ = RemoveServiceInstance(service.ID)
|
_ = RemoveServiceInstance(service.ID)
|
||||||
log.Warn().Err(err).Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Unable connect to service, dropped...")
|
log.Warn().Err(err).Str("id", service.ID).Str("addr", service.GrpcAddr).Msg("Unable connect to service, dropped...")
|
||||||
continue
|
continue
|
||||||
|
} else {
|
||||||
|
statusOfServices[service.Type] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
successCount++
|
successCount++
|
||||||
|
@ -13,19 +13,19 @@ import (
|
|||||||
|
|
||||||
func MapControllers(app *fiber.App) {
|
func MapControllers(app *fiber.App) {
|
||||||
app.Get("/check-ip", getClientIP)
|
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
|
// Some built-in public-accessible APIs
|
||||||
wellKnown := app.Group("/.well-known").Name("Well Known")
|
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 {
|
wellKnown.Get("/openid-configuration", func(c *fiber.Ctx) error {
|
||||||
service := directory.GetServiceInstanceByType(nex.ServiceTypeAuth)
|
service := directory.GetServiceInstanceByType(nex.ServiceTypeAuth)
|
||||||
if service == nil || service.HttpAddr == nil {
|
if service == nil || service.HttpAddr == nil {
|
||||||
@ -45,7 +45,7 @@ func MapControllers(app *fiber.App) {
|
|||||||
// WatchTower administration APIs
|
// WatchTower administration APIs
|
||||||
wt := app.Group("/wt").Name("WatchTower").Use(auth.ValidatorMiddleware)
|
wt := app.Group("/wt").Name("WatchTower").Use(auth.ValidatorMiddleware)
|
||||||
{
|
{
|
||||||
wt.Post("/maintenance/database", wtRunDbMaintenance)
|
wt.Post("/maintenance/database", watchRunDbMaintenance)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common websocket gateway
|
// Common websocket gateway
|
||||||
|
10
pkg/internal/web/api/status.go
Normal file
10
pkg/internal/web/api/status.go
Normal 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())
|
||||||
|
}
|
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/gofiber/fiber/v2"
|
"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 {
|
if err := sec.EnsureGrantedPerm(c, "AdminOperateWatchTower", true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -23,8 +23,8 @@ func NewServer() *WebApp {
|
|||||||
app := fiber.New(fiber.Config{
|
app := fiber.New(fiber.Config{
|
||||||
DisableStartupMessage: true,
|
DisableStartupMessage: true,
|
||||||
EnableIPValidation: true,
|
EnableIPValidation: true,
|
||||||
ServerHeader: "Hypernet.Nexus",
|
ServerHeader: "HyperNet.Nexus",
|
||||||
AppName: "Hypernet.Nexus",
|
AppName: "HyperNet.Nexus",
|
||||||
ProxyHeader: fiber.HeaderXForwardedFor,
|
ProxyHeader: fiber.HeaderXForwardedFor,
|
||||||
JSONEncoder: json.Marshal,
|
JSONEncoder: json.Marshal,
|
||||||
JSONDecoder: json.Unmarshal,
|
JSONDecoder: json.Unmarshal,
|
||||||
|
@ -117,6 +117,7 @@ func main() {
|
|||||||
// Configure timed tasks
|
// Configure timed tasks
|
||||||
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
||||||
quartz.AddFunc("@midnight", watchtower.RunDbMaintenance)
|
quartz.AddFunc("@midnight", watchtower.RunDbMaintenance)
|
||||||
|
quartz.AddFunc("@every 5m", directory.ValidateServices)
|
||||||
quartz.Start()
|
quartz.Start()
|
||||||
|
|
||||||
// Messages
|
// Messages
|
||||||
|
Loading…
x
Reference in New Issue
Block a user