-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglobal.go
More file actions
81 lines (71 loc) · 1.72 KB
/
Copy pathglobal.go
File metadata and controls
81 lines (71 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package app_core
import (
"errors"
"sync"
"github.com/sohaha/zlsgo/zdi"
"github.com/sohaha/zlsgo/zlog"
"github.com/zlsgo/app_core/service"
)
var (
// ErrNotInitialized is returned when global service is not initialized
ErrNotInitialized = errors.New("service not fully initialized")
initOnce sync.Once
initError error
)
// ensureInitialized checks if the global service is initialized
func ensureInitialized() error {
initOnce.Do(func() {
if service.Global == nil {
initError = ErrNotInitialized
}
})
return initError
}
func DI() (zdi.Invoker, error) {
if err := ensureInitialized(); err != nil {
return nil, err
}
return service.Global.DI, nil
}
// MustDI returns the DI invoker or panics if not initialized
// Deprecated: Use DI() instead and handle the error properly
func MustDI() zdi.Invoker {
di, err := DI()
if err != nil {
// Keep panic for backward compatibility
panic(err)
}
return di
}
func Conf() (*service.Conf, error) {
if err := ensureInitialized(); err != nil {
return nil, err
}
return service.Global.Conf, nil
}
// MustConf returns the configuration or panics if not initialized
// Deprecated: Use Conf() instead and handle the error properly
func MustConf() *service.Conf {
conf, err := Conf()
if err != nil {
// Keep panic for backward compatibility
panic(err)
}
return conf
}
func Log() (*zlog.Logger, error) {
if err := ensureInitialized(); err != nil {
return nil, err
}
return service.Global.Log, nil
}
// MustLog returns the logger or panics if not initialized
// Deprecated: Use Log() instead and handle the error properly
func MustLog() *zlog.Logger {
log, err := Log()
if err != nil {
// Keep panic for backward compatibility
panic(err)
}
return log
}