Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone gateway package #1239

Merged
merged 13 commits into from
Apr 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ EXAMPLES=examples/internal/proto/examplepb/echo_service.proto \
examples/internal/proto/examplepb/use_go_template.proto \
examples/internal/proto/examplepb/response_body_service.proto

STANDALONE_EXAMPLES=examples/internal/proto/examplepb/unannotated_echo_service.proto

HELLOWORLD=examples/internal/helloworld/helloworld.proto

EXAMPLE_SVCSRCS=$(EXAMPLES:.proto=.pb.go)
Expand Down Expand Up @@ -171,8 +173,12 @@ $(RUNTIME_TEST_SRCS): $(GO_PLUGIN) $(RUNTIME_TEST_PROTO)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GO_PLUGIN) --go_out=$(PKGMAP),plugins=grpc,paths=source_relative:. $(RUNTIME_TEST_PROTO)

$(EXAMPLE_GWSRCS): ADDITIONAL_GW_FLAGS:=$(ADDITIONAL_GW_FLAGS),grpc_api_configuration=examples/internal/proto/examplepb/unannotated_echo_service.yaml
$(EXAMPLE_GWSRCS): ADDITIONAL_SA_FLAGS:=,standalone=true,grpc_api_configuration=examples/internal/proto/examplepb/standalone_echo_service.yaml
$(EXAMPLE_GWSRCS): $(GATEWAY_PLUGIN) $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GATEWAY_PLUGIN) --grpc-gateway_out=logtostderr=true,allow_repeated_fields_in_body=true,$(PKGMAP)$(ADDITIONAL_GW_FLAGS):. $(EXAMPLES)
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GATEWAY_PLUGIN) --grpc-gateway_out=logtostderr=true,allow_repeated_fields_in_body=true,paths=source_relative$(ADDITIONAL_SA_FLAGS):. $(STANDALONE_EXAMPLES)
mv examples/internal/proto/examplepb/unannotated_echo_service.pb.gw.go examples/internal/proto/standalone/
protoc -I $(PROTOC_INC_PATH) -I. -I$(GOOGLEAPIS_DIR) --plugin=$(GATEWAY_PLUGIN) --grpc-gateway_out=logtostderr=true,allow_repeated_fields_in_body=true,paths=source_relative,$(PKGMAP)$(ADDITIONAL_GW_FLAGS):. $(EXAMPLES)


$(EXAMPLE_SWAGGERSRCS): ADDITIONAL_SWG_FLAGS:=$(ADDITIONAL_SWG_FLAGS),grpc_api_configuration=examples/internal/proto/examplepb/unannotated_echo_service.yaml
$(EXAMPLE_SWAGGERSRCS): $(SWAGGER_PLUGIN) $(SWAGGER_EXAMPLES)
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ annotation to your .proto file
```

It will generate a reverse proxy `gen/go/your/service/v1/your_service.pb.gw.go`.

OR generate a standalone reverse-proxy if needed.

Suppose you have a generated gRPC stub package, and you want to deploy several
API gateways using client-specific
[YAML annotations](https://grpc-ecosystem.github.io/grpc-gateway/docs/grpcapiconfiguration.html).
You can generate a grpc-gateway which imports the stub as an external
package, so you don't have to regenerate it several times.
To set the import path of the stub package, set its full path in the `go_package`.

```sh
protoc -I. --grpc-gateway_out=logtostderr=true,grpc_api_configuration=apiOne.yaml,paths=source_relative,standalone=true:./gen/go/client_one \
your/service/v1/your_service.proto

protoc -I. --grpc-gateway_out=logtostderr=true,grpc_api_configuration=apiTwo.yaml,paths=source_relative,standalone=true:./gen/go/client_two \
your/service/v1/your_service.proto

6. Write an entrypoint for the HTTP reverse-proxy server

