framework

package
v0.0.0-...-0fc66b4 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2023 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package framework is a minimal web framework.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r *http.Request, val any) error

Decode reads an HTTP request body looking for a JSON document. The body is decoded into the value provided.

The provided value is checked for validation tags if it's a struct.

func GetParam

func GetParam(c *gin.Context, param string) *string

GetParam is a utility to get a path parameter from context, nil if not found

func GetQueryValue

func GetQueryValue(c *gin.Context, param string) *string

GetQueryValue is a utility to get a parameter value from the query string, nil if not found

func IsShutdown

func IsShutdown(err error) bool

IsShutdown checks to see if the shutdown error is contained in the specified error value.

func LoggingRespondErrMsg

func LoggingRespondErrMsg(c *gin.Context, errMsg string, statusCode int)

LoggingRespondErrMsg sends an error response back to the client as a safe error from a msg

func LoggingRespondErrWithMsg

func LoggingRespondErrWithMsg(c *gin.Context, err error, errMsg string, statusCode int)

LoggingRespondErrWithMsg sends an error response back to the client as a safe error from an error and msg

func LoggingRespondError

func LoggingRespondError(c *gin.Context, err error, statusCode int)

LoggingRespondError sends an error response back to the client as a safe error

func NewShutdownError

func NewShutdownError(message string) error

NewShutdownError returns an error that causes the framework to signal. a graceful shutdown

func Respond

func Respond(c *gin.Context, data any, statusCode int)

Respond convert a Go value to JSON and sends it to the client.

func ValidateRequest

func ValidateRequest(request any) error

Types

type ErrorResponse

type ErrorResponse struct {
	Error  string `json:"error"`
	Fields string `json:"fields,omitempty"`
}

ErrorResponse is the structure of response error payloads sent back to the requester when validation of a request payload fails.

type FieldError

type FieldError struct {
	Field string `json:"field"`
	Error string `json:"error"`
}

FieldError is used to indicate an error with a field in a request payload.

type SafeError

type SafeError struct {
	Err        error
	StatusCode int
	Fields     []FieldError
}

SafeError is used to pass an error during the request through the server with web specific context. 'Safe' here means that the error messages do not include any sensitive information and can be sent straight back to the requester

func (*SafeError) Error

func (err *SafeError) Error() string

SafeError implements the `error` interface. It uses the default message of the wrapped error. This is what will be shown in a server's logs

func (*SafeError) FieldErrors

func (err *SafeError) FieldErrors() string

FieldErrors returns a string containing all field errors.

type Server

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

Server is the entrypoint into our application and what configures our context object for each of our http router. Feel free to add any configuration data/logic on this Server struct.

func NewServer

func NewServer(cfg config.ServerConfig, handler *gin.Engine, shutdown chan os.Signal) *Server

NewServer creates a Server that handles a set of routes for the application.

func (*Server) PreShutdownHooks

func (s *Server) PreShutdownHooks(ctx context.Context) error

PreShutdownHooks runs all hooks that were registered by calling RegisterPreShutdownHook.

func (*Server) RegisterPreShutdownHook

func (s *Server) RegisterPreShutdownHook(f func(_ context.Context) error)

RegisterPreShutdownHook registers a possibly blocking function to be run before Shutdown is called.

Jump to

Keyboard shortcuts

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