blia

package module
v0.0.0-...-6c0fe8b Latest Latest
Warning

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

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

README

blia

Documentation

Index

Constants

View Source
const (
	AuthorizationKey    = "Authorization"
	AuthorizationBearer = "Bearer"
)

Variables

View Source
var (
	ErrJwtTokenParseFailed = errors.New("jwt token prase failed")
	ErrJwtTokenInvalidAlg  = errors.New("jwt token prase with invalid alg")
)
View Source
var (
	ErrDecodeBodyFailed   = NewHTTPErrorMsg("decode body failed", http.StatusBadRequest)
	ErrDecodeQueryFailed  = NewHTTPErrorMsg("decode query failed", http.StatusBadRequest)
	ErrInvalidOffsetLimit = NewHTTPErrorMsg("invalid offset,limit or page,page_size", http.StatusBadRequest)
	ErrDataEmpty          = NewHTTPErrorMsg("data is empty", http.StatusBadRequest)
	ErrDataNotFound       = NewHTTPErrorMsg("data not found", http.StatusNotFound)
	ErrUnauthorized       = NewHTTPErrorMsg("unauthorized", http.StatusUnauthorized)
	ErrForbidden          = NewHTTPErrorMsg("forbidden", http.StatusForbidden)
	ErrSimpleQueryInvalid = NewHTTPErrorMsg("query struct invalid", http.StatusInternalServerError)
)
View Source
var StructQueryer = structquery.NewQueryer()

Functions

func Decode

func Decode(r *http.Request, outPtr Validator) error

func DecodeBody

func DecodeBody(r io.ReadCloser, outPtr Validator) (string, error)

func DecodeQuery

func DecodeQuery(r *http.Request, outPtr Validator) error

func Empty

func Empty() map[string]interface{}

func HTTPFullLogger

func HTTPFullLogger(next http.Handler) http.Handler

func HandleErr

func HandleErr(h HandlerFuncErr) http.HandlerFunc

func IndexMap

func IndexMap[T comparable](collection []T) map[T]int

func ReadBearerToken

func ReadBearerToken(r *http.Request) string

func SetDefaultPageLimit

func SetDefaultPageLimit(n int)

func SetLogger

func SetLogger(log Logger)

func SetMaxPageLimit

func SetMaxPageLimit(n int)

func SimpleFetch

func SimpleFetch[T Entity](ctx context.Context, f GetDB, query ModelQuery) ([]T, int64, error)

func SimpleQuery

func SimpleQuery[T any, U any, F GetDB](r *http.Request, f F) ([]T, int64, error)

func SortAs

func SortAs[T comparable, U any](collection []U, indexes []T, keyFn func(U) T)

func WriteError

func WriteError(w http.ResponseWriter, err error)

func WriteJSON

func WriteJSON(w http.ResponseWriter, v interface{}) error

Types

type AuthParser

type AuthParser[T any] interface {
	ParseFromRequest(r *http.Request) (T, error)
}

type Authenticator

type Authenticator[T any] struct {
	// contains filtered or unexported fields
}

func NewAuthenticator

func NewAuthenticator[T any](parser AuthParser[T]) *Authenticator[T]

func (*Authenticator[T]) GetUserFromContext

func (a *Authenticator[T]) GetUserFromContext(ctx context.Context) (T, bool)

func (*Authenticator[T]) LoginRequired

func (a *Authenticator[T]) LoginRequired() func(next http.Handler) http.Handler

func (*Authenticator[T]) WithLoginContext

func (a *Authenticator[T]) WithLoginContext() func(next http.Handler) http.Handler

func (*Authenticator[T]) WithUserCheck

func (a *Authenticator[T]) WithUserCheck(f func(u T) bool) func(next http.Handler) http.Handler

type Clauses

type Clauses interface {
	Clauses() clause.Expression
}

type Entity

type Entity interface {
	TableName() string
}

type ErrCode

type ErrCode interface{ ErrCode() int } // error code

type ErrData

type ErrData interface{ ErrData() interface{} } // other data

type ErrMessage

type ErrMessage interface{ ErrMessage() string } // user message

