Documentation
¶
Index ¶
- Constants
- Variables
- func Decode(r *http.Request, outPtr Validator) error
- func DecodeBody(r io.ReadCloser, outPtr Validator) (string, error)
- func DecodeQuery(r *http.Request, outPtr Validator) error
- func Empty() map[string]interface{}
- func GetModelIDSet[T WithModel](ms []T) mapset.Set[uint]
- func GetModelIDs[T WithModel](ms []T) []uint
- func HTTPFullLogger(next http.Handler) http.Handler
- func HandleErr(h HandlerFuncErr) http.HandlerFunc
- func IndexMap[T comparable](collection []T) map[T]int
- func ReadBearerToken(r *http.Request) string
- func SetDefaultPageLimit(n int)
- func SetLogger(log Logger)
- func SetMaxPageLimit(n int)
- func SimpleFetch[T Entity](ctx context.Context, f GetDB, query ModelQuery) ([]T, int64, error)
- func SimpleQuery[T any, U any, F GetDB](r *http.Request, f F) ([]T, int64, error)
- func SortAs[T comparable, U any](collection []U, indexes []T, keyFn func(U) T)
- func WriteError(w http.ResponseWriter, err error)
- func WriteJSON(w http.ResponseWriter, v interface{}) error
- type AuthParser
- type Authenticator
- func (a *Authenticator[T]) GetUserFromContext(ctx context.Context) (T, bool)
- func (a *Authenticator[T]) LoginRequired() func(next http.Handler) http.Handler
- func (a *Authenticator[T]) WithLoginContext() func(next http.Handler) http.Handler
- func (a *Authenticator[T]) WithUserCheck(f func(u T) bool) func(next http.Handler) http.Handler
- type Clauses
- type Entity
- type ErrCode
- type ErrData
- type ErrMessage
- type ErrStatusCode
- type GetDB
- type HTTPError
- type HandlerFuncErr
- type JWTAuthParser
- func (a *JWTAuthParser[T]) JWTParse(token string) (T, error)
- func (a *JWTAuthParser[T]) JWTSign(user T) (string, error)
- func (a *JWTAuthParser[T]) LoginRequired(readUserCtxFn func(ctx context.Context) (user T, err error), ...) func(next http.Handler) http.Handler
- func (a *JWTAuthParser[T]) OnAuth(setUserCtxFn func(ctx context.Context, user T, err error) context.Context) func(next http.Handler) http.Handler
- func (a *JWTAuthParser[T]) ParseFromRequest(r *http.Request) (T, error)
- type Logger
- type Model
- type ModelQuery
- type NoOrder
- type OffsetLimit
- type OffsetLimiter
- type OrderBy
- type OrderByID
- type PageAndPageSize
- type PagingMeta
- type StandList
- type Validator
- type WithModel
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) ErrForbidden = NewHTTPErrorMsg("forbidden", http.StatusForbidden) ErrSimpleQueryInvalid = NewHTTPErrorMsg("query struct invalid", http.StatusInternalServerError) )
View Source
var StructQueryer = structquery.NewQueryer()
Functions ¶
func DecodeBody ¶
func DecodeBody(r io.ReadCloser, outPtr Validator) (string, error)
func GetModelIDs ¶
func HandleErr ¶
func HandleErr(h HandlerFuncErr) http.HandlerFunc
func IndexMap ¶
func IndexMap[T comparable](collection []T) map[T]int
func ReadBearerToken ¶
func SetDefaultPageLimit ¶
func SetDefaultPageLimit(n int)
func SetMaxPageLimit ¶
func SetMaxPageLimit(n int)
func SimpleFetch ¶
func SimpleQuery ¶
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 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 ¶
type Clauses ¶
type Clauses interface {
Clauses() clause.Expression
}
type ErrMessage ¶
type ErrMessage interface{ ErrMessage() string } // user message
type ErrStatusCode ¶
type ErrStatusCode interface{ ErrStatusCode() int } // HTTP Code
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 NewHTTPError200 ¶
func NewHTTPErrorMsg ¶
func NewHTTPErrorStatusCode ¶
func (*HTTPError) ErrMessage ¶
func (*HTTPError) ErrStatusCode ¶
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 (*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]) LoginRequired ¶
func (*JWTAuthParser[T]) ParseFromRequest ¶
func (a *JWTAuthParser[T]) ParseFromRequest(r *http.Request) (T, error)
type Model ¶
type Model struct { ID uint `json:"id" gorm:"primarykey"` CreatedAt time.Time `json:"created_at" gorm:"<-:create"` UpdatedAt time.Time `json:"updated_at" gorm:""` DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index"` }
func (Model) GetModelID ¶
type ModelQuery ¶
type ModelQuery interface { Clauses OrderBy OffsetLimiter }
type NoOrder ¶
type NoOrder struct{}
func (NoOrder) OrderBy ¶
func (NoOrder) OrderBy() []clause.OrderByColumn
type OffsetLimit ¶
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 StandList ¶
type StandList[T any] struct { Data []T `json:"data"` Paging *PagingMeta `json:"paging"` }
func NewAllDataList ¶
func NewDataList ¶
func NewDataList[T any](data []T, totals int, off OffsetLimiter) *StandList[T]
type Validator ¶
type Validator interface {
Validate() error
}
func JoinValidator ¶
func NewValidator ¶
Click to show internal directories.
Click to hide internal directories.