{"id":22118391,"url":"https://github.com/ghchinoy/openapi-from-grpc","last_synced_at":"2025-03-24T06:19:26.317Z","repository":{"id":231579966,"uuid":"772376742","full_name":"ghchinoy/openapi-from-grpc","owner":"ghchinoy","description":"gRPC service with autogenerated OpenAPI spec via gRPC Gateway for REST/HTTP","archived":false,"fork":false,"pushed_at":"2024-04-04T17:35:46.000Z","size":22,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-29T12:11:18.956Z","etag":null,"topics":["grpc-gateway","openapi"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ghchinoy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","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}},"created_at":"2024-03-15T04:19:58.000Z","updated_at":"2024-04-04T17:15:31.000Z","dependencies_parsed_at":"2024-04-04T18:53:51.026Z","dependency_job_id":null,"html_url":"https://github.com/ghchinoy/openapi-from-grpc","commit_stats":null,"previous_names":["ghchinoy/openapi-from-grpc"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghchinoy%2Fopenapi-from-grpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghchinoy%2Fopenapi-from-grpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghchinoy%2Fopenapi-from-grpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ghchinoy%2Fopenapi-from-grpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ghchinoy","download_url":"https://codeload.github.com/ghchinoy/openapi-from-grpc/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245217946,"owners_count":20579300,"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":["grpc-gateway","openapi"],"created_at":"2024-12-01T13:49:41.603Z","updated_at":"2025-03-24T06:19:26.297Z","avatar_url":"https://github.com/ghchinoy.png","language":"Go","readme":"# Create OpenAPI from gRPC Gateway Service\n\nThis is an example gRPC service that has two services, one a gRPC service, and another that's exposed via a gRPC Gateway as REST.\n\nThe REST exposure definition is used to then define an OpenAPI Spec with a protoc tool called [gnostic](https://github.com/google/gnostic).\n\n\n## Install all the tools\n\n```\ngo install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest\ngo install google.golang.org/protobuf/cmd/protoc-gen-go@latest\ngo install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest\ngo install github.com/google/gnostic/cmd/protoc-gen-openapi@latest\n```\n\n## Build the protos\n\nSince this uses the http googleapis protos, clone that into a dir and copy out some files.\n\n```\ngit clone git@github.com:googleapis/googleapis.git\nmkdir -p bookstore/proto/google/api\ncp googleapis/google/api/http.proto bookstore/proto/google/api\ncp googleapis/google/api/annotations.proto bookstore/proto/google/api\n```\n\nThen, generate the code from protos and generate the OpenAPI file.\n\nThis should create the `bookstore/pb` protobuf generated go code in from the protobuf definition `proto` dir and also create an openapi.yaml file.\n\n```\ncd bookstore\ngo mod tidy\n\nprotoc --proto_path=proto proto/*.proto  --go_out=. --go-grpc_out=. --openapi_out=.\n```\n\n## Test\n\nStart the server; it'll start both a gRPC server and a gRPC Gateway server.\n\n\n```\ngo run *.go\n2024/03/14 22:16:27 gRPC server started on port 8080\n2024/03/14 22:16:27 gRPC-gateway started on port 8090\n```\n\nThen in another terminal, let's make some calls.\n\nUsing [grpcurl](https://github.com/fullstorydev/grpcurl), make a call to the gRPC server endpoint that's only exposed via gRPC.\n\n\n```\n$ grpcurl -plaintext localhost:8080 bookstore.Inventory.GetBooks\n{\n  \"books\": [\n    {\n      \"title\": \"Book 1\",\n      \"author\": \"Author 1\",\n      \"pages\": 100\n    },\n    {\n      \"title\": \"Book 2\",\n      \"author\": \"Author 2\",\n      \"pages\": 200\n    }\n  ]\n}\n```\n\nNow, using curl, make a call to the gRPC Gateway endpoint that's exposed via REST.\n\n\n```\n$ curl localhost:8090/v1/echo -d '{\"value\":\"hello you big boy\"}'\n{\"value\":\"hello you big boy\"}\n\n```\n\nSince it's gRPC as well, you can also make a gRPC call to the gRPC Gateway endpoint.\n\n```\ngrpcurl -plaintext -d '{\"value\": \"hello there big boy\"}' localhost:8080 bookstore.Echo.Echo\n{\n  \"value\": \"hello there big boy\"\n}\n```\n\nYou can also do the same with [grpc_cli](https://github.com/grpc/grpc/blob/master/doc/command_line_tool.md)\n\n\n```\ngrpc_cli call localhost:8080 Echo \"value: 'hi there you repeat this'\"\nconnecting to localhost:8080\nvalue: \"hi there you repeat this\"\nRpc succeeded with OK status\n```\n\n\n## Thanks to\n\n* https://github.com/google/gnostic “A compiler for APIs described by the OpenAPI Specification with plugins for code generation and other API support tasks.”\n* https://github.com/google/gnostic/tree/main/cmd/protoc-gen-openapi “... protoc plugin that generates an OpenAPI description for a REST API that corresponds to a Protocol Buffer service.”\n* https://grpc.io/docs/languages/go/basics/\n* https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/introduction/\n* https://grpc-ecosystem.github.io/grpc-gateway/docs/tutorials/adding_annotations/#using-protoc\n* https://sahansera.dev/building-grpc-server-go/\n\n\n## License\n\nApache 2.0; see [`LICENSE`](LICENSE) for details.\n\n## Disclaimer\n\nThis project is not an official Google project. It is not supported by\nGoogle and Google specifically disclaims all warranties as to its quality,\nmerchantability, or fitness for a particular purpose.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghchinoy%2Fopenapi-from-grpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghchinoy%2Fopenapi-from-grpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghchinoy%2Fopenapi-from-grpc/lists"}