2025-09-20 07:35:50 +00:00
|
|
|
// Package global provides global variables and interfaces for accessing web and subscription servers.
|
2023-02-09 19:18:06 +00:00
|
|
|
package global
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2026-05-18 20:57:31 +00:00
|
|
|
"sync"
|
2023-02-09 19:18:06 +00:00
|
|
|
_ "unsafe"
|
2023-04-27 20:45:06 +00:00
|
|
|
|
|
|
|
|
"github.com/robfig/cron/v3"
|
2023-02-09 19:18:06 +00:00
|
|
|
)
|
|
|
|
|
|
2024-03-10 21:31:24 +00:00
|
|
|
var (
|
|
|
|
|
webServer WebServer
|
|
|
|
|
subServer SubServer
|
2026-05-18 20:57:31 +00:00
|
|
|
|
|
|
|
|
restartHookMu sync.RWMutex
|
|
|
|
|
restartHook func()
|
2024-03-10 21:31:24 +00:00
|
|
|
)
|
2023-02-09 19:18:06 +00:00
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// WebServer interface defines methods for accessing the web server instance.
|
2023-02-09 19:18:06 +00:00
|
|
|
type WebServer interface {
|
2025-09-20 07:35:50 +00:00
|
|
|
GetCron() *cron.Cron // Get the cron scheduler
|
|
|
|
|
GetCtx() context.Context // Get the server context
|
2026-01-05 04:54:56 +00:00
|
|
|
GetWSHub() any // Get the WebSocket hub (using any to avoid circular dependency)
|
2023-02-09 19:18:06 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// SubServer interface defines methods for accessing the subscription server instance.
|
2023-05-22 14:36:34 +00:00
|
|
|
type SubServer interface {
|
2025-09-20 07:35:50 +00:00
|
|
|
GetCtx() context.Context // Get the server context
|
2023-05-22 14:36:34 +00:00
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// SetWebServer sets the global web server instance.
|
2023-02-09 19:18:06 +00:00
|
|
|
func SetWebServer(s WebServer) {
|
|
|
|
|
webServer = s
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetWebServer returns the global web server instance.
|
2023-02-09 19:18:06 +00:00
|
|
|
func GetWebServer() WebServer {
|
|
|
|
|
return webServer
|
|
|
|
|
}
|
2023-05-22 14:36:34 +00:00
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// SetSubServer sets the global subscription server instance.
|
2023-05-22 14:36:34 +00:00
|
|
|
func SetSubServer(s SubServer) {
|
|
|
|
|
subServer = s
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-20 07:35:50 +00:00
|
|
|
// GetSubServer returns the global subscription server instance.
|
2023-05-22 14:36:34 +00:00
|
|
|
func GetSubServer() SubServer {
|
|
|
|
|
return subServer
|
|
|
|
|
}
|
2026-05-18 20:57:31 +00:00
|
|
|
|
|
|
|
|
// SetRestartHook registers a callback that triggers an in-process panel
|
|
|
|
|
// restart. main.go sets this up to push SIGHUP into its own signal channel
|
|
|
|
|
// so the restart path works on Windows (where p.Signal(SIGHUP) is unsupported).
|
|
|
|
|
func SetRestartHook(fn func()) {
|
|
|
|
|
restartHookMu.Lock()
|
|
|
|
|
defer restartHookMu.Unlock()
|
|
|
|
|
restartHook = fn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TriggerRestart fires the registered restart hook. Returns false if none is set.
|
|
|
|
|
func TriggerRestart() bool {
|
|
|
|
|
restartHookMu.RLock()
|
|
|
|
|
fn := restartHook
|
|
|
|
|
restartHookMu.RUnlock()
|
|
|
|
|
if fn == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
fn()
|
|
|
|
|
return true
|
|
|
|
|
}
|