https://github.com/iamatypeofwalrus/batch
Multipart/Batch http handler for Go
https://github.com/iamatypeofwalrus/batch
batching go golang multipart
Last synced: 3 months ago
JSON representation
Multipart/Batch http handler for Go
- Host: GitHub
- URL: https://github.com/iamatypeofwalrus/batch
- Owner: iamatypeofwalrus
- License: mit
- Created: 2018-05-23T03:03:50.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2019-11-10T01:22:55.000Z (over 6 years ago)
- Last Synced: 2024-06-20T03:48:34.530Z (almost 2 years ago)
- Topics: batching, go, golang, multipart
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 2
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://codebuild.us-west-2.amazonaws.com/badges?uuid=eyJlbmNyeXB0ZWREYXRhIjoicWxUemVobEZQZW56S1hQckxFOE1UaGtUeGk4cjZoNWt4V05EbmZ6SkkvS1NzSG9zbStsampjZy9PYngzenlBN1lvb2ZhUWlzbE5jNW5EcThsbjdaWHJzPSIsIml2UGFyYW1ldGVyU3BlYyI6IlVDL0pqNlYzZUsvUFJGT0wiLCJtYXRlcmlhbFNldFNlcmlhbCI6MX0%3D&branch=master)
[](https://godoc.org/github.com/iamatypeofwalrus/batch)
[](https://goreportcard.com/report/github.com/iamatypeofwalrus/batch)
# Batch
Batch is a simple Go library for handling multipart batch requests. It adheres to the [HTTP Multipart Batched Request Format draft spec](https://tools.ietf.org/id/draft-snell-http-batch-00.html).
## Note
The draft spec is not well specified when it comes to whether or not upstream requests use `http` or `https`. The `http.Client` expects either when it makes a request.
By default all requests made by Batch use `http`. However, individual requests can set the `x-use-https` header in the Multipart/Batch message to use `https`.
Here's a complete example of a Batch request where the batch message uses the `x-use-https` header.
```
POST / HTTP/1.1
Host: example.org
Content-Type: multipart/batch; type="application/http;version=1.1" boundary=batch
Mime-Version: 1.0
--batch
Content-Type: application/http;version=1.1
Content-Transfer-Encoding: binary
Content-ID:
x-use-https: true
POST /example/application HTTP/1.1
Host: example.org
Content-Type: text/plain
Content-Length: 3
Foo
--batch--
```
## Examples
### Simple
Batch adheres to the `http.Handler` interface and it can be provided directly to `http.ListenAndServe`.
```go
package main
import (
"net/http"
"github.com/iamatypeofwalrus/batch"
)
func main() {
b := batch.New()
http.ListenAndServe(":8080", b)
}
```
### Custom
You can customize the HTTP Client that Batch uses to perform a single request by providing it with any struct that adheres to the `batch.HTTPClient` interface. The `http.Client` adheres to this interface.
You can provide Batch with a `batch.Logger` in order to capture any error messages that occur when processing requests. `log.Logger` adheres to this interface.
```go
package main
import (
"log"
"net/http"
"os"
"github.com/iamatypeofwalrus/batch"
)
func main() {
l := log.New(os.Stdout, "", log.LstdFlags)
b := &batch.Batch{
Log: l,
Client: http.DefaultClient,
}
http.HandleFunc("/batch", b.ServeHTTP)
log.Println("listening on :8080")
http.ListenAndServe(":8080", nil)
}
```