|
29 | 29 |
|
30 | 30 | ## Usages |
31 | 31 |
|
| 32 | +The basic `Proxy` struct. |
| 33 | +It implements http.Handler interface. |
| 34 | + |
| 35 | +```go |
| 36 | +// Proxy is an HTTP proxy. |
| 37 | +// It implements [net/http.Handler]. |
| 38 | +type Proxy struct { |
| 39 | + // The transport used to perform proxy requests. |
| 40 | + // If nil, [net/http.DefaultTransport] is used. |
| 41 | + Transport http.RoundTripper |
| 42 | + // PreRoundTrip is called just before roundtrip. |
| 43 | + // In request must not be modified in the function. |
| 44 | + // Out request can be modified in the function. |
| 45 | + // PreRoundTrip can be used for rewriting request url |
| 46 | + // or changing request context and so on. |
| 47 | + // Proxy stops proceeding the request when PreRoundTrip |
| 48 | + // returns non-nil error and calls ErrorHandler. |
| 49 | + PreRoundTrip func(*ProxyRequest) error |
| 50 | + // PostRoundTrip is called just after roundtrip. |
| 51 | + // The given response is the response from backend. |
| 52 | + // In and Out requests must not be modified in PostRoundTrip. |
| 53 | + // In holds the frontend-side request and response. |
| 54 | + // Out holds the backend-side request and response. |
| 55 | + PostRoundTrip func(*In, *Out) error |
| 56 | + // ErrorHandler is the optional error handler. |
| 57 | + // If non-nil, any errors occurred while proxying is given. |
| 58 | + // If nil, a default error handler is used. |
| 59 | + // The ResponseWriter will be nil when response is not writable. |
| 60 | + ErrorHandler func(http.ResponseWriter, *http.Request, *Error) |
| 61 | +} |
| 62 | + |
| 63 | +func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) |
| 64 | +func (p *Proxy) handleError(w http.ResponseWriter, r *http.Request, err *Error) |
| 65 | +``` |
| 66 | + |
32 | 67 | ### Built-in round-robin proxy |
33 | 68 |
|
34 | 69 | This example shows how to use built-in simple round-robin proxy. |
|
0 commit comments