-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainer.go
More file actions
100 lines (83 loc) · 2.9 KB
/
Copy pathcontainer.go
File metadata and controls
100 lines (83 loc) · 2.9 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package ectoinject
import (
"github.com/Gobusters/ectoinject/ectocontainer"
"github.com/Gobusters/ectoinject/internal/container"
"github.com/Gobusters/ectoinject/internal/logging"
"github.com/Gobusters/ectoinject/internal/store"
"github.com/Gobusters/ectoinject/loglevel"
)
var DefaulLoggerConfig = ectocontainer.DIContainerLoggerConfig{
Prefix: "ectoinject",
LogLevel: loglevel.INFO,
EnableColor: true,
Enabled: true,
LogFunc: nil,
}
var DefaultContainerConfig = ectocontainer.DIContainerConfig{
ID: store.GetDefaultContainerID(),
AllowCaptiveDependencies: true,
AllowMissingDependencies: true,
RequireInjectTag: false,
AllowUnsafeDependencies: false,
ConstructorFuncName: "Constructor",
InjectTagName: "inject",
LoggerConfig: &DefaulLoggerConfig,
}
// NewDIDefaultContainer creates a new container with the default configuration. The default configuration is:
// ID: "default"
// AllowCaptiveDependencies: true
// AllowMissingDependencies: true
// RequireInjectTag: false
// AllowUnsafeDependencies: false
// ConstructorFuncName: "Constructor"
// InjectTagName: "inject"
func NewDIDefaultContainer() (ectocontainer.DIContainer, error) {
return NewDIContainer(DefaultContainerConfig)
}
// NewDIContainer creates a new container with the provided configuration
// config: The configuration to use for the container
func NewDIContainer(config ectocontainer.DIContainerConfig) (ectocontainer.DIContainer, error) {
if config.ID == "" {
config.ID = store.GetDefaultContainerID()
}
if config.LoggerConfig == nil {
config.LoggerConfig = &DefaulLoggerConfig
}
if config.InjectTagName == "" {
config.InjectTagName = "inject"
}
if config.ConstructorFuncName == "" {
config.ConstructorFuncName = "Constructor"
}
loggerConfig := config.LoggerConfig
logger, err := logging.NewLogger(loggerConfig.Prefix, loggerConfig.LogLevel, loggerConfig.EnableColor, loggerConfig.Enabled, loggerConfig.LogFunc)
if err != nil {
return nil, err
}
ectoContainer := container.NewEctoContainer(config, logger)
err = RegisterInstance[ectocontainer.DIContainer](ectoContainer, &ectoContainer)
if err != nil {
return nil, err
}
err = store.RegisterContainer(ectoContainer)
if err != nil {
return nil, err
}
return ectoContainer, nil
}
// RegisterContainer adds the container to the container lookup
func RegisterContainer(container ectocontainer.DIContainer) error {
return store.RegisterContainer(container)
}
// SetDefaultContainer sets the default container to use
func SetDefaultContainer(containerID string) error {
return store.SetDefaultContainer(containerID)
}
// GetDefaultContainer gets the default container
func GetDefaultContainer() ectocontainer.DIContainer {
return store.GetDefaultContainer()
}
// GetContainer gets the container with the provided id
func GetContainer(id string) ectocontainer.DIContainer {
return store.GetContainer(id)
}