Skip to content

Commit

Permalink
HTTPBody writer and readers (#106)
Browse files Browse the repository at this point in the history
* HTTPBody writer and readers

* Update README.md for large files
  • Loading branch information
emcfarlane authored May 17, 2023
1 parent 2815fcc commit 91250e0
Show file tree
Hide file tree
Showing 5 changed files with 450 additions and 236 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ Send any content type with the protobuf type `google.api.HttpBody`.
Request bodies are unmarshalled from the body with the `ContentType` header.
Response bodies marshal to the body and set the `ContentType` header.
For large requests streaming RPCs support chunking the file into multiple messages.
This can be used as a way to document content APIs.
```protobuf
import "google/api/httpbody.proto";
Expand Down Expand Up @@ -212,6 +211,25 @@ message UploadFileRequest {
}
```
To better support stream uploads use the `AsHTTPBodyReader` and `AsHTTPBodyWriter` methods.
The returned `io.Reader` and `io.Writer` efficiently stream bytes for large messages.
This methods only works on streaming requests or streaming responses for gRPC-transcoding streams.
```go
// LargeUploadDownload echoes the request body as the response body with contentType.
func (s *asHTTPBodyServer) LargeUploadDownload(stream testpb.Files_LargeUploadDownloadServer) error {
var req testpb.UploadFileRequest
r, _ := larking.AsHTTPBodyReader(stream, &req)
log.Printf("got %s!", req.Filename)

rsp := httpbody.HttpBody{
ContentType: req.File.GetContentType(),
}
w, _ := larking.AsHTTPBodyWriter(stream, &rsp)

_, err := io.Copy(w, r)
return err
}
```
#### Websockets Annotations
Annotate a custom method kind `websocket` to enable clients to upgrade connections. This enables streams to be bidirectional over a websocket connection.
Expand Down
2 changes: 1 addition & 1 deletion larking/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func (m *Mux) serveGRPC(w http.ResponseWriter, r *http.Request) {
messageEncoding: messageEncoding,
//rHeader: r.Header,
}
// Sync stream return on stream methods.
// Sync handler return to stream methods.
defer func() {
cancel()
stream.wg.Wait()
Expand Down
Loading

0 comments on commit 91250e0

Please sign in to comment.