forked from go-kratos/kratos-layout
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mock.go
167 lines (144 loc) · 3.63 KB
/
mock.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package mock
import (
"context"
"fmt"
"net/http"
"runtime/debug"
"sync"
"github.com/go-cinch/common/mock"
"github.com/go-cinch/layout/internal/biz"
"github.com/go-cinch/layout/internal/conf"
"github.com/go-cinch/layout/internal/data"
"github.com/go-cinch/layout/internal/service"
"github.com/go-kratos/kratos/v2/transport"
)
func GameService() (gameService *service.GameService) {
gameUseCase := GameUseCase()
gameService = service.NewGameService(nil, gameUseCase)
return
}
func GameUseCase() (gameUseCase *biz.GameUseCase) {
c, dataData, cache := Data()
gameRepo := GameRepo()
transaction := data.NewTransaction(dataData)
gameUseCase = biz.NewGameUseCase(c, gameRepo, transaction, cache)
return
}
func GameRepo() biz.GameRepo {
_, d, _ := Data()
return data.NewGameRepo(d)
}
type headerCarrier http.Header
func (hc headerCarrier) Get(key string) string { return http.Header(hc).Get(key) }
func (hc headerCarrier) Set(key string, value string) { http.Header(hc).Set(key, value) }
func (hc headerCarrier) Add(key string, value string) { http.Header(hc).Add(key, value) }
// Keys lists the keys stored in this carrier.
func (hc headerCarrier) Keys() []string {
keys := make([]string, 0, len(hc))
for k := range http.Header(hc) {
keys = append(keys, k)
}
return keys
}
// Values returns a slice value associated with the passed key.
func (hc headerCarrier) Values(key string) []string {
return http.Header(hc).Values(key)
}
func newUserHeader(k, v string) *headerCarrier {
header := &headerCarrier{}
header.Set(k, v)
return header
}
type Transport struct {
kind transport.Kind
endpoint string
operation string
reqHeader transport.Header
}
func (tr *Transport) Kind() transport.Kind {
return tr.kind
}
func (tr *Transport) Endpoint() string {
return tr.endpoint
}
func (tr *Transport) Operation() string {
return tr.operation
}
func (tr *Transport) RequestHeader() transport.Header {
return tr.reqHeader
}
func (*Transport) ReplyHeader() transport.Header {
return nil
}
func NewContextWithUserId(ctx context.Context, u string) context.Context {
tr := &Transport{
reqHeader: newUserHeader("X-Md-Global-Code", u),
}
return transport.NewServerContext(ctx, tr)
}
var (
onceC *conf.Bootstrap
once sync.Once
)
func Data() (c *conf.Bootstrap, dataData *data.Data, cache biz.Cache) {
debug.SetGCPercent(-1)
once.Do(func() {
onceC = MySQLAndRedis()
})
c = onceC
// os.Setenv("COPIERX_UTC", "true")
universalClient, err := data.NewRedis(c)
if err != nil {
panic(err)
}
tenant, err := data.NewDB(c)
if err != nil {
panic(err)
}
sonyflake, err := data.NewSonyflake(c)
if err != nil {
panic(err)
}
tracerProvider, err := data.NewTracer(c)
if err != nil {
panic(err)
}
dataData, _ = data.NewData(universalClient, tenant, sonyflake, tracerProvider, nil)
cache = data.NewCache(c, universalClient)
return c, dataData, cache
}
func MySQLAndRedis() *conf.Bootstrap {
host1, port1, err := mock.NewMySQL()
if err != nil {
panic(err)
}
host2, port2, err := mock.NewRedis()
if err != nil {
panic(err)
}
// host1 = "127.0.0.1"
// port1 = 3306
return &conf.Bootstrap{
Server: &conf.Server{
MachineId: "123",
LogLevel: "debug",
},
Data: &conf.Data{
Database: &conf.Data_Database{
Endpoint: fmt.Sprintf("%s:%d", host1, port1),
Username: "root",
Password: "passwd",
Schema: "game",
Query: "charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=True&loc=Local&timeout=10000ms",
},
Redis: &conf.Data_Redis{
Dsn: fmt.Sprintf("redis://%s:%d", host2, port2),
},
},
Tracer: &conf.Tracer{
Enable: true,
Otlp: &conf.Tracer_Otlp{},
Stdout: &conf.Tracer_Stdout{},
},
}
}