engine

package
v1.13.5 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2024 License: Apache-2.0 Imports: 19 Imported by: 105

Documentation

Index

Constants

View Source
const DefaultMaxRequestBodySize = 32 << 20 // 32 MB

Variables

View Source
var (
	ErrUnsupported = errors.New(`Unsupported`)
)
View Source
var (
	HeaderSetCookie = `Set-Cookie`
)

Functions

func AddressPort added in v1.3.9

func AddressPort(address string) int

func Bytes2str

func Bytes2str(b []byte) string

func Get

func Get(name string) func(*Config) Engine

func NewListener added in v1.3.0

func NewListener(address string, reuse bool) (net.Listener, error)

func Register added in v1.6.0

func Register(name string, newEngine func(*Config) Engine)

func Str2bytes

func Str2bytes(s string) []byte

Types

type Config

type Config struct {
	Address            string       // TCP address to listen on.
	Listener           net.Listener // Custom `net.Listener`. If set, server accepts connections on it.
	ReusePort          bool
	TLSAuto            bool
	TLSHosts           []string
	TLSEmail           string
	TLSCacheDir        string
	TLSConfig          *tls.Config
	TLSCertFile        string        // TLS certificate file path.
	TLSKeyFile         string        // TLS key file path.
	DisableHTTP2       bool          // Disables HTTP/2.
	ReadTimeout        time.Duration // Maximum duration before timing out read of the request.
	WriteTimeout       time.Duration // Maximum duration before timing out write of the response.
	MaxConnsPerIP      int
	MaxRequestsPerConn int
	MaxRequestBodySize int
}

Config defines engine configuration.

func (*Config) AddTLSCert added in v1.3.0

func (c *Config) AddTLSCert(certFile, keyFile string) *Config

func (*Config) InitListener added in v1.3.0

func (c *Config) InitListener(before ...func() error) error

func (*Config) InitTLSConfig added in v1.3.0

func (c *Config) InitTLSConfig(certAndKey ...string) *Config

func (*Config) InitTLSListener added in v1.3.0

func (c *Config) InitTLSListener(before ...func() error) error

func (*Config) NewAutoTLSManager added in v1.4.3

func (c *Config) NewAutoTLSManager(hosts ...string) *autocert.Manager

func (*Config) Print added in v1.3.0

func (c *Config) Print(engine string)

func (*Config) SetListener added in v1.3.0

func (c *Config) SetListener(ln net.Listener) *Config

func (*Config) SupportAutoTLS added in v1.3.0

func (c *Config) SupportAutoTLS(autoTLSManager *autocert.Manager, hosts ...string) *Config

type ConfigSetter added in v1.6.0

type ConfigSetter func(*Config)

func Address added in v1.6.0

func Address(v string) ConfigSetter

Address TCP address to listen on.

func DisableHTTP2 added in v1.6.0

func DisableHTTP2(v bool) ConfigSetter

DisableHTTP2 Disables HTTP/2.

func Listener added in v1.6.0

func Listener(v net.Listener) ConfigSetter

Listener Custom `net.Listener`. If set, server accepts connections on it.

func MaxConnsPerIP added in v1.6.0

func MaxConnsPerIP(v int) ConfigSetter

func MaxRequestBodySize added in v1.6.0

func MaxRequestBodySize(v int) ConfigSetter

func MaxRequestsPerConn added in v1.6.0

func MaxRequestsPerConn(v int) ConfigSetter

func ReadTimeout added in v1.6.0

func ReadTimeout(v time.Duration) ConfigSetter

ReadTimeout Maximum duration before timing out read of the request.

func ReusePort added in v1.6.0

func ReusePort(v bool) ConfigSetter

func TLSAuto added in v1.6.0

func TLSAuto(v bool) ConfigSetter

func TLSCacheDir added in v1.6.0

func TLSCacheDir(v string) ConfigSetter

func TLSCertFile added in v1.6.0

func TLSCertFile(v string) ConfigSetter

TLSCertFile TLS certificate file path.

func TLSConfig added in v1.6.0

func TLSConfig(v *tls.Config) ConfigSetter

func TLSEmail added in v1.6.0

func TLSEmail(v string) ConfigSetter

func TLSHosts added in v1.6.0

func TLSHosts(v []string) ConfigSetter

func TLSKeyFile added in v1.6.0

func TLSKeyFile(v string) ConfigSetter

TLSKeyFile TLS key file path.

func WriteTimeout added in v1.6.0

func WriteTimeout(v time.Duration) ConfigSetter

WriteTimeout Maximum duration before timing out write of the response.

type Engine

type Engine interface {
	SetHandler(Handler)
	SetLogger(logger.Logger)
	Start() error
	Stop() error
	Shutdown(ctx context.Context) error
	Config() *Config
}