type ErrStatusCode

type ErrStatusCode interface{ ErrStatusCode() int } // HTTP Code

type GetDB

type GetDB func() *gorm.DB

type HTTPError

type HTTPError struct {
	Message    string      `json:"message"`
	Code       int         `json:"code"`
	Data       interface{} `json:"data,omitempty"`
	StatusCode int         `json:"-"`
	Err        error       `json:"-"`
}

func NewHTTPError

func NewHTTPError(err error) *HTTPError

func NewHTTPError200

func NewHTTPError200(err error) *HTTPError

func NewHTTPErrorMsg

func NewHTTPErrorMsg(msg string, status int) *HTTPError

func NewHTTPErrorStatusCode

func NewHTTPErrorStatusCode(err error, status int) *HTTPError

func (*HTTPError) ErrCode

func (s *HTTPError) ErrCode() int

func (*HTTPError) ErrMessage

func (s *HTTPError) ErrMessage() string

func (*HTTPError) ErrStatusCode

func (s *HTTPError) ErrStatusCode() int

func (*HTTPError) Error

func (s *HTTPError) Error() string

type HandlerFuncErr

type HandlerFuncErr func(w http.ResponseWriter, r *http.Request) error

type JWTAuthParser

type JWTAuthParser[T any] struct {
	// contains filtered or unexported fields
}

func NewJWTAuthParser

func NewJWTAuthParser[T any](secert string, expire time.Duration, oldSecerts ...string) *JWTAuthParser[T]

func (*JWTAuthParser[T]) JWTParse

func (a *JWTAuthParser[T]) JWTParse(token string) (T, error)

func (*JWTAuthParser[T]) JWTSign

func (a *JWTAuthParser[T]) JWTSign(user T) (string, error)

func (*JWTAuthParser[T]) ParseFromRequest

func (a *JWTAuthParser[T]) ParseFromRequest(r *http.Request) (T, error)

type Logger

type Logger interface {
	Info(context.Context, string, ...interface{})
	Warn(context.Context, string, ...interface{})
	Error(context.Context, string, ...interface{})
}

type ModelQuery

type ModelQuery interface {
	Clauses
	OrderBy
	OffsetLimiter
}

type NoOrder

type NoOrder struct{}

func (NoOrder) OrderBy

func (NoOrder) OrderBy() []clause.OrderByColumn

type OffsetLimit

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

func (OffsetLimit) ToOffsetLimit

func (o OffsetLimit) ToOffsetLimit() OffsetLimit

func (*OffsetLimit) Validate

func (o *OffsetLimit) Validate() error

type OffsetLimiter

type OffsetLimiter interface {
	ToOffsetLimit() OffsetLimit
}

type OrderBy

type OrderBy interface {
	OrderBy() []clause.OrderByColumn
}

type OrderByID

type OrderByID struct{}

func (OrderByID) OrderBy

func (OrderByID) OrderBy() []clause.OrderByColumn

type PageAndPageSize

type PageAndPageSize struct {
	OffsetLimit OffsetLimit `json:"-"`
	Page        int         `json:"page"`
	PageSize    int         `json:"page_size"`
}

func (PageAndPageSize) ToOffsetLimit

func (o PageAndPageSize) ToOffsetLimit() OffsetLimit

func (*PageAndPageSize) Validate

func (o *PageAndPageSize) Validate() error

type PagingMeta

type PagingMeta struct {
	Type   string `json:"type"`
	IsEnd  bool   `json:"is_end"`
	Totals int    `json:"totals"`
}

type StandList

type StandList[T any] struct {
	Data   []T         `json:"data"`
	Paging *PagingMeta `json:"paging"`
}

func NewAllDataList

func NewAllDataList[T any](data []T) *StandList[T]

func NewDataList

func NewDataList[T any](data []T, totals int, off OffsetLimiter) *StandList[T]

type Validator

type Validator interface {
	Validate() error
}

func JoinValidator

func JoinValidator(validators ...Validator) Validator

func NewValidator

func NewValidator(f func() error) Validator

Jump to

Keyboard shortcuts

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