Created
February 1, 2019 01:17
-
-
Save f3nry/9e8e9486f65816e31c61b72a6ff36100 to your computer and use it in GitHub Desktop.
Third Attempt: Length-prefix Construction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey, Could you please share a boilerplate code for grpc based serverless applications