Skip to content

Instantly share code, notes, and snippets.

@f3nry
Created February 1, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save f3nry/9e8e9486f65816e31c61b72a6ff36100 to your computer and use it in GitHub Desktop.
Save f3nry/9e8e9486f65816e31c61b72a6ff36100 to your computer and use it in GitHub Desktop.
Third Attempt: Length-prefix Construction
const (
payloadLen = 1
sizeLen = 4
headerLen = payloadLen + sizeLen
)
func msgHeader(data []byte) (hdr []byte, payload []byte) {
hdr = make([]byte, headerLen)
hdr[0] = byte(uint8(0))
// Write length of payload into buf
binary.BigEndian.PutUint32(hdr[payloadLen:], uint32(len(data)))
return hdr, data
}
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
message := &api.AliveResponse{Message: "Hello, world."}
b, err := proto.Marshal(message)
if err != nil {
return events.APIGatewayProxyResponse{
StatusCode: 500,
}, err
}
hdr, data := msgHeader(b)
hdr = append(hdr, data...)
return events.APIGatewayProxyResponse{
Body: base64.StdEncoding.EncodeToString(hdr),
Headers: map[string]string{
"Content-Type": "application/grpc+proto",
"grpc-status": "0",
},
IsBase64Encoded: true,
StatusCode: 200,
}, nil
}
@rakneo
Copy link

rakneo commented Aug 29, 2020

Hey, Could you please share a boilerplate code for grpc based serverless applications

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment