tinyhttp

package
v1.0.20 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 10 Imported by: 2

Documentation

Overview

Package tinyhttp provides HTTP server implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Server

type Server struct {
	*gin.Engine
	// contains filtered or unexported fields
}

Server is an object representing gin.Engine and implementing the tiny.Service interface.

func NewServer

func NewServer(address string, opts ...ServerOpt) *Server

NewServer creates new Server instance.

func (*Server) OnNoRoute

func (server *Server) OnNoRoute(handler func(c *gin.Context))

OnNoRoute sets a handler for requests that cannot be routed and would end up as 404s.

func (*Server) OnPanic

func (server *Server) OnPanic(handler func(c *gin.Context, recovered any))

OnPanic sets a handler for requests that resulted in panic and would end up as 500s.

func (*Server) Start

func (server *Server) Start() error

Start implements the interface of tiny.Service.

func (*Server) Stop

func (server *Server) Stop()

Stop implements the interface of tiny.Service.

type ServerConfig

type ServerConfig struct {

	// Network is a network type for the listener (default: "tcp").
	Network string

	// SecurityHeaders defines whether to include HTTP security headers to all responses or not (default: true).
	SecurityHeaders bool

	// ShutdownTimeout defines a maximal timeout of HTTP server shutdown (default: 5s).
	ShutdownTimeout time.Duration

	// TLSCert is a path to TLS certificate to use. When specified with TLSKey - enables TLS mode.
	TLSCert string

	// TLSKey is a path to TLS key to use. When specified with TLSCert - enables TLS mode.
	TLSKey string

	// TLSConfig is an optional TLS configuration to pass when using TLS mode.
	TLSConfig *tls.Config

	// ReadTimeout is a timeout used when creating underlying http server (see http.Server) (default: 5s).
	ReadTimeout time.Duration

	// ReadHeaderTimeout is a timeout used when creating underlying http server (see http.Server).
	ReadHeaderTimeout time.Duration

	// WriteTimeout is a timeout used when creating underlying http server (see http.Server) (default: 10s).
	WriteTimeout time.Duration

	// IdleTimeout is a timeout used when creating underlying http server (see http.Server) (default 2m).
	IdleTimeout time.Duration

	// MaxHeaderBytes is a value used when creating underlying http server (see http.Server).
	MaxHeaderBytes int

	// TrustedProxies is a list of CIDR address ranges that can be trusted when handling RemoteIP header.
	// (default: "10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16", "127.0.0.0/8", "fc00::/7", "::1/128")
	TrustedProxies []string

	// RemoteIPHeaders is a list of headers that overwrite the value of client's remote address.
	// (default: "X-Forwarded-For")
	RemoteIPHeaders []string
	// contains filtered or unexported fields
}

ServerConfig holds a configuration for NewServer.

type ServerOpt

type ServerOpt = func(*ServerConfig)

ServerOpt is an option to be specified to NewServer.

func IdleTimeout

func IdleTimeout(timeout time.Duration) ServerOpt

IdleTimeout is a timeout used when creating underlying http server (see http.Server).

func MaxHeaderBytes

func MaxHeaderBytes(maxHeaderBytes int) ServerOpt

MaxHeaderBytes is a value used when creating underlying http server (see http.Server).

func Network added in v1.0.8

func Network(network string) ServerOpt

Network is a network type for the listener.

func ReadHeaderTimeout

func ReadHeaderTimeout(timeout time.Duration) ServerOpt

ReadHeaderTimeout is a timeout used when creating underlying http server (see http.Server).

func ReadTimeout

func ReadTimeout(timeout time.Duration) ServerOpt

ReadTimeout is a timeout used when creating underlying http server (see http.Server).

func RemoteIPHeaders

func RemoteIPHeaders(headers []string) ServerOpt

RemoteIPHeaders is a list of headers that overwrite the value of client's remote address.

func SecurityHeaders

func SecurityHeaders(securityHeaders bool) ServerOpt

SecurityHeaders defines whether to include HTTP security headers to all responses or not.

func ShutdownTimeout

func ShutdownTimeout(timeout time.Duration) ServerOpt

ShutdownTimeout defines a maximal timeout of HTTP server shutdown.

func TLS

func TLS(cert, key string, tlsConfig ...*tls.Config) ServerOpt

TLS enables TLS mode if both cert and key point to valid TLS credentials. tlsConfig is optional.

func TrustedProxies

func TrustedProxies(trustedProxies []string) ServerOpt

TrustedProxies is a list of CIDR address ranges that can be trusted when handling RemoteIP header.

func WriteTimeout

func WriteTimeout(timeout time.Duration) ServerOpt

WriteTimeout is a timeout used when creating underlying http server (see http.Server).

Directories

Path Synopsis
Package httpauth provides authorization middleware for HTTP.
Package httpauth provides authorization middleware for HTTP.
Package requests provides HTTP client implementation.
Package requests provides HTTP client implementation.

Jump to

Keyboard shortcuts

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