forked from tokopedia/gripmock
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gripmock.go
186 lines (161 loc) · 4.79 KB
/
gripmock.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"bytes"
"flag"
"fmt"
"log"
"os"
"os/exec"
"os/signal"
"path"
"strings"
"syscall"
"github.com/tokopedia/gripmock/stub"
)
func main() {
outputPointer := flag.String("o", "", "directory to output server.go. Default is $GOPATH/src/grpc/")
grpcPort := flag.String("grpc-port", "4770", "Port of gRPC tcp server")
grpcBindAddr := flag.String("grpc-listen", "", "Adress the gRPC server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
adminport := flag.String("admin-port", "4771", "Port of stub admin server")
adminBindAddr := flag.String("admin-listen", "", "Adress the admin server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine")
stubPath := flag.String("stub", "", "Path where the stub files are (Optional)")
imports := flag.String("imports", "/protobuf", "comma separated imports path. default path /protobuf is where gripmock Dockerfile install WKT protos")
// for backwards compatibility
if os.Args[1] == "gripmock" {
os.Args = append(os.Args[:1], os.Args[2:]...)
}
flag.Parse()
fmt.Println("Starting GripMock")
if os.Getenv("GOPATH") == "" {
log.Fatal("$GOPATH is empty")
}
output := *outputPointer
if output == "" {
output = os.Getenv("GOPATH") + "/src/grpc"
}
// for safety
output += "/"
if _, err := os.Stat(output); os.IsNotExist(err) {
os.Mkdir(output, os.ModePerm)
}
// run admin stub server
stub.RunStubServer(stub.Options{
StubPath: *stubPath,
Port: *adminport,
BindAddr: *adminBindAddr,
})
// parse proto files
protoPaths := flag.Args()
if len(protoPaths) == 0 {
log.Fatal("Need at least one proto file")
}
importDirs := strings.Split(*imports, ",")
// generate pb.go and grpc server based on proto
generateProtoc(protocParam{
protoPath: protoPaths,
adminPort: *adminport,
grpcAddress: *grpcBindAddr,
grpcPort: *grpcPort,
output: output,
imports: importDirs,
})
// build the server
//buildServer(output)
// and run
run, runerr := runGrpcServer(output)
var term = make(chan os.Signal)
signal.Notify(term, syscall.SIGTERM, syscall.SIGKILL, syscall.SIGINT)
select {
case err := <-runerr:
log.Fatal(err)
case <-term:
fmt.Println("Stopping gRPC Server")
run.Process.Kill()
}
}
type protocParam struct {
protoPath []string
adminPort string
grpcAddress string
grpcPort string
output string
imports []string
}
func getProtodirs(protoPath string, imports []string) []string {
// deduced protodir from protoPath
splitpath := strings.Split(protoPath, "/")
protodir := ""
if len(splitpath) > 0 {
protodir = path.Join(splitpath[:len(splitpath)-1]...)
}
// search protodir prefix
protodirIdx := -1
for i := range imports {
dir := path.Join("protogen", imports[i])
if strings.HasPrefix(protodir, dir) {
protodir = dir
protodirIdx = i
break
}
}
protodirs := make([]string, 0, len(imports)+1)
protodirs = append(protodirs, protodir)
// include all dir in imports, skip if it has been added before
for i, dir := range imports {
if i == protodirIdx {
continue
}
protodirs = append(protodirs, dir)
}
return protodirs
}
func generateProtoc(param protocParam) {
param.protoPath = fixGoPackage(param.protoPath)
protodirs := getProtodirs(param.protoPath[0], param.imports)
// estimate args length to prevent expand
args := make([]string, 0, len(protodirs)+len(param.protoPath)+2)
for _, dir := range protodirs {
args = append(args, "-I", dir)
}
// the latest go-grpc plugin will generate subfolders under $GOPATH/src based on go_package option
pbOutput := os.Getenv("GOPATH") + "/src"
args = append(args, param.protoPath...)
args = append(args, "--go_out=plugins=grpc:"+pbOutput)
args = append(args, fmt.Sprintf("--gripmock_out=admin-port=%s,grpc-address=%s,grpc-port=%s:%s",
param.adminPort, param.grpcAddress, param.grpcPort, param.output))
protoc := exec.Command("protoc", args...)
protoc.Stdout = os.Stdout
protoc.Stderr = os.Stderr
err := protoc.Run()
if err != nil {
log.Fatal("Fail on protoc ", err)
}
}
// append gopackage in proto files if doesn't have any
func fixGoPackage(protoPaths []string) []string {
fixgopackage := exec.Command("fix_gopackage.sh", protoPaths...)
buf := &bytes.Buffer{}
fixgopackage.Stdout = buf
fixgopackage.Stderr = os.Stderr
err := fixgopackage.Run()
if err != nil {
log.Println("error on fixGoPackage", err)
return protoPaths
}
return strings.Split(strings.TrimRight(buf.String(), "\n"), "\n")
}
func runGrpcServer(output string) (*exec.Cmd, <-chan error) {
run := exec.Command("go", "run", output+"server.go")
run.Stdout = os.Stdout
run.Stderr = os.Stderr
err := run.Start()
if err != nil {
log.Fatal(err)
}
fmt.Printf("grpc server pid: %d\n", run.Process.Pid)
runerr := make(chan error)
go func() {
runerr <- run.Wait()
}()
return run, runerr
}