rest

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2020 License: Apache-2.0 Imports: 16 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, v interface{}) error

Decode decodes the request body into v and calls its OK() function to validate the object.

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

	HTTPListenPort         int
	HTTPServerReadTimeout  time.Duration
	HTTPServerWriteTimeout time.Duration
	HTTPServerIdleTimeout  time.Duration

	ServeFrontend    bool
	CompressionLevel int
}

Config for a HTTP server

func (*Config) RegisterFlags

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

RegisterFlags adds the flags required to config the server

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

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

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.

Jump to

Keyboard shortcuts

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