Documentation
¶
Index ¶
- func ErrorJson(w http.ResponseWriter, err error)
- func LoadConfig[I any](path string) (I, error)
- func NewHTTPError(message, detail string, code int) error
- func NewPostgresPool(dsn string, waitTime time.Duration) *pgxpool.Pool
- func ReadJson[I any](w http.ResponseWriter, r *http.Request) (I, error)
- func ReadJsonAndValidate[I any](w http.ResponseWriter, r *http.Request, ...) (I, error)
- func SetMaxBytes(mb int)
- func ValidateRequest(requestPayload any, ...) error
- func WriteJson(w http.ResponseWriter, status int, data any, headers ...http.Header)
- type ChiController
- type HTTPError
- type JWTMaker
- func (j JWTMaker[P]) CreateToken(payload P, tokenType TokenType) (string, P, error)
- func (j JWTMaker[P]) Middleware(store *sessions.CookieStore, sessionName string, tokenType TokenType) func(http.Handler) http.Handler
- func (j JWTMaker[P]) TokenFromRequest(request *http.Request, tokenType TokenType) (P, error)
- func (j JWTMaker[P]) VerifyToken(s string, tokenType TokenType) (P, error)
- type JWTPayload
- func (j JWTPayload) GetAudience() (jwt.ClaimStrings, error)
- func (j JWTPayload) GetExpirationTime() (*jwt.NumericDate, error)
- func (j JWTPayload) GetIssuedAt() (*jwt.NumericDate, error)
- func (j JWTPayload) GetIssuer() (string, error)
- func (j JWTPayload) GetNotBefore() (*jwt.NumericDate, error)
- func (j JWTPayload) GetSubject() (string, error)
- type PasetoMaker
- func (p PasetoMaker[P]) CreateToken(payload P, tokenType TokenType) (string, P, error)
- func (p PasetoMaker[P]) Middleware(store *sessions.CookieStore, sessionName string, tokenType TokenType) func(http.Handler) http.Handler
- func (p PasetoMaker[P]) TokenFromRequest(request *http.Request, tokenType TokenType) (P, error)
- func (p PasetoMaker[P]) VerifyToken(s string, tokenType TokenType) (P, error)
- type TokenMaker
- type TokenPayload
- type TokenType
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 NewHTTPError ¶
NewHTTPError registers a new HTTP error with its message, detail and code and returns the error
func NewPostgresPool ¶ added in v0.0.5
NewPostgresPool creates a new postgres connection pool.
func ReadJson ¶
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.
Types ¶
type ChiController ¶ added in v0.0.5
type ChiController interface {
Routes() *chi.Mux
}
ChiController is an interface for Chi controllers.
type HTTPError ¶
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 (JWTMaker[P]) Middleware ¶ added in v0.1.1
func (JWTMaker[P]) TokenFromRequest ¶ added in v0.1.1
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]