Engine defines an interface for HTTP server.

func New added in v1.6.0

func New(name string, config *Config) Engine

type Handler

type Handler interface {
	ServeHTTP(Request, Response)
}

Handler defines an interface to server HTTP requests via `ServeHTTP(Request, Response)` function.

type HandlerFunc

type HandlerFunc func(Request, Response)

HandlerFunc is an adapter to allow the use of `func(Request, Response)` as HTTP handlers.

func (HandlerFunc) ServeHTTP

func (h HandlerFunc) ServeHTTP(req Request, res Response)

ServeHTTP serves HTTP request.

type Header interface {
	// Add adds the key, value pair to the header. It appends to any existing values
	// associated with key.
	Add(string, string)

	// Del deletes the values associated with key.
	Del(string)

	// Get gets the first value associated with the given key. If there are
	// no values associated with the key, Get returns "".
	Get(string) string
	Values(string) []string

	// Set sets the header entries associated with key to the single element value.
	// It replaces any existing values associated with key.
	Set(string, string)

	Object() interface{}

	Std() http.Header
}

Header defines an interface for HTTP header.

type Request

type Request interface {
	Context() context.Context
	WithContext(ctx context.Context) *http.Request
	SetValue(key string, value interface{})
	SetMaxSize(maxSize int)
	MaxSize() int

	// Scheme returns the HTTP protocol scheme, `http` or `https`.
	Scheme() string

	// Host returns HTTP request host. Per RFC 2616, this is either the value of
	// the `Host` header or the host name given in the URL itself.
	Host() string

	// SetHost sets the host of the request.
	SetHost(string)

	// URI returns the unmodified `Request-URI` sent by the client.
	URI() string

	// SetURI sets the URI of the request.
	SetURI(string)

	// URL returns `engine.URL`.
	URL() URL

	// Header returns `engine.Header`.
	Header() Header

	// Proto returns the HTTP proto. (HTTP/1.1 etc.)
	Proto() string

	// RemoteAddress returns the client's network address.
	RemoteAddress() string

	// Method returns the request's HTTP function.
	Method() string

	// SetMethod sets the HTTP method of the request.
	SetMethod(string)

	// Body returns request's body.
	Body() io.ReadCloser

	SetBody(io.Reader)

	// FormValue returns the form field value for the provided name.
	FormValue(string) string
	Object() interface{}

	Form() URLValuer
	PostForm() URLValuer

	// MultipartForm returns the multipart form.
	MultipartForm() (*multipart.Form, error)

	// IsTLS returns true if HTTP connection is TLS otherwise false.
	IsTLS() bool
	Cookie(string) string
	Referer() string

	// UserAgent returns the client's `User-Agent`.
	UserAgent() string

	// FormFile returns the multipart form file for the provided name.
	FormFile(string) (multipart.File, *multipart.FileHeader, error)

	// Size returns the size of request's body.
	Size() int64

	BasicAuth() (string, string, bool)

	StdRequest() *http.Request
}

Request defines an interface for HTTP request.

type Response

type Response interface {
	// Header returns `engine.Header`
	Header() Header

	// WriteHeader sends an HTTP response header with status code.
	WriteHeader(int)

	KeepBody(bool)

	// Write writes the data to the connection as part of an HTTP reply.
	Write([]byte) (int, error)

	// Status returns the HTTP response status.
	Status() int

	// Size returns the number of bytes written to HTTP response.
	Size() int64

	// Committed returns true if HTTP response header is written, otherwise false.
	Committed() bool

	// SetWriter sets the HTTP response writer.
	SetWriter(io.Writer)

	// Write returns the HTTP response writer.
	Writer() io.Writer
	Object() interface{}

	Hijacker(func(net.Conn)) error
	Body() []byte
	Redirect(string, int)
	NotFound()
	SetCookie(*http.Cookie)
	ServeFile(string)
	ServeContent(content io.ReadSeeker, name string, modtime time.Time)
	Stream(func(io.Writer) (bool, error)) error
	Error(string, ...int)

	StdResponseWriter() http.ResponseWriter
}

Response defines an interface for HTTP response.

type URL

type URL interface {
	SetPath(string)
	RawPath() string
	Path() string
	QueryValue(string) string
	QueryValues(string) []string
	Query() url.Values
	RawQuery() string
	SetRawQuery(string)
	String() string
	Object() interface{}
}

URL defines an interface for HTTP request url.

type URLValuer

type URLValuer interface {
	Add(string, string)
	Del(string)
	Get(string) string
	Gets(string) []string
	Set(string, string)
	Encode() string
	All() map[string][]string
	Reset(url.Values)
	Merge(url.Values)
}

URLValuer Wrap url.Values

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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