api

package
v0.0.0-...-bc47717 Latest Latest
Warning

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

Go to latest
Published: May 17, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// SessionCtxKey is the context.Context key to store the request context.
	SessionUserCtxKey = &ContextKey{Name: "Session.User"}
	SessionRoleCtxKey = &ContextKey{Name: "Session.Role"}
)
View Source
var (
	// ErrBadID error message for bad or invalid id
	ErrBadID = &ApiError{StatusCode: http.StatusBadRequest, ErrorText: "bad or invalid id"}
	// ErrPermissionDenied error message for permission denied
	ErrPermissionDenied = &ApiError{StatusCode: http.StatusUnauthorized, ErrorText: "permission denied"}
	// ErrInvalidSession error message for invalid session
	ErrInvalidSession = &ApiError{StatusCode: http.StatusUnauthorized, ErrorText: "invalid session"}
	// ErrEncryptionError error message for bcrypt password encryption error
	ErrEncryptionError = &ApiError{StatusCode: http.StatusInternalServerError, ErrorText: "issue with email signup"}
	// ErrUserExists throws error if user already exists
	ErrUserExists = &ApiError{StatusCode: http.StatusConflict, ErrorText: "user already exists"}
)

Functions

func ByPassLogger

func ByPassLogger() func(next http.Handler) http.Handler

func Cors

func Cors() *cors.Cors

Cors middleware

func GetLogEntry

func GetLogEntry(r *http.Request) zerolog.Context

func IgnoreError

func IgnoreError(v ...interface{})

IgnoreError ignores error

func LogEntrySetField

func LogEntrySetField(r *http.Request, key string, value interface{})

func LogEntrySetFields

func LogEntrySetFields(r *http.Request, fields map[string]interface{})

func Render

func Render(w http.ResponseWriter, r *http.Request, v render.Renderer)

Types

type ApiError

type ApiError struct {
	Err error `json:"-"`

	StatusCode int    `json:"-"`
	StatusText string `json:"status"`

	Location  string      `json:"location,omitempty"`
	AppCode   int64       `json:"code,omitempty"`
	ErrorText string      `json:"error,omitempty"`
	Cause     string      `json:"cause,omitempty"`
	Data      interface{} `json:"data,omitempty"`
}

ApiError holds possible http api error fields

func ErrDatabase

func ErrDatabase(err error) *ApiError

ErrDatabase is error message for inability to perform database operation

func ErrInternalServerError

func ErrInternalServerError(err error) *ApiError

ErrInternalServerError is error message for Internal Server.

func ErrInvalidEmailSignup

func ErrInvalidEmailSignup(cause error) *ApiError

ErrInvalidEmailSignup shapes error message if the users email address is invalid

func ErrInvalidRequest

func ErrInvalidRequest(err error, data ...interface{}) *ApiError

ErrInvalidRequest is error message for bad request

func ErrPermission

func ErrPermission(err error) *ApiError

ErrPermission is error message for Unauthorized

func ErrRequestEntityTooLarge

func ErrRequestEntityTooLarge(err error) *ApiError

ErrRequestEntityTooLarge is error message for Request Entity Too Large

func ErrServiceUnavailable

func ErrServiceUnavailable(err error) *ApiError

ErrServiceUnavailable is error message for Service Unavailable

func ErrUnauthorized

func ErrUnauthorized(err error) *ApiError

ErrUnauthorized is error message for Unauthorized

func (*ApiError) Error

func (e *ApiError) Error() string

Error return an error text

func (*ApiError) Render

func (e *ApiError) Render(w http.ResponseWriter, r *http.Request) error

Render sends error message to the client

type ContextKey

type ContextKey struct {
	Name string
}

contextKey is a value for use with context.WithValue. It's used as a pointer so it fits in an interface{} without allocation. This technique for defining context keys was copied from Go 1.7's new use of context in net/http.

func (*ContextKey) String

func (k *ContextKey) String() string

type SeverityHook

type SeverityHook struct {
	AlertFn func(level zerolog.Level, msg string)
}

Run secondary functions based on severity.

func (SeverityHook) Run

func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string)

type Status

type Status struct {
	Code int
	Text string
}

Status customizes http response status

type StructuredLogger

type StructuredLogger struct {
	*zerolog.Logger
}

func (*StructuredLogger) NewLogEntry

func (z *StructuredLogger) NewLogEntry(r *http.Request) middleware.LogEntry

type StructuredLoggerEntry

type StructuredLoggerEntry struct {
	Logger zerolog.Context
}

func (*StructuredLoggerEntry) Panic

func (entry *StructuredLoggerEntry) Panic(v interface{}, stack []byte)

func (*StructuredLoggerEntry) Write

func (entry *StructuredLoggerEntry) Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{})

Jump to

Keyboard shortcuts

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