diff --git a/example/hello-world-idl/Makefile b/example/hello-world-idl/Makefile index 314e76634..e97a9724e 100644 --- a/example/hello-world-idl/Makefile +++ b/example/hello-world-idl/Makefile @@ -1,11 +1,13 @@ PROTOC_VERSION := 3.9.2 PROTOC_WEB_VERSION := 1.0.6 + PROTO_SOURCES := -I /usr/local/include PROTO_SOURCES += -I . PROTO_SOURCES += -I ${GOPATH}/src PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis +PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway -deps-linux: deps-protoc-linux deps-grpc-web-linux +deps-linux: deps-protoc-linux deps-grpc-web-linux deps-go deps-protoc-linux: curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip @@ -19,9 +21,14 @@ deps-grpc-web-linux: sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web chmod +x /usr/local/bin/protoc-gen-grpc-web -generate: generate-grpc generate-web +deps-go: + go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway + go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger + go get -u github.com/golang/protobuf/protoc-gen-go + +generate: generate-grpc generate-web generate-http -generate-grpc: +generate-grpc: mkdir -p gen/go protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/health/*.proto protoc ${PROTO_SOURCES} --go_out=plugins=grpc:./gen/go ./proto/helloworld/*.proto @@ -33,4 +40,11 @@ generate-web: protoc ${PROTO_SOURCES} --grpc-web_out=import_style=typescript,mode=grpcwebtext:gen/web ./proto/helloworld/*.proto cp -f -rv gen/web/proto/* gen/web rm -rf gen/web/Proto gen/web/proto +generate-http: + mkdir -p gen/http gen/swagger + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/health/*.proto + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/helloworld/*.proto + cp -f -rv gen/http/proto/* gen/http + cp -f -rv gen/swagger/proto/* gen/swagger + rm -rf gen/swagger/proto diff --git a/example/hello-world-idl/gen/go/health/health.pb.go b/example/hello-world-idl/gen/go/health/health.pb.go index db6a6eb6c..f4e617e8b 100644 --- a/example/hello-world-idl/gen/go/health/health.pb.go +++ b/example/hello-world-idl/gen/go/health/health.pb.go @@ -7,6 +7,8 @@ import ( context "context" fmt "fmt" proto "github.com/golang/protobuf/proto" + _ "github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger/options" + _ "google.golang.org/genproto/googleapis/api/annotations" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" status "google.golang.org/grpc/status" @@ -142,22 +144,28 @@ func init() { func init() { proto.RegisterFile("proto/health/health.proto", fileDescriptor_7bb2e9a145ccb86d) } var fileDescriptor_7bb2e9a145ccb86d = []byte{ - // 238 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x92, 0x2c, 0x28, 0xca, 0x2f, - 0xc9, 0xd7, 0xcf, 0x48, 0x4d, 0xcc, 0x29, 0xc9, 0x80, 0x52, 0x7a, 0x60, 0x31, 0x21, 0x81, 0xdc, - 0xca, 0xe4, 0xfc, 0xdc, 0x82, 0xc4, 0xbc, 0x4a, 0x3d, 0x88, 0xb8, 0x92, 0x1e, 0x97, 0x90, 0x07, - 0x98, 0xe5, 0x9c, 0x91, 0x9a, 0x9c, 0x1d, 0x94, 0x5a, 0x58, 0x9a, 0x5a, 0x5c, 0x22, 0x24, 0xc1, - 0xc5, 0x5e, 0x9c, 0x5a, 0x54, 0x96, 0x99, 0x9c, 0x2a, 0xc1, 0xa8, 0xc0, 0xa8, 0xc1, 0x19, 0x04, - 0xe3, 0x2a, 0x6d, 0x66, 0xe4, 0x12, 0x46, 0xd1, 0x50, 0x5c, 0x90, 0x9f, 0x57, 0x9c, 0x2a, 0xe4, - 0xcd, 0xc5, 0x56, 0x5c, 0x92, 0x58, 0x52, 0x5a, 0x0c, 0xd6, 0xc0, 0x67, 0x64, 0xac, 0x87, 0x6e, - 0x95, 0x1e, 0x16, 0x6d, 0x7a, 0xc1, 0x20, 0x63, 0xf3, 0xd2, 0x83, 0xc1, 0x5a, 0x83, 0xa0, 0x46, - 0x28, 0xf9, 0x73, 0xf1, 0xa2, 0x48, 0x08, 0x71, 0x73, 0xb1, 0x87, 0xfa, 0x79, 0xfb, 0xf9, 0x87, - 0xfb, 0x09, 0x30, 0x80, 0x38, 0xc1, 0xae, 0x41, 0x61, 0x9e, 0x7e, 0xee, 0x02, 0x8c, 0x42, 0xfc, - 0x5c, 0xdc, 0x7e, 0xfe, 0x21, 0xf1, 0x30, 0x01, 0x26, 0x21, 0x61, 0x2e, 0x7e, 0x30, 0xc7, 0xd9, - 0x35, 0x1e, 0xa6, 0x85, 0xd9, 0x68, 0x1b, 0x23, 0x17, 0x1b, 0xc4, 0x7a, 0xa1, 0x10, 0x2e, 0x56, - 0xb0, 0x13, 0x84, 0x54, 0x08, 0xb8, 0x10, 0x1c, 0x12, 0x52, 0xaa, 0x44, 0xf9, 0x43, 0x28, 0x8c, - 0x8b, 0x35, 0x3c, 0xb1, 0x24, 0x39, 0x83, 0xaa, 0xa6, 0x1a, 0x30, 0x26, 0xb1, 0x81, 0xe3, 0xcd, - 0x18, 0x10, 0x00, 0x00, 0xff, 0xff, 0x6e, 0xa9, 0x0a, 0x0e, 0xd4, 0x01, 0x00, 0x00, + // 324 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x51, 0xcd, 0x4e, 0x02, 0x31, + 0x10, 0xb6, 0x10, 0x20, 0x0e, 0x22, 0x64, 0x30, 0x06, 0x88, 0x07, 0xb2, 0xd1, 0xc4, 0x83, 0x74, + 0xf9, 0x79, 0x02, 0x25, 0x44, 0x0d, 0xc9, 0x92, 0x2c, 0x0a, 0x47, 0x52, 0x37, 0x4d, 0x97, 0x08, + 0xed, 0x4a, 0x0b, 0x86, 0xab, 0x8f, 0xa0, 0x8f, 0xe2, 0x5b, 0x78, 0xf5, 0x15, 0x7c, 0x10, 0x43, + 0x17, 0x0e, 0xa8, 0x89, 0x1e, 0x3c, 0x4d, 0x66, 0xbe, 0x9f, 0xf9, 0xda, 0x81, 0x72, 0x34, 0x53, + 0x46, 0xb9, 0x21, 0x67, 0x13, 0x13, 0xae, 0x0b, 0xb5, 0x33, 0x2c, 0x4c, 0x97, 0x81, 0x9a, 0x46, + 0x4c, 0x2e, 0x69, 0x3c, 0xaf, 0x1c, 0x09, 0xa5, 0xc4, 0x84, 0xbb, 0x2c, 0x1a, 0xbb, 0x4c, 0x4a, + 0x65, 0x98, 0x19, 0x2b, 0xa9, 0x63, 0x7e, 0xe5, 0xcc, 0x96, 0xa0, 0x26, 0xb8, 0xac, 0xe9, 0x47, + 0x26, 0x04, 0x9f, 0xb9, 0x2a, 0xb2, 0x8c, 0xef, 0x6c, 0x87, 0x02, 0x5e, 0x59, 0xd7, 0x76, 0xc8, + 0x83, 0x7b, 0x9f, 0x3f, 0xcc, 0xb9, 0x36, 0x58, 0x82, 0x8c, 0xe6, 0xb3, 0xc5, 0x38, 0xe0, 0x25, + 0x52, 0x25, 0xa7, 0xbb, 0xfe, 0xa6, 0x75, 0x5e, 0x09, 0x14, 0xb7, 0x04, 0x3a, 0x52, 0x52, 0x73, + 0xec, 0x42, 0x5a, 0x1b, 0x66, 0xe6, 0xda, 0x0a, 0xf6, 0x9b, 0x2d, 0xfa, 0x35, 0x36, 0xfd, 0x41, + 0x46, 0xfb, 0x2b, 0x5b, 0x29, 0xfa, 0x56, 0xea, 0xaf, 0x2d, 0x9c, 0x1e, 0xe4, 0xb6, 0x00, 0xcc, + 0x42, 0xe6, 0xd6, 0xeb, 0x7a, 0xbd, 0xa1, 0x57, 0xd8, 0x59, 0x35, 0xfd, 0x8e, 0x3f, 0xb8, 0xf6, + 0x2e, 0x0b, 0x04, 0xf3, 0x90, 0xf5, 0x7a, 0x37, 0xa3, 0xcd, 0x20, 0x81, 0x45, 0xc8, 0xdb, 0xa6, + 0xdd, 0x19, 0x6d, 0x24, 0xc9, 0xe6, 0x1b, 0x81, 0x74, 0xbc, 0x1e, 0x43, 0x48, 0xd9, 0x08, 0x78, + 0xfc, 0x4b, 0x42, 0xfb, 0x13, 0x95, 0x93, 0x3f, 0xbd, 0xc3, 0xc1, 0xa7, 0xf7, 0x8f, 0x97, 0xc4, + 0x1e, 0x82, 0xbb, 0x68, 0xac, 0xcf, 0x87, 0x03, 0x48, 0x0d, 0x99, 0x09, 0xc2, 0x7f, 0xdd, 0x54, + 0x27, 0x17, 0xe5, 0xe7, 0xf3, 0x43, 0x3c, 0x80, 0x5c, 0x8c, 0x56, 0x2d, 0xac, 0x9b, 0xc9, 0x06, + 0xad, 0xdf, 0xa5, 0xed, 0x51, 0x5b, 0x9f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x27, 0xce, 0xfd, 0xfa, + 0x4f, 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/example/hello-world-idl/gen/http/health/health.pb.gw.go b/example/hello-world-idl/gen/http/health/health.pb.gw.go new file mode 100644 index 000000000..0e2602738 --- /dev/null +++ b/example/hello-world-idl/gen/http/health/health.pb.gw.go @@ -0,0 +1,159 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: proto/health/health.proto + +/* +Package mycompany_health is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package mycompany_health + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray + +var ( + filter_Health_Check_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + +func request_Health_Check_0(ctx context.Context, marshaler runtime.Marshaler, client HealthClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HealthCheckRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Health_Check_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Check(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Health_Check_0(ctx context.Context, marshaler runtime.Marshaler, server HealthServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HealthCheckRequest + var metadata runtime.ServerMetadata + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Health_Check_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.Check(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterHealthHandlerServer registers the http handlers for service Health to "mux". +// UnaryRPC :call HealthServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +func RegisterHealthHandlerServer(ctx context.Context, mux *runtime.ServeMux, server HealthServer) error { + + mux.Handle("GET", pattern_Health_Check_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Health_Check_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Health_Check_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterHealthHandlerFromEndpoint is same as RegisterHealthHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterHealthHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterHealthHandler(ctx, mux, conn) +} + +// RegisterHealthHandler registers the http handlers for service Health to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterHealthHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterHealthHandlerClient(ctx, mux, NewHealthClient(conn)) +} + +// RegisterHealthHandlerClient registers the http handlers for service Health +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "HealthClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "HealthClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "HealthClient" to call the correct interceptors. +func RegisterHealthHandlerClient(ctx context.Context, mux *runtime.ServeMux, client HealthClient) error { + + mux.Handle("GET", pattern_Health_Check_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Health_Check_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Health_Check_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Health_Check_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "health"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Health_Check_0 = runtime.ForwardResponseMessage +) diff --git a/example/hello-world-idl/gen/http/proto/health/health.pb.gw.go b/example/hello-world-idl/gen/http/proto/health/health.pb.gw.go new file mode 100644 index 000000000..0e2602738 --- /dev/null +++ b/example/hello-world-idl/gen/http/proto/health/health.pb.gw.go @@ -0,0 +1,159 @@ +// Code generated by protoc-gen-grpc-gateway. DO NOT EDIT. +// source: proto/health/health.proto + +/* +Package mycompany_health is a reverse proxy. + +It translates gRPC into RESTful JSON APIs. +*/ +package mycompany_health + +import ( + "context" + "io" + "net/http" + + "github.com/golang/protobuf/proto" + "github.com/grpc-ecosystem/grpc-gateway/runtime" + "github.com/grpc-ecosystem/grpc-gateway/utilities" + "google.golang.org/grpc" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/grpclog" + "google.golang.org/grpc/status" +) + +var _ codes.Code +var _ io.Reader +var _ status.Status +var _ = runtime.String +var _ = utilities.NewDoubleArray + +var ( + filter_Health_Check_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + +func request_Health_Check_0(ctx context.Context, marshaler runtime.Marshaler, client HealthClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HealthCheckRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_Health_Check_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.Check(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_Health_Check_0(ctx context.Context, marshaler runtime.Marshaler, server HealthServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq HealthCheckRequest + var metadata runtime.ServerMetadata + + if err := runtime.PopulateQueryParameters(&protoReq, req.URL.Query(), filter_Health_Check_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.Check(ctx, &protoReq) + return msg, metadata, err + +} + +// RegisterHealthHandlerServer registers the http handlers for service Health to "mux". +// UnaryRPC :call HealthServer directly. +// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. +func RegisterHealthHandlerServer(ctx context.Context, mux *runtime.ServeMux, server HealthServer) error { + + mux.Handle("GET", pattern_Health_Check_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateIncomingContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_Health_Check_0(rctx, inboundMarshaler, server, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Health_Check_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +// RegisterHealthHandlerFromEndpoint is same as RegisterHealthHandler but +// automatically dials to "endpoint" and closes the connection when "ctx" gets done. +func RegisterHealthHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { + conn, err := grpc.Dial(endpoint, opts...) + if err != nil { + return err + } + defer func() { + if err != nil { + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + return + } + go func() { + <-ctx.Done() + if cerr := conn.Close(); cerr != nil { + grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) + } + }() + }() + + return RegisterHealthHandler(ctx, mux, conn) +} + +// RegisterHealthHandler registers the http handlers for service Health to "mux". +// The handlers forward requests to the grpc endpoint over "conn". +func RegisterHealthHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { + return RegisterHealthHandlerClient(ctx, mux, NewHealthClient(conn)) +} + +// RegisterHealthHandlerClient registers the http handlers for service Health +// to "mux". The handlers forward requests to the grpc endpoint over the given implementation of "HealthClient". +// Note: the gRPC framework executes interceptors within the gRPC handler. If the passed in "HealthClient" +// doesn't go through the normal gRPC flow (creating a gRPC client etc.) then it will be up to the passed in +// "HealthClient" to call the correct interceptors. +func RegisterHealthHandlerClient(ctx context.Context, mux *runtime.ServeMux, client HealthClient) error { + + mux.Handle("GET", pattern_Health_Check_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + rctx, err := runtime.AnnotateContext(ctx, mux, req) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_Health_Check_0(rctx, inboundMarshaler, client, req, pathParams) + ctx = runtime.NewServerMetadataContext(ctx, md) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + + forward_Health_Check_0(ctx, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + return nil +} + +var ( + pattern_Health_Check_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "health"}, "", runtime.AssumeColonVerbOpt(true))) +) + +var ( + forward_Health_Check_0 = runtime.ForwardResponseMessage +) diff --git a/example/hello-world-idl/gen/swagger/health/health.swagger.json b/example/hello-world-idl/gen/swagger/health/health.swagger.json new file mode 100644 index 000000000..5ecef0a83 --- /dev/null +++ b/example/hello-world-idl/gen/swagger/health/health.swagger.json @@ -0,0 +1,117 @@ +{ + "swagger": "2.0", + "info": { + "title": "Health Checks", + "version": "1.0" + }, + "schemes": [ + "http", + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": { + "/v1/health": { + "get": { + "operationId": "Check", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/healthHealthCheckResponse" + } + } + }, + "parameters": [ + { + "name": "service", + "in": "query", + "required": false, + "type": "string" + } + ], + "tags": [ + "Health" + ] + } + } + }, + "definitions": { + "HealthCheckResponseServingStatus": { + "type": "string", + "enum": [ + "UNKNOWN", + "SERVING", + "NOT_SERVING", + "SERVICE_UNKNOWN" + ], + "default": "UNKNOWN" + }, + "healthHealthCheckResponse": { + "type": "object", + "properties": { + "status": { + "$ref": "#/definitions/HealthCheckResponseServingStatus" + } + } + }, + "protobufAny": { + "type": "object", + "properties": { + "type_url": { + "type": "string", + "description": "A URL/resource name that uniquely identifies the type of the serialized\nprotocol buffer message. This string must contain at least\none \"/\" character. The last segment of the URL's path must represent\nthe fully qualified name of the type (as in\n`path/google.protobuf.Duration`). The name should be in a canonical form\n(e.g., leading \".\" is not accepted).\n\nIn practice, teams usually precompile into the binary all types that they\nexpect it to use in the context of Any. However, for URLs which use the\nscheme `http`, `https`, or no scheme, one can optionally set up a type\nserver that maps type URLs to message definitions as follows:\n\n* If no scheme is provided, `https` is assumed.\n* An HTTP GET on the URL must yield a [google.protobuf.Type][]\n value in binary format, or produce an error.\n* Applications are allowed to cache lookup results based on the\n URL, or have them precompiled into a binary to avoid any\n lookup. Therefore, binary compatibility needs to be preserved\n on changes to types. (Use versioned type names to manage\n breaking changes.)\n\nNote: this functionality is not currently available in the official\nprotobuf release, and it is not used for type URLs beginning with\ntype.googleapis.com.\n\nSchemes other than `http`, `https` (or the empty scheme) might be\nused with implementation specific semantics." + }, + "value": { + "type": "string", + "format": "byte", + "description": "Must be a valid serialized protocol buffer of the above specified type." + } + }, + "description": "`Any` contains an arbitrary serialized protocol buffer message along with a\nURL that describes the type of the serialized message.\n\nProtobuf library provides support to pack/unpack Any values in the form\nof utility functions or additional generated methods of the Any type.\n\nExample 1: Pack and unpack a message in C++.\n\n Foo foo = ...;\n Any any;\n any.PackFrom(foo);\n ...\n if (any.UnpackTo(\u0026foo)) {\n ...\n }\n\nExample 2: Pack and unpack a message in Java.\n\n Foo foo = ...;\n Any any = Any.pack(foo);\n ...\n if (any.is(Foo.class)) {\n foo = any.unpack(Foo.class);\n }\n\n Example 3: Pack and unpack a message in Python.\n\n foo = Foo(...)\n any = Any()\n any.Pack(foo)\n ...\n if any.Is(Foo.DESCRIPTOR):\n any.Unpack(foo)\n ...\n\n Example 4: Pack and unpack a message in Go\n\n foo := \u0026pb.Foo{...}\n any, err := ptypes.MarshalAny(foo)\n ...\n foo := \u0026pb.Foo{}\n if err := ptypes.UnmarshalAny(any, foo); err != nil {\n ...\n }\n\nThe pack methods provided by protobuf library will by default use\n'type.googleapis.com/full.type.name' as the type URL and the unpack\nmethods only use the fully qualified type name after the last '/'\nin the type URL, for example \"foo.bar.com/x/y.z\" will yield type\nname \"y.z\".\n\n\nJSON\n====\nThe JSON representation of an `Any` value uses the regular\nrepresentation of the deserialized, embedded message, with an\nadditional field `@type` which contains the type URL. Example:\n\n package google.profile;\n message Person {\n string first_name = 1;\n string last_name = 2;\n }\n\n {\n \"@type\": \"type.googleapis.com/google.profile.Person\",\n \"firstName\": \u003cstring\u003e,\n \"lastName\": \u003cstring\u003e\n }\n\nIf the embedded message type is well-known and has a custom JSON\nrepresentation, that representation will be embedded adding a field\n`value` which holds the custom JSON in addition to the `@type`\nfield. Example (for message [google.protobuf.Duration][]):\n\n {\n \"@type\": \"type.googleapis.com/google.protobuf.Duration\",\n \"value\": \"1.212s\"\n }" + }, + "runtimeStreamError": { + "type": "object", + "properties": { + "grpc_code": { + "type": "integer", + "format": "int32" + }, + "http_code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + }, + "http_status": { + "type": "string" + }, + "details": { + "type": "array", + "items": { + "$ref": "#/definitions/protobufAny" + } + } + } + } + }, + "x-stream-definitions": { + "healthHealthCheckResponse": { + "type": "object", + "properties": { + "result": { + "$ref": "#/definitions/healthHealthCheckResponse" + }, + "error": { + "$ref": "#/definitions/runtimeStreamError" + } + }, + "title": "Stream result of healthHealthCheckResponse" + } + } +} diff --git a/example/hello-world-idl/gen/swagger/helloworld/helloworld.swagger.json b/example/hello-world-idl/gen/swagger/helloworld/helloworld.swagger.json new file mode 100644 index 000000000..3d9104b74 --- /dev/null +++ b/example/hello-world-idl/gen/swagger/helloworld/helloworld.swagger.json @@ -0,0 +1,19 @@ +{ + "swagger": "2.0", + "info": { + "title": "proto/helloworld/helloworld.proto", + "version": "version not set" + }, + "schemes": [ + "http", + "https" + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "paths": {}, + "definitions": {} +} diff --git a/example/hello-world-idl/gen/web/health/health_pb.d.ts b/example/hello-world-idl/gen/web/health/health_pb.d.ts index 57a475723..015e32a18 100644 --- a/example/hello-world-idl/gen/web/health/health_pb.d.ts +++ b/example/hello-world-idl/gen/web/health/health_pb.d.ts @@ -1,5 +1,8 @@ import * as jspb from "google-protobuf" +import * as google_api_annotations_pb from '../../google/api/annotations_pb'; +import * as protoc$gen$swagger_options_annotations_pb from '../../protoc-gen-swagger/options/annotations_pb'; + export class HealthCheckRequest extends jspb.Message { getService(): string; setService(value: string): void; diff --git a/example/hello-world-idl/proto/health/health.proto b/example/hello-world-idl/proto/health/health.proto index fcdae025f..1068fcfa2 100644 --- a/example/hello-world-idl/proto/health/health.proto +++ b/example/hello-world-idl/proto/health/health.proto @@ -1,6 +1,15 @@ syntax = "proto3"; package mycompany.health; +import "google/api/annotations.proto"; +import "protoc-gen-swagger/options/annotations.proto"; + +option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = { + info: { + title: "Health Checks"; + version: "1.0"; + } +}; message HealthCheckRequest { string service = 1; @@ -17,7 +26,11 @@ message HealthCheckResponse { } service Health { - rpc Check(HealthCheckRequest) returns (HealthCheckResponse); + rpc Check(HealthCheckRequest) returns (HealthCheckResponse){ + option (google.api.http) = { + get: "/v1/health" + }; + } rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); } diff --git a/example/hello-world/sprout.yml b/example/hello-world/sprout.yml index fc44b9aca..8192e2d54 100644 --- a/example/hello-world/sprout.yml +++ b/example/hello-world/sprout.yml @@ -11,12 +11,13 @@ network: grpc: host: 0.0.0.0 port: 3000 - web: + http: enabled: true port: 8080 - graphql: + web: enabled: true - port: 8082 + port: 8090 + services: - name: helloworld diff --git a/templates/proto/health_proto.tmpl b/templates/proto/health_proto.tmpl index 7708c626e..86149f0f2 100644 --- a/templates/proto/health_proto.tmpl +++ b/templates/proto/health_proto.tmpl @@ -2,6 +2,18 @@ syntax = "proto3"; package {{ .Organization }}.health; +{{- if .Network.Http.Enabled }} +import "google/api/annotations.proto"; +import "protoc-gen-swagger/options/annotations.proto"; + +option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = { + info: { + title: "Health Checks"; + version: "1.0"; + } +}; +{{- end}} + message HealthCheckRequest { string service = 1; } @@ -17,7 +29,11 @@ message HealthCheckResponse { } service Health { - rpc Check(HealthCheckRequest) returns (HealthCheckResponse); + rpc Check(HealthCheckRequest) returns (HealthCheckResponse){ + option (google.api.http) = { + get: "/v1/health" + }; + } rpc Watch(HealthCheckRequest) returns (stream HealthCheckResponse); } diff --git a/templates/proto/makefile.tmpl b/templates/proto/makefile.tmpl index ca7d944f2..50f3362ab 100644 --- a/templates/proto/makefile.tmpl +++ b/templates/proto/makefile.tmpl @@ -1,14 +1,18 @@ PROTOC_VERSION := 3.9.2 PROTOC_WEB_VERSION := 1.0.6 + PROTO_SOURCES := -I /usr/local/include PROTO_SOURCES += -I . +{{- if .Network.Http.Enabled }} PROTO_SOURCES += -I ${GOPATH}/src PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway/third_party/googleapis +PROTO_SOURCES += -I ${GOPATH}/src/github.com/grpc-ecosystem/grpc-gateway +{{- end}} {{- $language := .Language }} {{- $http := .Network.Http.Enabled }} -deps-linux: deps-protoc-linux deps-grpc-web-linux +deps-linux: deps-protoc-linux deps-grpc-web-linux deps-go deps-protoc-linux: curl -OL https://github.com/google/protobuf/releases/download/v$(PROTOC_VERSION)/protoc-$(PROTOC_VERSION)-linux-x86_64.zip @@ -22,9 +26,14 @@ deps-grpc-web-linux: sudo mv protoc-gen-grpc-web-$(PROTOC_WEB_VERSION)-linux-x86_64 /usr/local/bin/protoc-gen-grpc-web chmod +x /usr/local/bin/protoc-gen-grpc-web -generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}} +deps-go: + go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-grpc-gateway + go get -u github.com/grpc-ecosystem/grpc-gateway/protoc-gen-swagger + go get -u github.com/golang/protobuf/protoc-gen-go -generate-grpc: +generate: generate-grpc {{ if .Network.Web.Enabled }}generate-web{{- end}} {{ if .Network.Http.Enabled }}generate-http{{- end}} + +generate-grpc: mkdir -p gen/{{ $language }} protoc ${PROTO_SOURCES} --{{ $language }}_out=plugins=grpc:./gen/{{ $language }} ./proto/health/*.proto {{- range .Services}} @@ -33,7 +42,6 @@ generate-grpc: cp -f -rv gen/go/proto/* gen/go rm -rf gen/go/proto - {{- if .Network.Web.Enabled }} generate-web: mkdir -p gen/web @@ -45,3 +53,15 @@ generate-web: rm -rf gen/web/Proto gen/web/proto {{- end}} +{{- if .Network.Http.Enabled }} +generate-http: + mkdir -p gen/http gen/swagger + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/health/*.proto +{{- range .Services}} + protoc ${PROTO_SOURCES} --grpc-gateway_out=logtostderr=true:gen/http --swagger_out=logtostderr=true:gen/swagger ./proto/{{ .Name }}/*.proto +{{- end }} + cp -f -rv gen/http/proto/* gen/http + cp -f -rv gen/swagger/proto/* gen/swagger + rm -rf gen/swagger/proto +{{- end}} + diff --git a/templates/sprout/sprout.tmpl b/templates/sprout/sprout.tmpl index 917932c4f..39bbc05ad 100644 --- a/templates/sprout/sprout.tmpl +++ b/templates/sprout/sprout.tmpl @@ -11,9 +11,13 @@ network: grpc: host: 0.0.0.0 port: 3000 - web: + http: enabled: true port: 8080 + web: + enabled: true + port: 8090 + services: - name: helloworld