-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathinfra.go
122 lines (108 loc) · 3.36 KB
/
infra.go
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package internal
import (
"errors"
"fmt"
"reflect"
"github.com/8treenet/freedom/infra/requests"
"github.com/go-redis/redis"
)
// Infra .
// The parent class of the infrastructure, which can be inherited using the parent class's methods.
type Infra struct {
worker Worker `json:"-"`
single bool
}
// BeginRequest Polymorphic method, subclasses can override overrides overrides.
// The request is triggered after entry.
func (infra *Infra) BeginRequest(rt Worker) {
if infra.single {
return
}
infra.worker = rt
}
// FetchOnlyDB Gets the installed database handle.
func (infra *Infra) FetchOnlyDB(db interface{}) error {
resultDB := globalApp.Database.db
if resultDB == nil {
return errors.New("DB not found, please install")
}
if !fetchValue(db, resultDB) {
return errors.New("DB not found, please install")
}
return nil
}
// Redis Gets the installed redis client.
func (infra *Infra) Redis() redis.Cmdable {
return globalApp.Cache.client
}
// FetchCustom gets the installed custom data sources.
func (infra *Infra) FetchCustom(obj interface{}) {
globalApp.other.get(obj)
return
}
// NewHTTPRequest transferBus : Whether to pass the context, turned on by default. Typically used for tracking internal services.
func (infra *Infra) NewHTTPRequest(url string, transferBus ...bool) requests.Request {
req := requests.NewHTTPRequest(url)
if len(transferBus) > 0 && !transferBus[0] {
return req
}
if infra.worker == nil {
//The singleton object does not have a Worker component
return req
}
req.SetHeader(infra.worker.Bus().Header)
return req
}
// NewH2CRequest transferBus : Whether to pass the context, turned on by default. Typically used for tracking internal services.
func (infra *Infra) NewH2CRequest(url string, transferBus ...bool) requests.Request {
req := requests.NewH2CRequest(url)
if len(transferBus) > 0 && !transferBus[0] {
return req
}
if infra.worker == nil {
//The singleton object does not have a Worker component
return req
}
req.SetHeader(infra.worker.Bus().Header)
return req
}
// InjectBaseEntity The base class object that is injected into the entity.
func (infra *Infra) InjectBaseEntity(entity Entity) {
if infra.worker == nil {
panic("The singleton object does not have a Worker component")
}
injectBaseEntity(infra.worker, entity)
return
}
// InjectBaseEntitys The base class object that is injected into the entity.
func (infra *Infra) InjectBaseEntitys(entitys interface{}) {
if infra.worker == nil {
panic("The singleton object does not have a Worker component")
}
entitysValue := reflect.ValueOf(entitys)
if entitysValue.Kind() != reflect.Slice {
panic(fmt.Sprintf("[Freedom] InjectBaseEntitys: It's not a slice, %v", entitysValue.Type()))
}
for i := 0; i < entitysValue.Len(); i++ {
iface := entitysValue.Index(i).Interface()
if _, ok := iface.(Entity); !ok {
panic(fmt.Sprintf("[Freedom] InjectBaseEntitys: This is not an entity, %v", entitysValue.Type()))
}
injectBaseEntity(infra.worker, iface)
}
return
}
// Worker Returns the requested Worer.
func (infra *Infra) Worker() Worker {
if infra.worker == nil {
panic("The singleton object does not have a Worker component")
}
return infra.worker
}
// FetchSingleInfra Gets the single-case component.
func (infra *Infra) FetchSingleInfra(com interface{}) bool {
return globalApp.FetchSingleInfra(com)
}
func (infra *Infra) setSingle() {
infra.single = true
}