Skip to content

Commit 28932ee

Browse files
authored
Merge pull request #23 from Azure/toma/addMetadataReadme
add httpmw/metadata docs
2 parents bc5d55a + 7e20507 commit 28932ee

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

docs/images/grpc-gateway.png

96.2 KB
Loading

docs/images/request-lifecycle.png

68.3 KB
Loading

httpmw/metadata/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**gRPC-Gateway is a plugin that allows us to accept incoming REST requests and converts them to gRPC calls**
2+
- layer over the gRPC service that uses the api.proto file to generate an api.pb.gw.go file
3+
- This is the reverse proxy that is responsible for translating gRPC into RESTful JSON APIs
4+
5+
![grpc-gateway.png](/docs/images/grpc-gateway.png)
6+
7+
REST is still a very popular protocol used by many, and by allowing our gRPC service to accept REST requests, we are making our service more accessible. Especially with cases like ARM, which does not communicate using gRPC.
8+
9+
Using the gateway, we can also propagate http headers to our backend gRPC service by utilizing runtime serveMuxOptions. In our [aks-middleware](https://github.com/Azure/aks-middleware/blob/main/httpmw/metadata/metadata.go), we define a NewMetadataMiddleware which accepts an metadataToHeader map, headerToMetadata map, and returns an array of runtime.ServeMuxOptions. This contains two options:
10+
- runtime.WithMetadata
11+
- this loops through the headerToMetdata map, and checks if the headerName exists in the request headers. If it does, it creates a new metadata pair with the metadata key and value. This is how we are able to transform http request headers into gRPC metadata
12+
- runtime.WithOutgoingHeaderMatcher
13+
- When sending the gRPC response back to the mux, there may be certain metadata that we do not want to convert to a http header and send back as part of the response. This matcher uses the metadataToHeader map to ensure only the headers we allow will be returned in the response
14+
15+
These options can be easily imported from the aks-middleware and used within the gRPC-gateway mux like so:
16+
```go
17+
import aksMiddlewareMetadata "github.com/Azure/aks-middleware/httpmw/metadata"
18+
19+
metadataToHeader := map[string]string{
20+
"custom-header": "X-Custom-Header",
21+
"another-header": "X-Another-Header",
22+
}
23+
headerToMetadata := map[string]string{
24+
"X-Custom-Header": "custom-header",
25+
"X-Another-Header": "another-header",
26+
27+
}
28+
29+
gwmux := runtime.NewServeMux(
30+
aksMiddlewareMetadata.NewMetadataMiddleware(headerToMetadata, metadataToHeader)...,
31+
)
32+
```
33+
34+
In our middleware, we also have a [responseheader interceptor](https://github.com/Azure/aks-middleware/blob/main/responseheader/responseheader.go) that copies the desired incoming metadata to the gRPC response headers. We use this interceptor to send back selected metadata which is then converted to headers by the outgoing header matcher.
35+
36+
The following is a end-to-end request lifecycle diagram to show how http headers are processed:
37+
![updated diagram.png](/docs/images/request-lifecycle.png)
38+
39+

0 commit comments

Comments
 (0)