reverseproxy

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: Apache-2.0 Imports: 21 Imported by: 10

README

reverseproxy (This is a community driven project)

reserve proxy extension for Hertz

Install

go get github.com/hertz-contrib/reverseproxy

Quick Start

package main

import (
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/hertz-contrib/reverseproxy"
)

func main() {
	h := server.New()
	rp, _ := reverseproxy.NewSingleHostReverseProxy("http://localhost:8082/test")
	h.GET("/ping", rp.ServeHTTP)
	h.Spin()
}
Use tls

Currently netpoll does not support tls,we need to use the net (standard library)

package main

import (
	"github.com/cloudwego/hertz/pkg/app/client"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/network/standard"
	"github.com/hertz-contrib/reverseproxy"
)

func main() {
	h := server.New()
	rp, _ := reverseproxy.NewSingleHostReverseProxy("https://localhost:8082/test",client.WithDialer(standard.NewDialer()))
	h.GET("/ping", rp.ServeHTTP)
	h.Spin()
}
Use service discovery

Use nacos as example and more information refer to registry

package main

import (
	"github.com/cloudwego/hertz/pkg/app/client"
	"github.com/cloudwego/hertz/pkg/app/middlewares/client/sd"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/config"
	"github.com/cloudwego/hertz/pkg/protocol"
	"github.com/hertz-contrib/registry/nacos"
	"github.com/hertz-contrib/reverseproxy"
)

func main() {
	cli, err := client.NewClient()
	if err != nil{
		panic(err)
	}
	r, err := nacos.NewDefaultNacosResolver()
	if err != nil{
		panic(err)
	}
	cli.Use(sd.Discovery(r))
	h := server.New()
	rp, _ := reverseproxy.NewSingleHostReverseProxy("http://test.demo.api/test")
	rp.SetClient(cli)
	rp.SetDirector(func(req *protocol.Request){
		req.SetRequestURI(string(reverseproxy.JoinURLPath(req, rp.Target)))
		req.Header.SetHostBytes(req.URI().Host())
		req.Options().Apply([]config.RequestOption{config.WithSD(true)})
	})
	h.GET("/ping", rp.ServeHTTP)
	h.Spin()
}
Request/Response

ReverseProxy provides SetDirectorSetModifyResponseSetErrorHandler to modify Request and Response.

Websocket Reverse Proxy

Websocket reverse proxy for Hertz, inspired by fasthttp-reverse-proxy

package main

import (
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/hertz-contrib/reverseproxy"
)

func main() {
	h := server.Default()
	h.GET("/backend", reverseproxy.NewWSReverseProxy("ws://example.com").ServeHTTP)
	h.Spin()
}
Configuration Default Description
WithDirector nil customize the forward header
WithDialer gorillaws.DefaultDialer for dialer customization
WithUpgrader hzws.HertzUpgrader for upgrader customization
More info

See example

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultOptions = &Options{
	Director: nil,
	Dialer:   websocket.DefaultDialer,
	Upgrader: &hzws.HertzUpgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	},
}

Functions

func JoinURLPath

func JoinURLPath(req *protocol.Request, target string) (path []byte)

Types

type Director added in v0.1.4

type Director func(ctx context.Context, c *app.RequestContext, forwardHeader http.Header)

type Option added in v0.1.4

type Option func(o *Options)

func WithDialer added in v0.1.4

func WithDialer(dialer *websocket.Dialer) Option

WithDialer for dialer customization

func WithDirector added in v0.1.4

func WithDirector(director Director) Option

WithDirector user can edit the forward header by using custom Director NOTE: custom Director will overwrite default forward header field if they have the same key

func WithUpgrader added in v0.1.4

func WithUpgrader(upgrader *hzws.HertzUpgrader) Option

WithUpgrader for upgrader customization

type Options added in v0.1.4

type Options struct {
	Director Director
	Dialer   *websocket.Dialer
	Upgrader *hzws.HertzUpgrader
}

type ReverseProxy

type ReverseProxy struct {

	// target is set as a reverse proxy address
	Target string
	// contains filtered or unexported fields
}

func NewSingleHostReverseProxy

func NewSingleHostReverseProxy(target string, options ...config.ClientOption) (*ReverseProxy, error)

NewSingleHostReverseProxy returns a new ReverseProxy that routes URLs to the scheme, host, and base path provided in target. If the target's path is "/base" and the incoming request was for "/dir", the target request will be for /base/dir. NewSingleHostReverseProxy does not rewrite the Host header. To rewrite Host headers, use ReverseProxy directly with a custom director policy.

Note: if no config.ClientOption is passed it will use the default global client.Client instance. When passing config.ClientOption it will initialize a local client.Client instance. Using ReverseProxy.SetClient if there is need for shared customized client.Client instance.

func (*ReverseProxy) ServeHTTP

func (r *ReverseProxy) ServeHTTP(c context.Context, ctx *app.RequestContext)

func (*ReverseProxy) SetClient

func (r *ReverseProxy) SetClient(client *client.Client)

SetClient use to customize client

func (*ReverseProxy) SetDirector

func (r *ReverseProxy) SetDirector(director func(req *protocol.Request))

SetDirector use to customize protocol.Request

func (*ReverseProxy) SetErrorHandler

func (r *ReverseProxy) SetErrorHandler(eh func(c *app.RequestContext, err error))

SetErrorHandler use to customize error handler

func (*ReverseProxy) SetModifyResponse

func (r *ReverseProxy) SetModifyResponse(mr func(*protocol.Response) error)

SetModifyResponse use to modify response

func (*ReverseProxy) SetSaveOriginResHeader added in v0.1.4

func (r *ReverseProxy) SetSaveOriginResHeader(b bool)

func (*ReverseProxy) SetTransferTrailer added in v0.1.4

func (r *ReverseProxy) SetTransferTrailer(b bool)

type WSReverseProxy added in v0.1.4

type WSReverseProxy struct {
	// contains filtered or unexported fields
}

func NewWSReverseProxy added in v0.1.4

func NewWSReverseProxy(target string, opts ...Option) *WSReverseProxy

NewWSReverseProxy new a proxy which will provide handler for websocket reverse proxy

func (*WSReverseProxy) ServeHTTP added in v0.1.4

func (w *WSReverseProxy) ServeHTTP(ctx context.Context, c *app.RequestContext)

ServeHTTP provides websocket reverse proxy service

Jump to

Keyboard shortcuts

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