rest

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: Apache-2.0 Imports: 21 Imported by: 11

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetQueryParam added in v0.8.0

func GetQueryParam(r *http.Request, key string) string

GetQueryParam retrieves the query parameter with your given key from the request, unescapes the value and returns it. If the query parameter does not exist an empty string will be returned. If the unescaping fails the unescaped string will be returned.

func GetURLParam added in v0.8.0

func GetURLParam(r *http.Request, key string) string

GetURLParam retrieves the url parameter with your given key from the request, unescapes the value and returns it. If the url parameter does not exist an empty string will be returned. If the unescaping fails the unescaped string will be returned.

func HandleMethodNotAllowed

func HandleMethodNotAllowed(logger *zap.Logger) http.HandlerFunc

HandleMethodNotAllowed returns a handler func to respond to routes requested with the wrong verb a REST compliant error message

func HandleNotFound

func HandleNotFound(logger *zap.Logger) http.HandlerFunc

HandleNotFound returns a handler func to respond to non existent routes with a REST compliant error message

func SendRESTError

func SendRESTError(w http.ResponseWriter, r *http.Request, logger *zap.Logger, restErr *Error)

SendRESTError accepts a REST error which can be send to the user

func SendResponse

func SendResponse(w http.ResponseWriter, r *http.Request, logger *zap.Logger, status int, data interface{})

SendResponse tries to send your data as JSON. If this fails it will print REST compliant errors

func TokenFromHeader

func TokenFromHeader(r *http.Request) string

TokenFromHeader tries to retreive the token string from the "Authorization" reqeust header: "Authorization: BEARER T".

Types

type Config

type Config struct {
	ServerGracefulShutdownTimeout time.Duration `yaml:"gracefulShutdownTimeout"`

	HTTPListenAddress      string        `yaml:"listenAddress"`
	HTTPListenPort         int           `yaml:"listenPort"`
	HTTPServerReadTimeout  time.Duration `yaml:"readTimeout"`
	HTTPServerWriteTimeout time.Duration `yaml:"writeTimeout"`
	HTTPServerIdleTimeout  time.Duration `yaml:"idleTimeout"`

	// HTTPSListenPort and HTTPListenPort are separate so that we can listen
	// on both and redirect users to the HTTPS url.
	HTTPSListenPort int `yaml:"httpsListenPort"`
	// AdvertisedHTTPSListenPort is the HTTPS port that will be used for
	// redirecting the user in the browser. If your application is serving
	// the TLS port on 8081 but some LoadBalancer between the user and your
	// Go application remaps this to 443 you want to set this port to
	// 443 as well. Otherwise, users will be redirected to your HTTPSListenPort.
	AdvertisedHTTPSListenPort int `yaml:"advertisedHttpsListenPort"`

	CompressionLevel int `yaml:"compressionLevel"`

	BasePath                        string `yaml:"basePath"`
	SetBasePathFromXForwardedPrefix bool   `yaml:"setBasePathFromXForwardedPrefix"`
	StripPrefix                     bool   `yaml:"stripPrefix"`

	TLS TLSConfig `yaml:"tls"`
}

Config for a HTTP server

func (*Config) RegisterFlags

func (c *Config) RegisterFlags(f *flag.FlagSet)

RegisterFlags adds the flags required to config the server

func (*Config) SetDefaults added in v0.4.0

func (c *Config) SetDefaults()

type Error

type Error struct {
	Err          error           `json:"-"`
	Status       int             `json:"statusCode"`
	Message      string          `json:"message"`
	InternalLogs []zapcore.Field `json:"-"`
	IsSilent     bool            `json:"-"`
}

Error must be created to issue a REST compliant error

func Decode

func Decode(w http.ResponseWriter, r *http.Request, dst interface{}) *Error

Decode tries to decode the request body into dst and calls its OK() function to validate the object. It returns an error if: - the content-type does not contain "application/json" - body is smaller than 1MB - any unknown fields were set - deserialization fails - the OK() method returns an error

type Server

type Server struct {
	Router *chi.Mux
	Server *http.Server
	Logger *zap.Logger
	// contains filtered or unexported fields
}

Server struct to handle a common http routing server

func NewServer

func NewServer(cfg *Config, logger *zap.Logger, router *chi.Mux) (*Server, error)

NewServer create server instance

func (*Server) Start

func (s *Server) Start() error

Start the HTTP server and blocks until we either receive a signal or the HTTP server returns an error.

type TLSConfig added in v0.9.0

type TLSConfig struct {
	Enabled      bool   `yaml:"enabled"`
	CertFilepath string `yaml:"certFilepath"`
	KeyFilepath  string `yaml:"keyFilepath"`
}

TLSConfig contains the configuration properties for the HTTP TLS configuration. If enabled, the HTTP server will serve on HTTPS and terminate TLS.

func (*TLSConfig) RegisterFlagsWithPrefix added in v0.9.0

func (c *TLSConfig) RegisterFlagsWithPrefix(f *flag.FlagSet, prefix string)

RegisterFlagsWithPrefix adds the flags required to config the server

Jump to

Keyboard shortcuts

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