server

package
v0.0.0-...-fac7322 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrorGenericError     = NewError("Something expected went wrong.", "GENERIC_ERROR", http.StatusInternalServerError)
	ErrorInvalidAuthToken = NewError("Your request has an invalid authorization token.", "AUTH_TOKEN_INVALID", http.StatusUnauthorized)
	ErrorMissingAuthToken = NewError("Your request is missing an authorization token.", "AUTH_TOKEN_MISSING", http.StatusUnauthorized)
	ErrorNotFound         = NewError("The entity you requested was not found.", "NOT_FOUND", http.StatusNotFound)
	ErrorValidationErrors = NewError("Your request has some validation errors.", "VALIDATION_ERRORS", http.StatusBadRequest)
)

Functions

func WriteErrorResponse

func WriteErrorResponse(w http.ResponseWriter, errorResponse ErrorInterface)

func WriteResponse

func WriteResponse(w http.ResponseWriter, data interface{}, metadata *Metadata, statusCode ...int)

Types

type ContextKey

type ContextKey string
const (
	UserContextKey ContextKey = "user"
)

type Error

type Error struct {
	Message string `json:"message"`
	Status  int    `json:"status"`
	Type    string `json:"type"`
}

func NewError

func NewError(message, errorType string, status int) *Error

func (Error) GetMessage

func (e Error) GetMessage() string

func (Error) GetStatus

func (e Error) GetStatus() int

func (Error) String

func (e Error) String() string

type ErrorInterface

type ErrorInterface interface {
	GetMessage() string
	GetStatus() int
}

type Metadata

type Metadata struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
	Total  int `json:"total"`
}

func NewMetadata

func NewMetadata(total int, params *PaginationParams) *Metadata

type PaginationParams

type PaginationParams struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

type Server

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

func NewServer

func NewServer(ctx context.Context, port int, allowedOrigins []string, allowedHeaders []string) *Server

func (*Server) GetRouter

func (s *Server) GetRouter() *mux.Router

func (*Server) Start

func (s *Server) Start()

func (*Server) Stop

func (s *Server) Stop()

type ValidationErrors

type ValidationErrors struct {
	Fields []ValidationFieldError `json:"fields"`
	*Error
}

func NewValidationErrors

func NewValidationErrors(fields []ValidationFieldError) *ValidationErrors

type ValidationFieldError

type ValidationFieldError struct {
	Field   string                 `json:"field"`
	Fields  []ValidationFieldError `json:"fields,omitempty"`
	Message string                 `json:"message"`
}

Jump to

Keyboard shortcuts

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