vulcand / oxy

Go middlewares for HTTP servers & proxies
Apache License 2.0
2.02k stars 324 forks source link

Oxy Build Status

Oxy is a Go library with HTTP handlers that enhance HTTP standard library:

It is designed to be fully compatible with http standard library, easy to customize and reuse.

Status

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


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:


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:


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()