{"id":301699,"date":"2020-04-12T03:00:14","date_gmt":"2020-04-12T03:00:14","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=301699"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=301699","title":{"rendered":"\u041f\u043e\u043b\u043d\u044b\u0439 \u043d\u0430\u0431\u043e\u0440 gRPC, RESTful JSON API \u0438 Swagger \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e .proto \u0444\u0430\u0439\u043b\u0430. \u041e\u0442 \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0434\u043e \u043d\u044e\u0430\u043d\u0441\u043e\u0432 \u0438 \u0442\u043e\u043d\u043a\u043e\u0441\u0442\u0435\u0439 grpc-gateway"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/496574\/\">\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 gRPC \u0438 RESTful JSON API \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 Swagger \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043a \u043d\u0435\u043c\u0443. <\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 <a href=\"https:\/\/habr.com\/ru\/post\/496098\/#grpc-gateway\">\u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 API-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 Golang \u0441 \u0430\u0432\u0442\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u0434\u0430 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>. \u0422\u0430\u043c \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435.<\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/grpc-ecosystem.github.io\/grpc-gateway\/\" rel=\"nofollow\">grpc-gateway<\/a> \u2014 \u044d\u0442\u043e \u043f\u043b\u0430\u0433\u0438\u043d <a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\" rel=\"nofollow\">protoc<\/a>. \u041e\u043d \u0447\u0438\u0442\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 gRPC \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u043a\u0441\u0438-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 RESTful JSON API \u0432 gRPC. \u042d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 gRPC.<\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/v7\/an\/ua\/v7anuaz1tvdaaptesd2tg_w_57q.png\"><\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h1 id=\"ustanovka\">\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430<\/h1>\n<p>  <\/p>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c <a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\/releases\" rel=\"nofollow\">protoc<\/a>.<\/p>\n<p>  <\/p>\n<p>\u0418 \u0435\u0449\u0435 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f 3 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430 Go <code>protoc-gen-go<\/code>, <code>protoc-gen-swagger<\/code>, <code>protoc-gen-grpc-gateway<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u043a\u0430\u043a \u043c\u044b \u0432\u0441\u0435 \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u043f\u0435\u0440\u0435\u0448\u043b\u0438 \u043d\u0430 \u043c\u043e\u0434\u0443\u043b\u0438, \u0442\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 <a href=\"https:\/\/github.com\/golang\/go\/wiki\/Modules#how-can-i-track-tool-dependencies-for-a-module\" rel=\"nofollow\">\u0437\u0430\u0444\u0438\u043a\u0441\u0438\u0440\u0443\u0435\u043c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438<\/a> \u043f\u043e \u044d\u0442\u043e\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438.<\/p>\n<p>  <\/p>\n<p>\u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b\u0438\u043a <code>tools.go<\/code> \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0435\u0433\u043e \u0432 \u043d\u0430\u0448 \u043c\u043e\u0434\u0443\u043b\u044c.<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">\/\/ +build tools  package tools  import (     _ &quot;github.com\/grpc-ecosystem\/grpc-gateway\/protoc-gen-grpc-gateway&quot;     _ &quot;github.com\/grpc-ecosystem\/grpc-gateway\/protoc-gen-swagger&quot;     _ &quot;github.com\/golang\/protobuf\/protoc-gen-go&quot; )<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0437\u043e\u0432\u0435\u043c <code>go mod tidy<\/code> \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u043d\u0443\u0436\u043d\u044b\u0445 \u0432\u0435\u0440\u0441\u0438\u0439 \u043f\u0430\u043a\u0435\u0442\u043e\u0432. \u0418 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u043c \u0438\u0445:<\/p>\n<p>  <\/p>\n<pre><code class=\"bash\">$ go install \\     github.com\/grpc-ecosystem\/grpc-gateway\/protoc-gen-grpc-gateway \\     github.com\/grpc-ecosystem\/grpc-gateway\/protoc-gen-swagger \\     github.com\/golang\/protobuf\/protoc-gen-go<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u0443 \u043d\u0430\u0441 \u0432\u0435\u0441\u044c \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 \u0433\u043e\u0442\u043e\u0432, \u043c\u043e\u0436\u043d\u043e \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<h1 id=\"opisanie-interfeysa\">\u041e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430<\/h1>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0443\u0436\u0435 \u043f\u0438\u0441\u0430\u043b\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u044b \u0434\u043b\u044f gRPC, \u0442\u043e \u044d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0432\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c\u0441\u044f \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u043c. \u0415\u0441\u043b\u0438 \u0436\u0435 \u043d\u0435\u0442, \u0442\u043e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/grpc.io\/docs\/quickstart\/go\/\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">syntax = &quot;proto3&quot;; package api_pb; message AddressRequest {     string address = 1;     uint64 height = 2; } message AddressResponse {     map&lt;string, string&gt; balance = 1;     string transactions_count = 2; }  service BlockchainService {     rpc Address (AddressRequest) returns (AddressResponse); }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u0433\u043e \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438 <a href=\"https:\/\/github.com\/googleapis\/googleapis\/blob\/master\/google\/api\/http.proto#L46\" rel=\"nofollow\"><code>google.api.http<\/code><\/a>.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">syntax = &quot;proto3&quot;; package api_pb; import &quot;google\/api\/annotations.proto&quot;;  message AddressRequest {     string address = 1;     uint64 height = 2; } message AddressResponse {     map&lt;string, string&gt; balance = 1;     string transactions_count = 2; }  service BlockchainService {     rpc Address (AddressRequest) returns (AddressResponse) {         option (google.api.http) = {             get: &quot;\/address\/{address}&quot;         };     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0442\u0430\u043a \u0436\u0435 web-socket endpoint.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">syntax = &quot;proto3&quot;; package api_pb; import &quot;google\/api\/annotations.proto&quot;; import &quot;google\/protobuf\/struct.proto&quot;;  message AddressRequest {     string address = 1;     uint64 height = 2; } message AddressResponse {     map&lt;string, string&gt; balance = 1;     string transactions_count = 2; } message SubscribeRequest {     string query = 1; } message SubscribeResponse {     string query = 1;     google.protobuf.Struct data = 2;     message Event {         string key = 1;         repeated string events = 2;     }     repeated Event events = 3; }  service BlockchainService {     rpc Address (AddressRequest) returns (AddressResponse) {         option (google.api.http) = {             get: &quot;\/address\/{address}&quot;         };     }     rpc Subscribe (SubscribeRequest) returns (stream SubscribeResponse) {         option (google.api.http) = {             get: &quot;\/subscribe&quot;         };     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u042f \u043f\u0440\u0435\u0434\u043f\u043e\u0447\u0438\u0442\u0430\u044e \u0441\u043e\u0437\u0434\u0430\u0442\u044c Makefile \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043a\u043e\u043c\u0430\u043d\u0434 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u0438 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0430\u043f\u043a\u0443 \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">all:         mkdir -p &quot;api_pb&quot;         protoc -I\/usr\/local\/include -I. \\             -I${GOPATH}\/src \\             -I${GOPATH}\/src\/github.com\/grpc-ecosystem\/grpc-gateway\/third_party\/googleapis \\             -I${GOPATH}\/src\/github.com\/grpc-ecosystem\/grpc-gateway \\             --grpc-gateway_out=logtostderr=true:.\/api_pb \\             --swagger_out=allow_merge=true,merge_file_name=api:. \\             --go_out=plugins=grpc:.\/api_pb .\/*.proto<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u0438\u0445 \u0438\u0437 <code>gen.go<\/code> \u0444\u0430\u0439\u043b\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">\/\/go:generate make  package grpc_gateway_example<\/code><\/pre>\n<p>  <\/p>\n<p>\u0423 \u043d\u0430\u0441 \u043f\u043e\u044f\u0432\u0438\u043b\u0438\u0441\u044c 3 \u043d\u043e\u0432\u044b\u0445 \u0444\u0430\u0439\u043b\u0430 <code>.\/api_pb\/api.go<\/code>, <code>.\/api_pb\/api.gw.go<\/code> \u0438 <code>.\/api.swager.json<\/code>. <\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tp\/0z\/zg\/tp0zzgxbhtcaorum_nkrk8mwp_m.png\"><\/p>\n<p>  <\/p>\n<p>\u0410 \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u043d\u0438\u043c\u0438 \u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u043c \u043d\u0430\u0434\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c:<\/p>\n<p>  <\/p>\n<pre><code class=\"go\">\/\/ BlockchainServiceServer is the server API for BlockchainService service. type BlockchainServiceServer interface {     Address(context.Context, *AddressRequest) (*AddressResponse, error)     Subscribe(*SubscribeRequest, BlockchainService_SubscribeServer) error }<\/code><\/pre>\n<p>  <\/p>\n<h2 id=\"vstroennye-tipy\">\u0412\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b<\/h2>\n<p>  <\/p>\n<p>\u042f \u0431\u044b \u0445\u043e\u0442\u0435\u043b \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u0445 \u0442\u0438\u043f\u0430\u0445 protobuf. \u0422\u0438\u043f <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/reference\/google.protobuf#struct\" rel=\"nofollow\"><code>google.protobuf.Struct<\/code><\/a> \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 JSON. \u041b\u044e\u0431\u043e\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 <code>proto3<\/code> \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043c\u0435\u0445\u0430\u043d\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u043e \u0432 JSON \u0438 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u0432 \u043f\u043e\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u0442\u0438\u043f\u0430. \u042d\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0433\u0438\u0431\u043a\u0438\u0439 \u0442\u0438\u043f \u0438 \u0434\u0430\u0435\u0442 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432\u0430 \u0434\u0438\u043d\u0430\u043c\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0442\u0438\u043f\u0438\u0437\u0430\u0446\u0438\u0438 \u0434\u043b\u044f <code>protobuf<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0422\u0438\u043f <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto3#any\" rel=\"nofollow\"><code>google.protobuf.Any<\/code><\/a> \u0432\u0441\u0442\u0440\u0430\u0438\u0432\u0430\u0435\u0442 \u0434\u0432\u043e\u0438\u0447\u043d\u044b\u0439 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0439 <code>protobuf<\/code> \u0432\u043c\u0435\u0441\u0442\u0435 \u0441 \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0435\u0439 \u043e \u0442\u0438\u043f\u0435 \u0432 \u043f\u043e\u043b\u0435 \u0434\u0440\u0443\u0433\u043e\u0433\u043e protobuf. \u0412\u043d\u0443\u0442\u0440\u0438 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u0431\u0430\u0439\u0442\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0441\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0435\u0439 \u043f\u0440\u043e\u0442\u043e\u043a\u043e\u043b\u044c\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u0438 \u0441\u0442\u0440\u043e\u043a\u043e\u0439, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0435\u0439 \u0442\u0438\u043f URL. URL-\u0430\u0434\u0440\u0435\u0441 \u0442\u0438\u043f\u0430 \u2014 \u044d\u0442\u043e, \u043f\u043e \u0441\u0443\u0442\u0438, \u0441\u0442\u0440\u043e\u043a\u0430, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0430\u044f \u0438\u043c\u044f \u0442\u0438\u043f\u0430 \u0432 \u0444\u043e\u0440\u043c\u0435 <code>type.googleapis.com\/packagename.messagename<\/code>.<\/p>\n<p>  <\/p>\n<p>\u0425\u043e\u0442\u044f \u044d\u0442\u0438 \u0442\u0438\u043f\u044b \u043f\u043e\u0445\u043e\u0436\u0438, \u043e\u043d\u0438 \u0438\u043c\u0435\u044e\u0442 <a href=\"https:\/\/blog.envoyproxy.io\/dynamic-extensibility-and-protocol-buffers-dcd0bf0b8801\" rel=\"nofollow\">\u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0430\u0437\u043b\u0438\u0447\u0438\u044f<\/a>.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u0442\u0438\u043f\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c <a href=\"https:\/\/developers.google.com\/protocol-buffers\/docs\/proto3#simple\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>. <\/p>\n<p>  <\/p>\n<h1 id=\"inicializaciya-servera\">\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/h1>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 BlockchainServiceServer<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"go\">package service  import (     &quot;bytes&quot;     &quot;context&quot;     &quot;encoding\/json&quot;     &quot;github.com\/golang\/protobuf\/jsonpb&quot;     _struct &quot;github.com\/golang\/protobuf\/ptypes\/struct&quot;     &quot;github.com\/klim0v\/grpc-gateway-example\/api_pb&quot;     &quot;google.golang.org\/grpc\/codes&quot;     &quot;google.golang.org\/grpc\/status&quot;     &quot;time&quot; )  type BlockchainServer struct {     eventBus &lt;-chan interface{} }  func NewBlockchainServer(eventBus &lt;-chan interface{}) *BlockchainServer {     return &amp;BlockchainServer{eventBus: eventBus} }  func (b *BlockchainServer) Address(_ context.Context, req *api_pb.AddressRequest) (*api_pb.AddressResponse, error) {     if req.Address != &quot;Mxb9a117e772a965a3fddddf83398fd8d71bf57ff6&quot; {         return &amp;api_pb.AddressResponse{}, status.Error(codes.FailedPrecondition, &quot;wallet not found&quot;)     }     return &amp;api_pb.AddressResponse{         Balance: map[string]string{             &quot;BIP&quot;: &quot;12345678987654321&quot;,         },         TransactionsCount: &quot;120&quot;,     }, nil }  func (b *BlockchainServer) Subscribe(req *api_pb.SubscribeRequest, stream api_pb.BlockchainService_SubscribeServer) error {     for {         select {         case &lt;-stream.Context().Done():             return stream.Context().Err()         case event := &lt;-b.eventBus:             byteData, err := json.Marshal(event)             if err != nil {                 return err             }             var bb bytes.Buffer             bb.Write(byteData)             data := &amp;_struct.Struct{Fields: make(map[string]*_struct.Value)}             if err := (&amp;jsonpb.Unmarshaler{}).Unmarshal(&amp;bb, data); err != nil {                 return err             }              if err := stream.Send(&amp;api_pb.SubscribeResponse{                 Query: req.Query,                 Data:  data,                 Events: []*api_pb.SubscribeResponse_Event{                     {                         Key:    &quot;tx.hash&quot;,                         Events: []string{&quot;01EFD8EEF507A5BFC4A7D57ECA6F61B96B7CDFF559698639A6733D25E2553539&quot;},                     },                 },             }); err != nil {                 return err             }         case &lt;-time.After(5 * time.Second):             return nil         }     } }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u041f\u0440\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435, \u0444\u043e\u0440\u043c\u0430\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0438 \u043a\u043e\u0434\u044b \u043e\u0448\u0438\u0431\u043e\u043a <a href=\"https:\/\/cloud.google.com\/apis\/design\/errors\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>, <a href=\"https:\/\/cloud.google.com\/service-infrastructure\/docs\/service-management\/reference\/rpc\/google.rpc#google.rpc.Code\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/github.com\/googleapis\/googleapis\/blob\/master\/google\/rpc\/code.proto\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041f\u0440\u0438\u043c\u0435\u0440 main.go<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"go\">package main  import (     &quot;context&quot;     &quot;flag&quot;     &quot;github.com\/golang\/glog&quot;     grpc_prometheus &quot;github.com\/grpc-ecosystem\/go-grpc-prometheus&quot;     &quot;github.com\/grpc-ecosystem\/grpc-gateway\/runtime&quot;     gw &quot;github.com\/klim0v\/grpc-gateway-example\/api_pb&quot;     &quot;github.com\/klim0v\/grpc-gateway-example\/service&quot;     &quot;github.com\/tmc\/grpc-websocket-proxy\/wsproxy&quot;     &quot;golang.org\/x\/sync\/errgroup&quot;     &quot;google.golang.org\/grpc&quot;     &quot;net&quot;     &quot;net\/http&quot;     &quot;time&quot; )  func run() error {     ctx := context.Background()     ctx, cancel := context.WithCancel(ctx)     defer cancel()      lis, err := net.Listen(&quot;tcp&quot;, &quot;:8842&quot;)     if err != nil {         return err     }      grpcServer := grpc.NewServer(         grpc.StreamInterceptor(grpc_prometheus.StreamServerInterceptor),         grpc.UnaryInterceptor(grpc_prometheus.UnaryServerInterceptor),     )     eventBus := make(chan interface{})     gw.RegisterBlockchainServiceServer(grpcServer, service.NewBlockchainServer(eventBus))     grpc_prometheus.Register(grpcServer)      var group errgroup.Group      group.Go(func() error {         return grpcServer.Serve(lis)     })      mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &amp;runtime.JSONPb{OrigName: true, EmitDefaults: true}))     opts := []grpc.DialOption{         grpc.WithInsecure(),         grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(50000000)),     }      group.Go(func() error {         return gw.RegisterBlockchainServiceHandlerFromEndpoint(ctx, mux, &quot;:8842&quot;, opts)     })     group.Go(func() error {         return http.ListenAndServe(&quot;:8843&quot;, wsproxy.WebsocketProxy(mux))     })         group.Go(func() error {         return http.ListenAndServe(&quot;:2662&quot;, promhttp.Handler())     })     group.Go(func() error {         for i := 0; i &lt; 100; i++ {             eventBus &lt;- struct {                 Type             byte                 Coin             string                 Value            int                 TransactionCount int                 Timestamp        time.Time             }{                 Type:             1,                 Coin:             &quot;BIP&quot;,                 TransactionCount: i,                 Timestamp:        time.Now(),             }         }         return nil     })      return group.Wait() }  func main() {     flag.Parse()     defer glog.Flush()      if err := run(); err != nil {         glog.Fatal(err)     } }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0417\u0434\u0435\u0441\u044c \u044f \u043f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043b <code>jsonpb.Marshaler<\/code> \u0441 \u0435\u0433\u043e \u043f\u043e\u043b\u044f\u043c\u0438:<\/p>\n<p>  <\/p>\n<ul>\n<li><code>EmitDefaults: true<\/code> \u2014 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e\u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e, \u0442\u0430\u043a\u0438\u0445 \u043a\u0430\u043a <code>0<\/code> \u0434\u043b\u044f <code>int<\/code>, <code>&quot;&quot;<\/code> \u0434\u043b\u044f <code>string<\/code>;<\/li>\n<li><code>EnumsAsInts: true<\/code> \u2014 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 <code>enum<\/code> \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u043e \u0438\u0445 \u0441\u0442\u0440\u043e\u043a\u043e\u0432\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e, \u0430 \u043d\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0443;<\/li>\n<li><code>OrigName: true<\/code> \u2014 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u0430 \u0438\u043c\u0435\u043d \u043f\u043e\u043b\u0435\u0439 \u0432 json&#8217;\u0435, \u043f\u043e \u0438\u0445 \u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u044e \u0432 <code>.proto<\/code> \u0444\u0430\u0439\u043b\u0435.<\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u0423\u0432\u0435\u043b\u0438\u0447\u0438\u043b <strong>\u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0430\u0437\u043c\u0435\u0440 \u043e\u0442\u0432\u0435\u0442\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430<\/strong> (\u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e) grpc.MaxCallRecvMsgSize(50000000).<\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u043e\u0431\u043d\u0430\u0436\u0438\u0442\u044c <strong>\u043f\u043e\u0442\u043e\u043a\u043e\u0432\u044b\u0435 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0435 \u0442\u043e\u0447\u043a\u0438 web-sokets<\/strong>, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c handler \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 wsproxy.WebsocketProxy(mux). <a href=\"https:\/\/github.com\/tmc\/grpc-websocket-proxy\" rel=\"nofollow\">wsproxy<\/a> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 json-\u043a\u043e\u0434\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u0441 \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u0442\u0435\u043b\u044f\u043c\u0438 \u0441\u0442\u0440\u043e\u043a.<\/p>\n<p>  <\/p>\n<p>\u0414\u043b\u044f <strong><a href=\"https:\/\/github.com\/grpc-ecosystem\/go-grpc-prometheus\" rel=\"nofollow\">prometheus \u043c\u0435\u0442\u0440\u0438\u043a\u0438<\/a> \u0438 <a href=\"https:\/\/github.com\/grpc-ecosystem\/go-grpc-middleware\" rel=\"nofollow\">middleware<\/a><\/strong> \u0435\u0441\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0435 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0435 <a href=\"https:\/\/github.com\/grpc-ecosystem\" rel=\"nofollow\">github<\/a>.<\/p>\n<p>  <\/p>\n<h1 id=\"sovety-po-napisaniyu-protobuf\">\u0421\u043e\u0432\u0435\u0442\u044b \u043f\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e <code>protobuf<\/code><\/h1>\n<p>  <\/p>\n<p><strong>\u041f\u0435\u0440\u0435\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0438\u043c\u0435\u043d\u0430 \u043f\u043e\u043b\u0435\u0439 JSON<\/strong> \u043c\u043e\u0436\u043d\u043e \u0432 <code>.proto<\/code> \u0444\u0430\u0439\u043b\u0435.<br \/>  \u0413\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440 Swagger \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <code>grpc-gateway<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0432\u0435\u0440\u0431\u043b\u044e\u0436\u0438\u0439 \u0440\u0435\u0433\u0438\u0441\u0442\u0440 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e \u043f\u0440\u0438 \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0438 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0439 Swagger. \u0415\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e snake_case, \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u044e \u043f\u043e\u043b\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0433\u043e <code>json_name<\/code> \u0432 <code>protobuf<\/code> \u043d\u0430 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u0435 \u0438\u043c\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">message AwesomeName {     uint32 id = 1;     string awesome_name = 2 [json_name = &quot;awesome_name&quot;]; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u041c\u043e\u0436\u043d\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043f\u043e\u043b\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f<\/strong>. \u0415\u0441\u0442\u044c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044f, \u0442\u0430\u043a\u0438\u0435 \u043a\u0430\u043a \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0440\u0435\u0441\u0443\u0440\u0441\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043c\u044b\u0441\u043b\u0430 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c. \u041f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0439 <code>\/\/ Output only.<\/code> \u0432 \u043f\u043e\u043b\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f <code>protobuf<\/code> \u043f\u043e\u043c\u0435\u0442\u0438\u0442 \u043f\u043e\u043b\u0435 \u043a\u0430\u043a \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">message AwesomeName {     \/\/ Output only.     uint32 id = 1;     string awesome_name = 2; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0412\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043f\u0443\u0442\u0438 URL \u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 <code>protobuf<\/code><\/strong><br \/>  \u042d\u0442\u043e \u043f\u043e\u043b\u0435\u0437\u043d\u043e, \u0435\u0441\u043b\u0438 \u0432\u044b \u0445\u043e\u0442\u0438\u0442\u0435, \u0447\u0442\u043e\u0431\u044b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0432\u0430\u0448\u0438\u0445 \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u043e\u0432 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0439 \u0431\u044b\u043b\u0438 \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u044b\u043c\u0438. \u0412 \u0442\u0438\u043f\u0438\u0447\u043d\u043e\u0439 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 REST \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432 \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0440\u0435\u0441\u0443\u0440\u0441\u0430 \u0431\u044b\u043b \u043f\u0435\u0440\u0435\u0434\u0430\u043d \u0432 URL. <\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">service AwesomeService {     rpc UpdateAppointment (UpdateAwesomeNameRequest) returns (AwesomeName) {         option (google.api.http) = {             put: &quot;\/v1\/awesome-name\/{awesome_name.id}&quot;             body: &quot;awesome_name&quot;         };     }; } message UpdateAwesomeNameRequest {     AwesomeName awesome_name = 1; }<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0424\u0438\u043b\u044c\u0442\u0440 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u0434\u043b\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f<\/strong>. \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0431\u0443\u0434\u0435\u0442 \u0442\u0440\u0443\u0434\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c, \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0443\u0441\u0442\u044b\u043c \u0438\u043b\u0438 \u043d\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043e \u0432 \u0437\u0430\u043f\u0440\u043e\u0441\u0435, <code>grpc-gateway<\/code> \u0441\u0430\u043c \u043c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0430\u0442\u044c \u044d\u0442\u0443 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 protobuf \u0434\u043e\u043b\u0436\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043f\u043e\u043b\u0435 <code>update_mask<\/code> \u0441 \u0442\u0438\u043f\u043e\u043c, \u0430 \u0437\u0430\u043f\u0440\u043e\u0441 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u043c <code>PATCH<\/code>. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 <a href=\"https:\/\/pkg.go.dev\/google.golang.org\/genproto\/protobuf\/field_mask?tab=doc#FieldMask\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a> \u0438 <a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\/blob\/master\/src\/google\/protobuf\/field_mask.proto\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a>.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">message UpdateAwesomeNameRequest {     AwesomeName awesome_name = 1;     google.protobuf.FieldMask update_mask = 2; \/\/ This field will be automatically populated by grpc-gateway. }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c <strong>\u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e HTTP-\u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0434\u043b\u044f \u043e\u0434\u043d\u043e\u0433\u043e RPC<\/strong>, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f \u043e\u043f\u0446\u0438\u044e <code>additional_bindings<\/code>. \u042d\u0442\u043e\u0442 \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f endpoint&#8217;\u043e\u0432 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway\/blob\/master\/third_party\/googleapis\/google\/api\/http.proto\" rel=\"nofollow\">\u0437\u0434\u0435\u0441\u044c<\/a><\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e \u0431\u044b <strong>\u043e\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b Swagger<\/strong> \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u043e\u043f\u0438\u0448\u0435\u043c <code>option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger)<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"plaintext\">syntax = &quot;proto3&quot;; import &quot;protoc-gen-swagger\/options\/annotations.proto&quot;; package awesome.service; option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = {     info: {         title: &quot;My Habr Example Service&quot;         version: &quot;1.0&quot;         contact: {             name: &quot;Klimov Sergey&quot;             url: &quot;https:\/\/github.com\/klim0v&quot;             email: &quot;klim0v-sergey@yandex.ru&quot;         };     };     schemes: [HTTP,HTTPS]     consumes: &quot;application\/json&quot;     produces: &quot;application\/json&quot;     responses: {         key: &quot;404&quot;         value: {             description: &quot;Returned when the resource does not exist.&quot;             schema: {                 json_schema: {                     type: STRING                 };             };         };     }; };<\/code><\/pre>\n<p>  <\/p>\n<p><strong>\u0411\u043e\u043b\u044c\u0448\u0435 \u0434\u0435\u0442\u0430\u043b\u0435\u0439 \u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439<\/strong> \u0432\u044b \u043d\u0430\u0439\u0434\u0435\u0442\u0435 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 <a href=\"https:\/\/github.com\/grpc-ecosystem\/grpc-gateway#features\" rel=\"nofollow\">Features<\/a><\/p>\n<p>  <\/p>\n<h1 id=\"vyvod\">\u0412\u044b\u0432\u043e\u0434<\/h1>\n<p>  <\/p>\n<p>\u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c grpc-gateway \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 HTTP \u043f\u0440\u043e\u043a\u0441\u0438 \u043a gRPC \u0438 \u0444\u0430\u0439\u043b\u044b swagger \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438 \u2014 \u044d\u0442\u043e \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043c\u043e\u0433\u0430\u0435\u0442 \u0431\u044b\u0441\u0442\u0440\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0438 \u043a\u0440\u0430\u0441\u0438\u0432\u0443\u044e \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e, \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. <\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0447\u0438\u0442\u0430\u0432 \u044d\u0442\u043e\u0442 \u043f\u043e\u0441\u0442, \u044f \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0447\u0442\u043e \u0432\u0430\u043c \u0431\u0443\u0434\u0435\u0442 \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439. \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0438 \u0438\u043b\u0438 \u043f\u0440\u0435\u0434\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u043f\u0438\u0448\u0438\u0442\u0435 \u0432 \u0440\u0430\u0437\u0434\u0435\u043b\u0435 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u0435\u0432.<\/p>\n<p>  <\/p>\n<p>P.S. \u0412\u0441\u0435 \u0444\u0430\u0439\u043b\u044b \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438 <a href=\"https:\/\/github.com\/klim0v\/grpc-gateway-example\" rel=\"nofollow\">https:\/\/github.com\/klim0v\/grpc-gateway-example<\/a><\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/496574\/\"> https:\/\/habr.com\/ru\/post\/496574\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/post\/496574\/\">\n<p>\u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u043e\u043f\u0438\u0448\u0443 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 gRPC \u0438 RESTful JSON API \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e \u0438 Swagger \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e \u043a \u043d\u0435\u043c\u0443. <\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u0430 \u0441\u0442\u0430\u0442\u044c\u044f \u2014 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0440\u0430\u0437\u0431\u043e\u0440\u0430 \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0445 <a href=\"https:\/\/habr.com\/ru\/post\/496098\/#grpc-gateway\">\u0441\u043f\u043e\u0441\u043e\u0431\u043e\u0432 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0439 API-\u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u043d\u0430 Golang \u0441 \u0430\u0432\u0442\u043e\u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u043a\u043e\u0434\u0430 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>. \u0422\u0430\u043c \u044f \u043e\u0431\u0435\u0449\u0430\u043b \u0431\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u044d\u0442\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435.<\/p>\n<p>  <\/p>\n<p><a href=\"https:\/\/grpc-ecosystem.github.io\/grpc-gateway\/\" rel=\"nofollow\">grpc-gateway<\/a> \u2014 \u044d\u0442\u043e \u043f\u043b\u0430\u0433\u0438\u043d <a href=\"https:\/\/github.com\/protocolbuffers\/protobuf\" rel=\"nofollow\">protoc<\/a>. \u041e\u043d \u0447\u0438\u0442\u0430\u0435\u0442 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u0441\u0435\u0440\u0432\u0438\u0441\u0430 gRPC \u0438 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u0442 \u043e\u0431\u0440\u0430\u0442\u043d\u044b\u0439 \u043f\u0440\u043e\u043a\u0441\u0438-\u0441\u0435\u0440\u0432\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 RESTful JSON API \u0432 gRPC. \u042d\u0442\u043e\u0442 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0438\u043c\u0438 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430\u043c\u0438 \u0432 \u0432\u0430\u0448\u0435\u043c \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u0438 gRPC.<\/p>\n<p>  <\/p>\n<p>\u042d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/v7\/an\/ua\/v7anuaz1tvdaaptesd2tg_w_57q.png\"><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-301699","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=301699"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/301699\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=301699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=301699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=301699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}