Skip to content

Commit 56c24b9

Browse files
committed
init openapi
1 parent 9f1bd81 commit 56c24b9

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

openapi/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
gen/
2+
gateway
3+
go.sum

openapi/Makefile

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
PROTOC=protoc \
2+
-I ../proto/ \
3+
-I ../proto/wechaty/ \
4+
../proto/wechaty/puppet.proto \
5+
6+
.PHONY: all
7+
all: clean generate reverse_proxy_server
8+
9+
.PHONY: clean
10+
clean:
11+
rm -fr gen/*
12+
13+
.PHONY: install
14+
install:
15+
[ -d gen ] || mkdir gen
16+
go install \
17+
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway \
18+
github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2 \
19+
google.golang.org/protobuf/cmd/protoc-gen-go \
20+
google.golang.org/grpc/cmd/protoc-gen-go-grpc
21+
go mod tidy
22+
23+
.PHONY: generate
24+
generate: gen_protobuf gen_gateway gen_openapi
25+
26+
.PHONY: gen_protobuf
27+
gen_protobuf:
28+
${PROTOC} \
29+
--go_out ./gen/ --go_opt paths=source_relative \
30+
--go-grpc_out ./gen/ --go-grpc_opt paths=source_relative \
31+
../proto/wechaty/puppet.proto
32+
33+
.PHONY: gen_gateway
34+
gen_gateway:
35+
${PROTOC} \
36+
--grpc-gateway_out ./gen/ \
37+
--grpc-gateway_opt logtostderr=true \
38+
--grpc-gateway_opt paths=source_relative \
39+
--grpc-gateway_opt generate_unbound_methods=true \
40+
41+
.PHONY: gen_openapi
42+
gen_openapi:
43+
${PROTOC} \
44+
--openapiv2_out ./gen/ \
45+
--openapiv2_opt logtostderr=true \
46+

openapi/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2+
gRPC Gateway: <https://github.com/grpc-ecosystem/grpc-gateway>

openapi/gateway.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"flag"
6+
"net/http"
7+
8+
"github.com/golang/glog"
9+
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
10+
"google.golang.org/grpc"
11+
12+
"github.com/wechaty/go-grpc/openapi/gen/wechaty" // Update
13+
)
14+
15+
var (
16+
// command-line options:
17+
// gRPC server endpoint
18+
grpcServerEndpoint = flag.String("grpc-server-endpoint", "localhost:9090", "gRPC server endpoint")
19+
)
20+
21+
func run() error {
22+
ctx := context.Background()
23+
ctx, cancel := context.WithCancel(ctx)
24+
defer cancel()
25+
26+
// Register gRPC server endpoint
27+
// Note: Make sure the gRPC server is running properly and accessible
28+
mux := runtime.NewServeMux()
29+
opts := []grpc.DialOption{grpc.WithInsecure()}
30+
err := wechaty.RegisterPuppetHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
31+
if err != nil {
32+
return err
33+
}
34+
35+
// Start HTTP server (and proxy calls to gRPC server endpoint)
36+
return http.ListenAndServe(":8081", mux)
37+
}
38+
39+
func main() {
40+
flag.Parse()
41+
defer glog.Flush()
42+
43+
if err := run(); err != nil {
44+
glog.Fatal(err)
45+
}
46+
}

openapi/go.mod

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module github.com/wechaty/go-grpc/openapi
2+
3+
go 1.16
4+
5+
require (
6+
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
7+
github.com/golang/protobuf v1.4.3
8+
github.com/grpc-ecosystem/grpc-gateway/v2 v2.1.0
9+
github.com/wechaty/go-grpc v0.18.12
10+
google.golang.org/grpc v1.34.0
11+
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0
12+
google.golang.org/protobuf v1.25.0
13+
)

openapi/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// +build tools
2+
3+
package tools
4+
5+
import (
6+
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway"
7+
_ "github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2"
8+
_ "google.golang.org/grpc/cmd/protoc-gen-go-grpc"
9+
_ "google.golang.org/protobuf/cmd/protoc-gen-go"
10+
)

0 commit comments

Comments
 (0)