mirror of
https://github.com/gl-inet/glkvm-cloud.git
synced 2026-10-21 19:25:39 +00:00
7931fa0efd
Refactor legacy login endpoint and keep compatibility with existing authentication logic. Signed-off-by: GL.iNet-Yongping.Xie <yongping.xie@gl-inet.com>
52 lines
1.4 KiB
Go
Executable File
52 lines
1.4 KiB
Go
Executable File
package xconfig
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var (
|
|
globalOnce sync.Once
|
|
globalCfg atomic.Value // stores *Config
|
|
globalErr atomic.Value // stores error
|
|
)
|
|
|
|
// InitGlobal initializes the global config singleton.
|
|
// Call this once at program startup after parsing CLI flags.
|
|
func InitGlobal(c *Config) {
|
|
if c == nil {
|
|
panic("xconfig: InitGlobal with nil config")
|
|
}
|
|
globalOnce.Do(func() {
|
|
globalCfg.Store(c)
|
|
})
|
|
}
|
|
|
|
// InitGlobalFromCLI parses config from CLI command then stores as global.
|
|
// Helpful if you want a single call in main().
|
|
// Note: requires you already built the CLI command and flags.
|
|
func InitGlobalFromCLI(cmd any) (*Config, error) {
|
|
// We avoid importing cli here to keep xconfig decoupled.
|
|
// If you prefer, you can remove this helper and call cfg.Parse(c) in main() yourself.
|
|
return nil, fmt.Errorf("xconfig: InitGlobalFromCLI not implemented; call cfg.Parse(cliCmd) then InitGlobal(cfg)")
|
|
}
|
|
|
|
// Get returns the global config if initialized, otherwise nil.
|
|
func Get() *Config {
|
|
v := globalCfg.Load()
|
|
if v == nil {
|
|
return nil
|
|
}
|
|
return v.(*Config)
|
|
}
|
|
|
|
// Must returns the global config or panics if not initialized.
|
|
func Must() *Config {
|
|
cfg := Get()
|
|
if cfg == nil {
|
|
panic("xconfig: global config not initialized; call xconfig.InitGlobal(cfg) at startup")
|
|
}
|
|
return cfg
|
|
}
|