Expand Down
1 change: 1 addition & 0 deletions examples/internal/clients/unannotatedecho/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
"api_unannotated_echo_service.go",
"client.go",
"configuration.go",
"model_examplepb_unannotated_embedded.go",
"model_examplepb_unannotated_simple_message.go",
"model_protobuf_any.go",
"model_runtime_error.go",
Expand Down
125 changes: 125 additions & 0 deletions examples/internal/clients/unannotatedecho/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,52 @@ paths:
type: "string"
x-exportParamName: "Duration"
x-optionalDataType: "String"
- name: "line_num"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "LineNum"
x-optionalDataType: "String"
- name: "lang"
in: "query"
required: false
type: "string"
x-exportParamName: "Lang"
x-optionalDataType: "String"
- name: "status.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "StatusProgress"
x-optionalDataType: "String"
- name: "status.note"
in: "query"
required: false
type: "string"
x-exportParamName: "StatusNote"
x-optionalDataType: "String"
- name: "en"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "En"
x-optionalDataType: "String"
- name: "no.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "NoProgress"
x-optionalDataType: "String"
- name: "no.note"
in: "query"
required: false
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand Down Expand Up @@ -119,6 +165,52 @@ paths:
type: "string"
x-exportParamName: "Duration"
x-optionalDataType: "String"
- name: "line_num"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "LineNum"
x-optionalDataType: "String"
- name: "lang"
in: "query"
required: false
type: "string"
x-exportParamName: "Lang"
x-optionalDataType: "String"
- name: "status.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "StatusProgress"
x-optionalDataType: "String"
- name: "status.note"
in: "query"
required: false
type: "string"
x-exportParamName: "StatusNote"
x-optionalDataType: "String"
- name: "en"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "En"
x-optionalDataType: "String"
- name: "no.progress"
in: "query"
required: false
type: "string"
format: "int64"
x-exportParamName: "NoProgress"
x-optionalDataType: "String"
- name: "no.note"
in: "query"
required: false
type: "string"
x-exportParamName: "NoNote"
x-optionalDataType: "String"
responses:
200:
description: "A successful response."
Expand All @@ -129,6 +221,18 @@ paths:
schema:
$ref: "#/definitions/runtimeError"
definitions:
examplepbUnannotatedEmbedded:
type: "object"
properties:
progress:
type: "string"
format: "int64"
note:
type: "string"
description: "Embedded represents a message embedded in SimpleMessage."
example:
note: "note"
progress: "progress"
examplepbUnannotatedSimpleMessage:
type: "object"
properties:
Expand All @@ -140,12 +244,33 @@ definitions:
format: "int64"
duration:
type: "string"
line_num:
type: "string"
format: "int64"
lang:
type: "string"
status:
$ref: "#/definitions/examplepbUnannotatedEmbedded"
en:
type: "string"
format: "int64"
"no":
$ref: "#/definitions/examplepbUnannotatedEmbedded"
description: "UnannotatedSimpleMessage represents a simple message sent to the\
\ unannotated Echo service."
example:
duration: "duration"
"no":
note: "note"
progress: "progress"
line_num: "line_num"
num: "num"
en: "en"
id: "id"
lang: "lang"
status:
note: "note"
progress: "progress"
protobufAny:
type: "object"
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,26 @@ The message posted as the id parameter will also be returned.
* @param num
* @param optional nil or *UnannotatedEchoServiceEcho2Opts - Optional Parameters:
* @param "Duration" (optional.String) -
* @param "LineNum" (optional.String) -
* @param "Lang" (optional.String) -
* @param "StatusProgress" (optional.String) -
* @param "StatusNote" (optional.String) -
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -

@return ExamplepbUnannotatedSimpleMessage
*/

type UnannotatedEchoServiceEcho2Opts struct {
Duration optional.String
LineNum optional.String
Lang optional.String
StatusProgress optional.String
StatusNote optional.String
En optional.String
NoProgress optional.String
NoNote optional.String
}

