apiutil

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorJson

func ErrorJson(w http.ResponseWriter, err error)

ErrorJson writes a json response with the specified error. A non HTTPError type will follow:

  • Status: 400
  • Message: "bad request"
  • Detail: error.Error()

func LoadConfig added in v0.0.3

func LoadConfig[I any](path string) (I, error)

func NewHTTPError

func NewHTTPError(message, detail string, code int) error

NewHTTPError registers a new HTTP error with its message, detail and code and returns the error

func NewPostgresPool added in v0.0.5

func NewPostgresPool(dsn string, waitTime time.Duration) *pgxpool.Pool

NewPostgresPool creates a new postgres connection pool.

func ReadJson

func ReadJson[I any](w http.ResponseWriter, r *http.Request) (I, error)

ReadJson reads a json request body into the specified struct. The maximum read bytes is defined by `maxBytes`.

func ReadJsonAndValidate added in v0.0.2

func ReadJsonAndValidate[I any](w http.ResponseWriter, r *http.Request, customValidatorsSlice ...map[string]func(p validator.FieldLevel) bool) (I, error)

ReadJsonAndValidate reads a json request body into the specified struct and validates it. The maximum read bytes is defined by `maxBytes`. The customValidators parameter is a map of field name to custom validation function.

func SetMaxBytes

func SetMaxBytes(mb int)

SetMaxBytes sets the maximum number of bytes allowed in the request body. Default is 10485760.

func ValidateRequest added in v0.0.2

func ValidateRequest(requestPayload any, customValidatorsSlice ...map[string]func(level validator.FieldLevel) bool) error

ValidateRequest validates the request payload using the Go Playground Validator. Use the `validate` tag on the struct fields to specify the validation rules. The customValidators map can be used to register custom validation rules.

func WriteJson

func WriteJson(w http.ResponseWriter, status int, data any, headers ...http.Header)

WriteJson writes a json response with the specified status and data.

Types

type ChiController added in v0.0.5

type ChiController interface {
	Routes() *chi.Mux
}

ChiController is an interface for Chi controllers.

type HTTPError

type HTTPError struct {
	Message string `json:"message"`
	Detail  string `json:"detail"`
	Status  int    `json:"status"`
}

func (HTTPError) Error

func (e HTTPError) Error() string

type JWTMaker added in v0.0.7

type JWTMaker[P TokenPayload] struct {
	// contains filtered or unexported fields
}

func (JWTMaker[P]) CreateToken added in v0.0.7

func (j JWTMaker[P]) CreateToken(payload P, tokenType TokenType) (string, P, error)

func (JWTMaker[P]) Middleware added in v0.1.1

func (j JWTMaker[P]) Middleware(store *sessions.CookieStore, sessionName string, tokenType TokenType) func(http.Handler) http.Handler

func (JWTMaker[P]) TokenFromRequest added in v0.1.1

func (j JWTMaker[P]) TokenFromRequest(request *http.Request, tokenType TokenType) (P, error)

func (JWTMaker[P]) VerifyToken added in v0.0.7

func (j JWTMaker[P]) VerifyToken(s string, tokenType TokenType) (P, error)

type JWTPayload added in v0.0.7

type JWTPayload struct {
	jwt.Claims
	MarshalledPayload string `json:"payload"`
	IssuedAt          int64  `json:"iat"`
	ExpiresAt         int64  `json:"exp"`
}

func (JWTPayload) GetAudience added in v0.0.7

func (j JWTPayload) GetAudience() (jwt.ClaimStrings, error)

func (JWTPayload) GetExpirationTime added in v0.0.7

func (j JWTPayload) GetExpirationTime() (*jwt.NumericDate, error)

func (JWTPayload) GetIssuedAt added in v0.0.7

func (j JWTPayload) GetIssuedAt() (*jwt.NumericDate, error)

func (JWTPayload) GetIssuer added in v0.0.7

func (j JWTPayload) GetIssuer() (string, error)

func (JWTPayload) GetNotBefore added in v0.0.7

func (j JWTPayload) GetNotBefore() (*jwt.NumericDate, error)

func (JWTPayload) GetSubject added in v0.0.7

func (j JWTPayload) GetSubject() (string, error)

type PasetoMaker added in v0.0.7

type PasetoMaker[P TokenPayload] struct {
	// contains filtered or unexported fields
}

func (PasetoMaker[P]) CreateToken added in v0.0.7

func (p PasetoMaker[P]) CreateToken(payload P, tokenType TokenType) (string, P, error)

func (PasetoMaker[P]) Middleware added in v0.1.1

func (p PasetoMaker[P]) Middleware(store *sessions.CookieStore, sessionName string, tokenType TokenType) func(http.Handler) http.Handler

func (PasetoMaker[P]) TokenFromRequest added in v0.1.1

func (p PasetoMaker[P]) TokenFromRequest(request *http.Request, tokenType TokenType) (P, error)

func (PasetoMaker[P]) VerifyToken added in v0.0.7

func (p PasetoMaker[P]) VerifyToken(s string, tokenType TokenType) (P, error)

type TokenMaker added in v0.0.8

type TokenMaker[P TokenPayload] interface {
	CreateToken(P, TokenType) (string, P, error)
	VerifyToken(string, TokenType) (P, error)
	Middleware(*sessions.CookieStore, string, TokenType) func(http.Handler) http.Handler
	TokenFromRequest(*http.Request, TokenType) (P, error)
}

func NewJWTMaker added in v0.0.7

func NewJWTMaker[P TokenPayload](durations map[TokenType]time.Duration, errInvalidToken, errExpiredToken, errInvalidCookie error, secret string) TokenMaker[P]

func NewPasetoMaker added in v0.0.7

func NewPasetoMaker[P TokenPayload](durations map[TokenType]time.Duration, errInvalidToken, errExpiredToken, errInvalidCookie error) TokenMaker[P]

type TokenPayload added in v0.0.8

type TokenPayload interface {
	GetIssuedAt() time.Time
	GetExpiresAt() time.Time
	GetType() TokenType
	SetIssuedAt(time.Time)
	SetExpiresAt(time.Time)
	SetType(TokenType)
}

type TokenType added in v0.0.7

type TokenType int

Jump to

Keyboard shortcuts

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