https://github.com/jobala/middleware_pipeline
Middleware Pipeline for the Go HTTP Client
https://github.com/jobala/middleware_pipeline
Last synced: about 1 month ago
JSON representation
Middleware Pipeline for the Go HTTP Client
- Host: GitHub
- URL: https://github.com/jobala/middleware_pipeline
- Owner: jobala
- License: mit
- Created: 2021-01-10T12:10:15.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2021-01-23T08:09:44.000Z (over 5 years ago)
- Last Synced: 2023-07-28T12:56:59.739Z (almost 3 years ago)
- Language: Go
- Size: 7.81 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Middleware Pipeline
Middleware Pipeline for the Go HTTP Client.
## Getting Started
Create a middleware
```go
type AuthorizationMiddleware struct{}
func (s AuthorizationMiddleware) Intercept(pipeline pipeline.Pipeline, req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer token")
body, _ := httputil.DumpRequest(req, true)
log.Println(fmt.Sprintf("%s", string(body)))
/*
If you want to perform an action based on the response, do the following
resp, err = pipeline.Next
// perform some action
return resp, err
*/
return pipeline.Next(req)
}
```
Create a transport object
```go
// NewCustomTransport can take multiple middlewares
transport := pipeline.NewCustomTransport(&AuthorizationMiddleware{})
transport.MaxIdleConns = 10
transport.IdleConnTimeout = 30 * time.Second
```
Hook up transport with HTTP client
```go
client := &http.Client{Transport: transport}
resp, err := client.Get("https://example.com")
if err == nil {
fmt.Println(resp.Status)
}
```