func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho2(ctx context.Context, id string, num string, localVarOptionals *UnannotatedEchoServiceEcho2Opts) (ExamplepbUnannotatedSimpleMessage, *http.Response, error) {
Expand All @@ -163,6 +177,27 @@ func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEcho2(ctx conte
if localVarOptionals != nil && localVarOptionals.Duration.IsSet() {
localVarQueryParams.Add("duration", parameterToString(localVarOptionals.Duration.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.LineNum.IsSet() {
localVarQueryParams.Add("line_num", parameterToString(localVarOptionals.LineNum.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.Lang.IsSet() {
localVarQueryParams.Add("lang", parameterToString(localVarOptionals.Lang.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusProgress.IsSet() {
localVarQueryParams.Add("status.progress", parameterToString(localVarOptionals.StatusProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusNote.IsSet() {
localVarQueryParams.Add("status.note", parameterToString(localVarOptionals.StatusNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.En.IsSet() {
localVarQueryParams.Add("en", parameterToString(localVarOptionals.En.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoProgress.IsSet() {
localVarQueryParams.Add("no.progress", parameterToString(localVarOptionals.NoProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json"}

Expand Down Expand Up @@ -345,6 +380,13 @@ UnannotatedEchoServiceApiService EchoDelete method receives a simple message and
* @param "Id" (optional.String) - Id represents the message identifier.
* @param "Num" (optional.String) -
* @param "Duration" (optional.String) -
* @param "LineNum" (optional.String) -
* @param "Lang" (optional.String) -
* @param "StatusProgress" (optional.String) -
* @param "StatusNote" (optional.String) -
* @param "En" (optional.String) -
* @param "NoProgress" (optional.String) -
* @param "NoNote" (optional.String) -

@return ExamplepbUnannotatedSimpleMessage
*/
Expand All @@ -353,6 +395,13 @@ type UnannotatedEchoServiceEchoDeleteOpts struct {
Id optional.String
Num optional.String
Duration optional.String
LineNum optional.String
Lang optional.String
StatusProgress optional.String
StatusNote optional.String
En optional.String
NoProgress optional.String
NoNote optional.String
}

func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEchoDelete(ctx context.Context, localVarOptionals *UnannotatedEchoServiceEchoDeleteOpts) (ExamplepbUnannotatedSimpleMessage, *http.Response, error) {
Expand Down Expand Up @@ -380,6 +429,27 @@ func (a *UnannotatedEchoServiceApiService) UnannotatedEchoServiceEchoDelete(ctx
if localVarOptionals != nil && localVarOptionals.Duration.IsSet() {
localVarQueryParams.Add("duration", parameterToString(localVarOptionals.Duration.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.LineNum.IsSet() {
localVarQueryParams.Add("line_num", parameterToString(localVarOptionals.LineNum.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.Lang.IsSet() {
localVarQueryParams.Add("lang", parameterToString(localVarOptionals.Lang.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusProgress.IsSet() {
localVarQueryParams.Add("status.progress", parameterToString(localVarOptionals.StatusProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.StatusNote.IsSet() {
localVarQueryParams.Add("status.note", parameterToString(localVarOptionals.StatusNote.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.En.IsSet() {
localVarQueryParams.Add("en", parameterToString(localVarOptionals.En.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoProgress.IsSet() {
localVarQueryParams.Add("no.progress", parameterToString(localVarOptionals.NoProgress.Value(), ""))
}
if localVarOptionals != nil && localVarOptionals.NoNote.IsSet() {
localVarQueryParams.Add("no.note", parameterToString(localVarOptionals.NoNote.Value(), ""))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{"application/json"}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* examples/internal/proto/examplepb/unannotated_echo_service.proto
*
* Unannotated Echo Service Similar to echo_service.proto but without annotations. See unannotated_echo_service.yaml for the equivalent of the annotations in gRPC API configuration format. Echo Service API consists of a single service which returns a message.
*
* API version: version not set
* Generated by: Swagger Codegen (https://github.com/swagger-api/swagger-codegen.git)
*/

package unannotatedecho

// Embedded represents a message embedded in SimpleMessage.
type ExamplepbUnannotatedEmbedded struct {
Progress string `json:"progress,omitempty"`
Note string `json:"note,omitempty"`
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ type ExamplepbUnannotatedSimpleMessage struct {
Id string `json:"id,omitempty"`
Num string `json:"num,omitempty"`
Duration string `json:"duration,omitempty"`
LineNum string `json:"line_num,omitempty"`
Lang string `json:"lang,omitempty"`
Status *ExamplepbUnannotatedEmbedded `json:"status,omitempty"`
En string `json:"en,omitempty"`
No *ExamplepbUnannotatedEmbedded `json:"no,omitempty"`
}
1 change: 1 addition & 0 deletions examples/internal/gateway/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
visibility = ["//visibility:public"],
deps = [
"//examples/internal/proto/examplepb:go_default_library",
"//examples/internal/proto/standalone:go_default_library",
"//runtime:go_default_library",
"@com_github_golang_glog//:go_default_library",
"@org_golang_google_grpc//:go_default_library",
Expand Down
2 changes: 2 additions & 0 deletions examples/internal/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/examplepb"
standalone "github.com/grpc-ecosystem/grpc-gateway/v2/examples/internal/proto/standalone"
gwruntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"google.golang.org/grpc"
)
Expand All @@ -19,6 +20,7 @@ func newGateway(ctx context.Context, conn *grpc.ClientConn, opts []gwruntime.Ser

for _, f := range []func(context.Context, *gwruntime.ServeMux, *grpc.ClientConn) error{
examplepb.RegisterEchoServiceHandler,
standalone.RegisterUnannotatedEchoServiceHandler,
examplepb.RegisterStreamServiceHandler,
examplepb.RegisterABitOfEverythingServiceHandler,
examplepb.RegisterFlowCombinationHandler,
Expand Down
Loading