httptunnel

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2020 License: MIT Imports: 11 Imported by: 0

README

go-wirenet-http-tunnel

wirenet client or server <--------> wirenet handler <---------------> http server
                          tunnel                       http request

Examples

tunnel.go

// server side
wire, err := wirenet.Mount(":8989")
// OR client side
wire, err := wirenet.Join(":8989")

...
tunnel := httptunnel.New()
wire.Stream("tunnel", tunnel.Handle)

client.go OR server.go

 // server side
 wire, err := wirenet.Mount(":8989")
 // OR client side
 wire, err := wirenet.Join(":8989")
 ...

 // Make client
 client := httptunnel.NewClient(wire, "tunnel")
 ctx := context.Background()
 
 // 1. One request in one stream
 session := hub.findSession("id")
 sessCtx := WithSession(ctx, session.ID())
 req, err := http.NewRequestWithContext(sessCtx, "GET", "http://google.com", nil) 
 resp, err := client.Do(req)
 ...
 
 // 2. Group related queries in one stream
 err = client.WithTx(ctx, session.ID(), func(streamCtx context.Context) error {
    req, err := http.NewRequestWithContext(streamCtx, "GET", "http://domain.com/1", nil) 
    resp, err := client.Do(req)	
    // ...
    req, err = http.NewRequestWithContext(streamCtx, "GET", "http://domain.com/2", nil) 
    resp, err = client.Do(req)	
    // ... 
    req, err = http.NewRequestWithContext(streamCtx, "GET", "http://domain.com/2", nil) 
    resp, err = client.Do(req)	
    return nil
})
 

Documentation

Index

Constants

View Source
const (
	// ContextKeyStreamReader is populated in the context by stream reader.
	ContextKeyStreamReader contextKey = iota + 1

	// ContextKeyStreamWriter is populated in the context by stream writer.
	ContextKeyStreamWriter

	// ContextKeySessionID is populated in the context by current session id.
	ContextKeySessionID
)
View Source
const (
	DefaultReTryCount  = 10
	DefaultHTTPTimeout = 120 * time.Second
)

Variables

View Source
var (
	// ErrStreamReaderNotDefined returned when no stream reader is defined in context.
	ErrStreamReaderNotDefined = errors.New("httptunnel: stream reader not defined")

	// ErrStreamWriterNotDefined returned when no stream reader is defined in context.
	ErrStreamWriterNotDefined = errors.New("httptunnel: stream writer not defined")
)

Functions

func WithSession

func WithSession(ctx context.Context, sessionID uuid.UUID) context.Context

Types

type Client

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

func NewClient

func NewClient(w wirenet.Wire, streamName string, options ...ClientOption) *Client

func (*Client) Do

func (c *Client) Do(req *http.Request) (resp *http.Response, err error)

func (*Client) WithTx

func (c *Client) WithTx(ctx context.Context, sessionID uuid.UUID, fn func(streamCtx context.Context) error) error

type ClientOption

type ClientOption func(c *Client)

ClientOption sets an optional parameter for client instance.

func SetReadResponse

func SetReadResponse(r ReadResponse) ClientOption

type ErrorHandler

type ErrorHandler interface {
	Handle(ctx context.Context, err error)
}

ErrorHandler receives a transport error to be processed for diagnostic purposes. Usually this means logging the error.

type ErrorHandlerFunc

type ErrorHandlerFunc func(ctx context.Context, err error)

The ErrorHandlerFunc type is an adapter to allow the use of ordinary function as ErrorHandler. If f is a function with the appropriate signature, ErrorHandlerFunc(f) is a ErrorHandler that calls f.

func (ErrorHandlerFunc) Handle

func (f ErrorHandlerFunc) Handle(ctx context.Context, err error)

Handle calls f(ctx, err).

type LogErrorHandler

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

func NewLogErrorHandler

func NewLogErrorHandler() *LogErrorHandler

func (*LogErrorHandler) Handle

func (h *LogErrorHandler) Handle(ctx context.Context, err error)

type ReadRequest

type ReadRequest func(*bufio.Reader) (*http.Request, error)

ReadRequest reads and parses an incoming request from b.

type ReadResponse

type ReadResponse func(*bufio.Reader, *http.Request) (*http.Response, error)

ReadResponse reads and returns an HTTP response from r.

type RequestFunc added in v0.2.0

type RequestFunc func(r *http.Request)

RequestFunc

type ResponseFunc added in v0.2.0

type ResponseFunc func(r *http.Response)

ResponseFunc

type Tunnel

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

func New

func New(
	options ...TunnelOption,
) *Tunnel

func (Tunnel) Handle

func (tunnel Tunnel) Handle(ctx context.Context, stream wirenet.Stream)

Handle implements the Handler interface.

type TunnelOption

type TunnelOption func(tunnel *Tunnel)

TunnelOption sets an optional parameter for tunnel instance.

func RequestHook added in v0.2.0

func RequestHook(before ...RequestFunc) TunnelOption

RequestHook functions are executed on the HTTP request object before the request is send.

func ResponseHook added in v0.2.0

func ResponseHook(after ...ResponseFunc) TunnelOption

ResponseHook functions are executed on the HTTP response writer after the endpoint is invoked, but before anything is written to the client.

func SetClient

func SetClient(client httpclient.Client) TunnelOption

SetClient sets the HTTP client.

func SetReadRequest

func SetReadRequest(r ReadRequest) TunnelOption

SetReadRequest sets the parser for incoming request.

func SetTunnelErrorHandler

func SetTunnelErrorHandler(errorHandler ErrorHandler) TunnelOption

SetTunnelErrorHandler is used to handle non-terminal errors. By default, non-terminal errors are ignored. This is intended as a diagnostic measure.

Directories

Path Synopsis
examples
default/client command
default/tunnel command

Jump to

Keyboard shortcuts

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