{"id":15954797,"url":"https://github.com/tembleking/test-grpc-gateway","last_synced_at":"2025-10-15T19:59:52.418Z","repository":{"id":117580579,"uuid":"524056902","full_name":"tembleking/test-grpc-gateway","owner":"tembleking","description":null,"archived":false,"fork":false,"pushed_at":"2025-06-03T16:43:35.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-06-14T05:03:04.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tembleking.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-08-12T11:07:14.000Z","updated_at":"2025-06-03T16:43:37.000Z","dependencies_parsed_at":"2024-06-20T23:10:35.267Z","dependency_job_id":"331d0ea0-d0e4-41ce-8ab5-c8bffa52589d","html_url":"https://github.com/tembleking/test-grpc-gateway","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/tembleking/test-grpc-gateway","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tembleking%2Ftest-grpc-gateway","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tembleking%2Ftest-grpc-gateway/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tembleking%2Ftest-grpc-gateway/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tembleking%2Ftest-grpc-gateway/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tembleking","download_url":"https://codeload.github.com/tembleking/test-grpc-gateway/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tembleking%2Ftest-grpc-gateway/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259763076,"owners_count":22907407,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-07T13:19:55.363Z","updated_at":"2025-10-15T19:59:47.367Z","avatar_url":"https://github.com/tembleking.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Test of gRPC-gateway\n\nThis is a POC of a server listening to both HTTP and gRPC with autogenerated server and client implementations, and swagger documentation.\n\n## Protobuf definition\n\nThe definition is stored in `pkg/application/proto/service.proto`. It has some annotations for swagger documentation, but the definition is as minimum as follows:\n\n```protobuf\nsyntax = \"proto3\";\n\nimport \"google/api/annotations.proto\";\nimport \"google/protobuf/empty.proto\";\n\noption go_package = \"github.com/tembleking/test-grpc-gateway/pkg/application/proto\";\n\n// The description of the test service\nservice TestService {\n  // The description of the method\n  rpc Test(google.protobuf.Empty) returns (TestResponse) {\n    option (google.api.http) = {\n      get: \"/test\"\n    };\n  }\n}\n\n// TestResponse is the response message for the Test method.\nmessage TestResponse {\n  // Message is the test message.\n  string message = 1;\n}\n```\n\nAs you can see this is a minimum hello world service that will listen to `/test` and will return a response with a `message` field.\n\nThe three files in `pkg/application/proto/.*pb.*go` are autogenerated. The other three `pkg/application/proto/buf.*` files are for [`buf`](https://buf.build) to generate the code.\n\n## Swagger documentation\n\nThe swagger definition is automatically generated in `pkg/application/http/docs/swagger.swagger.yaml`.\n\n\n## HTTP server\nThe HTTP router implementation in `pkg/application/http/router.go` just registers the endpoints defined in the Proto definition and will redirect them to the gRPC server:\n\n```go\noptions := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}\nerr := proto.RegisterTestServiceHandlerFromEndpoint(ctx, mux, grpcListenAddress, options)\n```\n\n## gRPC server\n\nThe gRPC server is implemented in `pkg/application/grpc/grpc.go`. Contains the actual logic that will be executed in the application.\nIn case of an HTTP request to `/test`, this will be processed here as well.\n\n```go\ntype server struct {\n}\n\nfunc (s *server) Test(ctx context.Context, empty *emptypb.Empty) (*proto.TestResponse, error) {\n\treturn \u0026proto.TestResponse{Message: \"Hello world!\"}, nil\n}\n\nfunc NewServer() *grpc.Server {\n\tgrpcServer := grpc.NewServer()\n\tserver := \u0026server{}\n\tproto.RegisterTestServiceServer(grpcServer, server)\n\treflection.Register(grpcServer)\n\n\treturn grpcServer\n}\n```\n\nIt will just answer with a `TestResponse` with a `message` field set to `Hello world!`.\n\n## Execution\n\nThe server can be launched with `go run ./cmd/test-grpc-gateway/`. It will listen to both HTTP and gRPC on `:8080` and `:9090`.\n\n### HTTP Request\nIf you perform an HTTP request to `/test`, you will get the response:\n\n```\n$ http localhost:8080/test\nHTTP/1.1 200 OK\nContent-Length: 26\nContent-Type: application/json\nDate: Fri, 12 Aug 2022 10:33:30 GMT\nGrpc-Metadata-Content-Type: application/grpc\n\n{\n    \"message\": \"Hello world!\"\n}\n```\n\n### gRPC Request\nThere is a gRPC client implementation in `cmd/grpc-client/` that uses the autogenerated code in `pkg/application/proto` that\nwill contact the server on `:9090`.\n\n```go\nfunc main() {\n    conn, err := grpc.Dial(\"localhost:9090\", grpc.WithTransportCredentials(insecure.NewCredentials()))\n    if err != nil { ... }\n    defer conn.Close()\n    \n    client := proto.NewTestServiceClient(conn)\n\t\n    response, err := client.Test(context.Background(), \u0026emptypb.Empty{})\n    if err != nil { ... }\n    \n    log.Printf(\"%s\", response.Message)\n}\n```\n\nAfter executing it, with the server listening, you should see the following:\n```\n$ go run ./cmd/grpc-client/\n2022/08/12 10:33:30 Hello world!\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftembleking%2Ftest-grpc-gateway","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftembleking%2Ftest-grpc-gateway","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftembleking%2Ftest-grpc-gateway/lists"}