-
Notifications
You must be signed in to change notification settings - Fork 46
/
template_sdk.go
133 lines (106 loc) · 2.59 KB
/
template_sdk.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
package main
const (
templateSDK = `// {{.Header}} DO NOT EDIT.
package sdk
import (
"sync"
// @candi:serviceImport
)
// Option func type
type Option func(*sdkInstance)
var (
sdk SDK
once sync.Once
)
// SetGlobalSDK constructor with each sdk service option.
func SetGlobalSDK(opts ...Option) {
s := new(sdkInstance)
for _, o := range opts {
o(s)
}
once.Do(func() {
sdk = s
})
}
// GetSDK get global sdk instance
func GetSDK() SDK {
return sdk
}
// @candi:construct
// SDK instance abstraction
type SDK interface {
// @candi:serviceMethod
}
// sdkInstance implementation
type sdkInstance struct {
// @candi:serviceField
}
// @candi:instanceMethod
`
templateSDKServiceAbstraction = `package {{lower (clean $.ServiceName)}}
// {{upper (clean $.ServiceName)}} client abstract interface
type {{upper (clean $.ServiceName)}} interface {
// Add service client method
}
`
templateSDKServiceGRPC = `package {{lower (clean $.ServiceName)}}
import (
"net/url"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
)
type {{lower (clean $.ServiceName)}}GRPCImpl struct {
host string
authKey string
conn *grpc.ClientConn
}
// New{{upper (clean $.ServiceName)}}ServiceGRPC constructor
func New{{upper (clean $.ServiceName)}}ServiceGRPC(host string, authKey string) {{upper (clean $.ServiceName)}} {
if u, _ := url.Parse(host); u.Host != "" {
host = u.Host
}
conn, err := grpc.Dial(host, grpc.WithInsecure(), grpc.WithConnectParams(grpc.ConnectParams{
Backoff: backoff.Config{
BaseDelay: 50 * time.Millisecond,
Multiplier: 5,
MaxDelay: 50 * time.Millisecond,
},
MinConnectTimeout: 1 * time.Second,
}))
if err != nil {
panic(err)
}
return &{{lower (clean $.ServiceName)}}GRPCImpl{
host: host,
authKey: authKey,
conn: conn,
}
}
`
templateSDKServiceREST = `package {{lower (clean $.ServiceName)}}
import (
"net/http"
"time"
"{{.LibraryName}}/candiutils"
)
type {{lower (clean $.ServiceName)}}RESTImpl struct {
host string
authKey string
httpReq candiutils.HTTPRequest
}
// New{{upper (clean $.ServiceName)}}ServiceREST constructor
func New{{upper (clean $.ServiceName)}}ServiceREST(host string, authKey string) {{upper (clean $.ServiceName)}} {
return &{{lower (clean $.ServiceName)}}RESTImpl{
host: host,
authKey: authKey,
httpReq: candiutils.NewHTTPRequest(
candiutils.HTTPRequestSetRetries(5),
candiutils.HTTPRequestSetSleepBetweenRetry(500*time.Millisecond),
candiutils.HTTPRequestSetHTTPErrorCodeThreshold(http.StatusBadRequest),
candiutils.HTTPRequestSetBreakerName("{{lower (clean $.ServiceName)}}"),
),
}
}
`
)