core

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package core provides some core runtime functions.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AcquireResponseWriter is used to customize the wrapper
	// from http.ResponseWriter to ResponseWriter.
	AcquireResponseWriter func(http.ResponseWriter) ResponseWriter = acquireResponseWriter

	// ReleaseResponseWriter is used to release the response writer
	// acquired by AcquireResponseWriter.
	ReleaseResponseWriter func(ResponseWriter) = releaseResponseWriter
)

Functions

func CopyResponse

func CopyResponse(c *Context, resp *http.Response)

CopyResponse copies the response header and body from the upstream server to the client.

func CopyResponseHeader

func CopyResponseHeader(c *Context, resp *http.Response)

CopyResponseHeader copies the response header from the upstream server to the client.

func ReleaseContext

func ReleaseContext(c *Context)

ReleaseContext releases the context to the pool.

func StdResponse

func StdResponse(c *Context, resp *http.Response, err error)

StdResponse is a standard response handler.

Types

type Context

type Context struct {
	Context context.Context
	Next    Handler

	RouteId    string // Set by the router after matching the route.
	UpstreamId string // Set by the router after matching the route.

	// For Client
	ClientRequest  *http.Request  // Set by the router when serving the request.
	ClientResponse ResponseWriter // Set by the router when serving the request.
	Responser      Responser      // Set by the router after matching the route.

	// For Upstream
	Upstream         interface{}
	UpstreamRequest  *http.Request
	UpstreamResponse *http.Response // Set by the upstream after forwarding the request.
	ForwardTimeout   time.Duration
	Endpoint         loadbalancer.Endpoint // Set by the endpoint

	IsAborted bool
	Error     error          // Set when aborting the context process anytime.
	Data      interface{}    // The contex data that is set and used by the final user.
	Kvs       map[string]any // The interim context key-value cache.
	// contains filtered or unexported fields
}

Context represents a runtime context.

func AcquireContext

func AcquireContext(ctx context.Context) *Context

AcquireContext acquires a context from the pool.

func NewContext

func NewContext() *Context

func (*Context) Abort

func (c *Context) Abort(err error)

Abort sets the error informaion and aborts the context process.

func (*Context) CallbackOnForward

func (c *Context) CallbackOnForward()

CallbackOnForward calls the callback functions added by OnForward.

func (*Context) CallbackOnResponseHeader

func (c *Context) CallbackOnResponseHeader()

CallbackOnResponseHeader calls the callback functions added by OnResponseHeader.

func (*Context) ClientIP

func (c *Context) ClientIP() netip.Addr

func (*Context) Cookie

func (c *Context) Cookie(name string) string

Cookie returns the cookie value by the name.

func (*Context) Cookies

func (c *Context) Cookies() []*http.Cookie

Cookies parses and returns the request header Cookie.

func (*Context) OnForward

func (c *Context) OnForward(cb func())

OnForward appends the callback function, which is called before upstream forwards the request.

func (*Context) OnResponseHeader

func (c *Context) OnResponseHeader(cb func())

OnResponseHeader appends the callback function, which is called after setting the response header and before copying the response body from the upstream server to the client.

func (*Context) Queries

func (c *Context) Queries() url.Values

Queries parses and returns the query string.

func (*Context) RemoteAddr

func (c *Context) RemoteAddr() string

RemoteAddr returns the remote address of the request.

func (*Context) RequestID

func (c *Context) RequestID() string

RequestID returns the request id of the request.

func (*Context) Reset

func (c *Context) Reset()

Reset resets the context to the initial state.

func (*Context) SendResponse

func (c *Context) SendResponse()

SendResponse sends the response from the upstream server to the client.

type Handler

type Handler func(c *Context)

Handler is used to handle the http request.

type ResponseWriter

type ResponseWriter interface {
	http.ResponseWriter
	WroteHeader() bool
	StatusCode() int
}

ResponseWriter is the extension of http.ResponseWriter.

type Responser

type Responser interface {
	Respond(*Context, *http.Response, error)
}

Responser is used to handle the response from the upstream server to the client.

type ResponserFunc added in v0.3.0

type ResponserFunc func(*Context, *http.Response, error)

ResponserFunc is a Responder function.

func (ResponserFunc) Respond added in v0.3.0

func (f ResponserFunc) Respond(c *Context, resp *http.Response, err error)

Respond implements the interface Responder.

Jump to

Keyboard shortcuts

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