Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/vulcand/oxy
Go middlewares for HTTP servers & proxies
https://github.com/vulcand/oxy
Last synced: 1 day ago
JSON representation
Go middlewares for HTTP servers & proxies
- Host: GitHub
- URL: https://github.com/vulcand/oxy
- Owner: vulcand
- License: apache-2.0
- Created: 2014-12-06T07:23:38.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2024-12-16T18:01:38.000Z (29 days ago)
- Last Synced: 2025-01-06T18:13:14.845Z (8 days ago)
- Language: Go
- Homepage:
- Size: 720 KB
- Stars: 2,036
- Watchers: 73
- Forks: 324
- Open Issues: 33
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- go-awesome - oxy - Go middleware for HTTP servers and proxies (Open source library / The Internet)
README
Oxy [![Build Status](https://travis-ci.org/vulcand/oxy.svg?branch=master)](https://travis-ci.org/vulcand/oxy)
=====Oxy is a Go library with HTTP handlers that enhance HTTP standard library:
* [Buffer](https://pkg.go.dev/github.com/vulcand/oxy/buffer) retries and buffers requests and responses
* [Stream](https://pkg.go.dev/github.com/vulcand/oxy/stream) passes-through requests, supports chunked encoding with configurable flush interval
* [Forward](https://pkg.go.dev/github.com/vulcand/oxy/forward) forwards requests to remote location and rewrites headers
* [Roundrobin](https://pkg.go.dev/github.com/vulcand/oxy/roundrobin) is a round-robin load balancer
* [Circuit Breaker](https://pkg.go.dev/github.com/vulcand/oxy/cbreaker) Hystrix-style circuit breaker
* [Connlimit](https://pkg.go.dev/github.com/vulcand/oxy/connlimit) Simultaneous connections limiter
* [Ratelimit](https://pkg.go.dev/github.com/vulcand/oxy/ratelimit) Rate limiter (based on tokenbucket algo)
* [Trace](https://pkg.go.dev/github.com/vulcand/oxy/trace) Structured request and response loggerIt is designed to be fully compatible with http standard library, easy to customize and reuse.
Status
------* Initial design is completed
* Covered by tests
* Used as a reverse proxy engine in [Vulcand](https://github.com/vulcand/vulcand)Quickstart
-----------Every handler is ``http.Handler``, so writing and plugging in a middleware is easy. Let us write a simple reverse proxy as an example:
Simple reverse proxy
====================```go
import (
"net/http"
"github.com/vulcand/oxy/v2/forward"
"github.com/vulcand/oxy/v2/testutils"
)// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd := forward.New(false)redirect := http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
// let us forward this request to another server
req.URL = testutils.ParseURI("http://localhost:63450")
fwd.ServeHTTP(w, req)
})
// that's it! our reverse proxy is ready!
s := &http.Server{
Addr: ":8080",
Handler: redirect,
}
s.ListenAndServe()
```As a next step, let us add a round robin load-balancer:
```go
import (
"net/http"
"github.com/vulcand/oxy/v2/forward"
"github.com/vulcand/oxy/v2/roundrobin"
)// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd := forward.New(false)
lb, _ := roundrobin.New(fwd)lb.UpsertServer(url1)
lb.UpsertServer(url2)s := &http.Server{
Addr: ":8080",
Handler: lb,
}
s.ListenAndServe()
```What if we want to handle retries and replay the request in case of errors? `buffer` handler will help:
```go
import (
"net/http"
"github.com/vulcand/oxy/v2/forward"
"github.com/vulcand/oxy/v2/buffer"
"github.com/vulcand/oxy/v2/roundrobin"
)// Forwards incoming requests to whatever location URL points to, adds proper forwarding headers
fwd := forward.New(false)
lb, _ := roundrobin.New(fwd)// buffer will read the request body and will replay the request again in case if forward returned status
// corresponding to nework error (e.g. Gateway Timeout)
buffer, _ := buffer.New(lb, buffer.Retry(`IsNetworkError() && Attempts() < 2`))lb.UpsertServer(url1)
lb.UpsertServer(url2)// that's it! our reverse proxy is ready!
s := &http.Server{
Addr: ":8080",
Handler: buffer,
}
s.ListenAndServe()
```