adapters

package
v1.5.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 23, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package adapters bridges stdlib net/http middleware and handlers into celeris handler chains.

Use WrapMiddleware to adapt any func(http.Handler) http.Handler into a celeris.HandlerFunc, enabling reuse of existing stdlib middleware such as rs/cors, gorilla/csrf, and similar libraries.

Use ReverseProxy to forward requests to a backend URL, with optional hooks via WithTransport, WithModifyRequest, WithModifyResponse, and WithErrorHandler.

Note: do not combine WrapMiddleware with a stdlib CORS library and the native celeris/middleware/cors in the same chain — this produces duplicate Access-Control-* headers and conflicting preflight handling.

Documentation

Full guides and examples: https://goceleris.dev/docs/net-http-interop

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReverseProxy

func ReverseProxy(target *url.URL, opts ...Option) celeris.HandlerFunc

ReverseProxy creates a reverse proxy handler that forwards requests to the given target URL. Uses httputil.ReverseProxy under the hood, wrapped via celeris.Adapt for integration with the celeris handler chain.

The proxy sets X-Forwarded-For, X-Forwarded-Host, and X-Forwarded-Proto headers automatically via httputil.ProxyRequest.SetXForwarded.

Panics if target is nil.

Example
package main

import (
	"fmt"
	"net/http"
	"net/url"

	"github.com/goceleris/celeris"
	"github.com/goceleris/celeris/middleware/adapters"
)

func main() {
	target, _ := url.Parse("http://backend:8080")

	s := celeris.New(celeris.Config{})
	s.Any("/api/*path", adapters.ReverseProxy(target,
		adapters.WithModifyRequest(func(r *http.Request) {
			r.Header.Set("X-Forwarded-By", "celeris")
		}),
	))
	fmt.Println("proxy configured")
}
Output:
proxy configured
Example (ModifyResponse)
package main

import (
	"fmt"
	"net/http"
	"net/url"

	"github.com/goceleris/celeris"
	"github.com/goceleris/celeris/middleware/adapters"
)

func main() {
	target, _ := url.Parse("http://backend:8080")

	s := celeris.New(celeris.Config{})
	s.Any("/api/*path", adapters.ReverseProxy(target,
		adapters.WithModifyResponse(func(resp *http.Response) error {
			resp.Header.Set("X-Proxy", "celeris")
			return nil
		}),
	))
	fmt.Println("proxy with response modifier configured")
}
Output:
proxy with response modifier configured

func WrapMiddleware

func WrapMiddleware(mw func(http.Handler) http.Handler) celeris.HandlerFunc

WrapMiddleware adapts a standard net/http middleware (func(http.Handler) http.Handler) into a celeris HandlerFunc. The adapted middleware receives a reconstructed *http.Request and a responseCapture that records headers, status, and body.

When the stdlib middleware calls the inner handler (next.ServeHTTP), the celeris handler chain continues via c.Next(). Any headers the stdlib middleware set on the ResponseWriter before calling next are propagated to the celeris response.

When the stdlib middleware does NOT call the inner handler (e.g., it returns an early 403), the captured response is written back via c.Blob.

Example
package main

import (
	"fmt"
	"net/http"

	"github.com/goceleris/celeris"
	"github.com/goceleris/celeris/middleware/adapters"
)

func main() {
	// Wrap a standard net/http middleware that adds a header.
	addHeader := func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("X-Middleware", "active")
			next.ServeHTTP(w, r)
		})
	}

	s := celeris.New(celeris.Config{})
	s.Use(adapters.WrapMiddleware(addHeader))
	s.GET("/", func(c *celeris.Context) error {
		return c.String(200, "ok")
	})
	fmt.Println("middleware registered")
}
Output:
middleware registered

Types

type Option

type Option func(*proxyConfig)

Option configures a ReverseProxy.

func WithErrorHandler

func WithErrorHandler(f func(http.ResponseWriter, *http.Request, error)) Option

WithErrorHandler registers a function that handles proxy errors (e.g., connection refused, timeout). If not set, the default httputil.ReverseProxy error handler is used.

func WithModifyRequest

func WithModifyRequest(f func(*http.Request)) Option

WithModifyRequest registers a function that mutates the outbound request before it is sent to the target.

func WithModifyResponse

func WithModifyResponse(f func(*http.Response) error) Option

WithModifyResponse registers a function that modifies the response from the backend before it is forwarded to the client. The function can inspect or modify the response headers and status code. If it returns an error, the error handler is called.

func WithTransport

func WithTransport(rt http.RoundTripper) Option

WithTransport sets the transport used by the reverse proxy.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL