helpers

package module
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2023 License: MIT Imports: 22 Imported by: 5

README

gofiber-helpers

Helpers for GO Fiber

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckPasswordHash

func CheckPasswordHash(password, hash []byte) (err error)

CheckPasswordHash with bcrypt

func CheckPasswordHashString

func CheckPasswordHashString(password, hash string) (err error)

CheckPasswordHash with bcrypt

func DateStrTotime

func DateStrTotime(dateStr string) (result time.Time, err error)

func ENVJSONArray

func ENVJSONArray(name string) (value []interface{}, err error)

func ENVJSONObj

func ENVJSONObj(name string) (value map[string]interface{}, err error)

func FileNameWithoutExtension

func FileNameWithoutExtension(fileName string) string

func GetStackTrace

func GetStackTrace() (trace []string)

GetStackTrace prints the stack trace with given error by the formatter. If the error is not traceable, empty string is returned.

func HashPassword

func HashPassword(password []byte) ([]byte, error)

HashPassword with bcrypt

func HashPasswordString

func HashPasswordString(password string) (string, error)

HashPassword with bcrypt

func InitSnowflake

func InitSnowflake(machineIDs ...int) (snowflakeGen *snowflake.Generator)

InitSnowflake machine id base on process id

func RandomHash

func RandomHash() (string, error)

RandomHash with sha3-256

func RandomKey

func RandomKey() (string, error)

RandomKey with crc64(UUIDv4)

func RemoveIndex

func RemoveIndex(s []string, index int) []string

RemoveIndex from array

func StructCheckSumSha256

func StructCheckSumSha256(obj interface{}, checksum string) error

func StructSumSha256

func StructSumSha256(obj interface{}) string

func TimeToDGADate

func TimeToDGADate(dateTime time.Time) (dgaDate string)

func UUIDv4

func UUIDv4() (uuidStr string, err error)

func ValidCID

func ValidCID(cid string) (result bool, err error)

func WhereAmI

func WhereAmI(skipList ...int) string

WhereAmI return current path and line

Types

type AuthType

type AuthType string
const (
	BasicAuth   AuthType = "basic"
	BearerToken AuthType = "bearer"
)

type Ctx

type Ctx struct {
	*fiber.Ctx
}

Ctx represents the context of the current HTTP request. It holds request and response objects, path, path parameters, data and registered handler.

func (*Ctx) BasicAuth

func (c *Ctx) BasicAuth(user, passwd string) error

func (*Ctx) FormValueArray

func (c *Ctx) FormValueArray(name string, sep ...string) (result []string)

FormValueInt returns the form field value for the provided name, as string array.

If not found returns empty array.

func (*Ctx) FormValueBase64

func (c *Ctx) FormValueBase64(name string) string

FormValueBase64 returns the form field value for the provided name.

If value encoded with base64 return will be decoded string.

func (*Ctx) FormValueBool

func (c *Ctx) FormValueBool(name string) bool

FormValueBool returns the form field value for the provided name, as bool.

If not found or value is false, then it returns true, otherwise false.

func (*Ctx) FormValueDate

func (c *Ctx) FormValueDate(name string) time.Time

FormValueDate returns the form field date value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

func (*Ctx) FormValueDateTime

func (c *Ctx) FormValueDateTime(name string) time.Time

FormValueDateTime returns the form field datetime-local value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local

func (*Ctx) FormValueFloat64

func (c *Ctx) FormValueFloat64(name string) (float64, error)

FormValueFloat64 returns the form field value for the provided name, as float64.

If not found returns 0 and a non-nil error.

func (*Ctx) FormValueFloat64Default

func (c *Ctx) FormValueFloat64Default(name string, def float64) float64

FormValueFloat64Default returns the form field value for the provided name, as float64.

If not found or parse errors returns the "def".

func (*Ctx) FormValueInt

func (c *Ctx) FormValueInt(name string) (int, error)

FormValueInt returns the form field value for the provided name, as int.

If not found returns 0 and a non-nil error.

func (*Ctx) FormValueInt64

func (c *Ctx) FormValueInt64(name string) (int64, error)

FormValueInt64 returns the form field value for the provided name, as float64.

If not found returns 0 and a no-nil error.

func (*Ctx) FormValueInt64Default

func (c *Ctx) FormValueInt64Default(name string, def int64) int64

FormValueInt64Default returns the form field value for the provided name, as int64.

If not found or parse errors returns the "def".

func (*Ctx) FormValueIntDefault

func (c *Ctx) FormValueIntDefault(name string, def int) int

FormValueIntDefault returns the form field value for the provided name, as int.

If not found returns or parse errors the "def".

func (*Ctx) FormValueTime

func (c *Ctx) FormValueTime(name string) time.Time

FormValueTime returns the form field time value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time

func (*Ctx) FormValueTrim

func (c *Ctx) FormValueTrim(name string) string

FormValueTrim returns the form field value for the provided name, without trailing spaces.

func (*Ctx) ParamBase64

func (c *Ctx) ParamBase64(name string) string

ParamBase64 returns path parameter by name.

If value encoded with base64 return will be decoded string.

func (*Ctx) ParamBool

func (c *Ctx) ParamBool(name string) bool

ParamBool returns path parameter by name, as bool.

If not found or value is false, then it returns true, otherwise false.

func (*Ctx) ParamDate

func (c *Ctx) ParamDate(name string) time.Time

ParamDate returns the form field date value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

func (*Ctx) ParamDateTime

func (c *Ctx) ParamDateTime(name string) time.Time

ParamDateTime returns the form field datetime-local value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/datetime-local

func (*Ctx) ParamFloat64

func (c *Ctx) ParamFloat64(name string) (float64, error)

ParamFloat64 returns path parameter by name, as float64.

If not found returns 0 and a non-nil error.

func (*Ctx) ParamFloat64Default

func (c *Ctx) ParamFloat64Default(name string, def float64) float64

ParamFloat64Default returns path parameter by name, as float64.

If not found or parse errors returns the "def".

func (*Ctx) ParamInt

func (c *Ctx) ParamInt(name string) (int, error)

ParamInt returns path parameter by name, as int.

If not found returns 0 and a non-nil error.

func (*Ctx) ParamInt64

func (c *Ctx) ParamInt64(name string) (int64, error)

ParamInt64 returns path parameter by name, as float64.

If not found returns 0 and a no-nil error.

func (*Ctx) ParamInt64Default

func (c *Ctx) ParamInt64Default(name string, def int64) int64

ParamInt64Default returns path parameter by name, as int64.

If not found or parse errors returns the "def".

func (*Ctx) ParamIntDefault

func (c *Ctx) ParamIntDefault(name string, def int) int

ParamIntDefault returns path parameter by name, as int.

If not found returns or parse errors the "def".

func (*Ctx) ParamTime

func (c *Ctx) ParamTime(name string) time.Time

ParamTime returns the form field time value for the provided name. https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/time

func (*Ctx) ParamTrim

func (c *Ctx) ParamTrim(name string) string

ParamTrim returns path parameter by name, without trailing spaces.

type Error added in v0.1.1

type Error struct {
	Code    int         `json:"code"`
	Source  interface{} `json:"source,omitempty"`
	Title   string      `json:"title,omitempty"`
	Message string      `json:"message,omitempty"`
}

Error represents an error that occurred while handling a request.

func NewError added in v0.1.1

func NewError(code int, message ...string) (err *Error)

func NewErrorSource added in v0.1.7

func NewErrorSource(code int, source string, message ...string) (err *Error)

func (*Error) Error added in v0.1.1

func (e *Error) Error() (errStr string)

func (*Error) Log added in v0.1.7

func (e *Error) Log()

type GrantType

type GrantType string

GrantType Oauth grant type

const (
	GrantTypeCode     GrantType = "authorization_code"
	GrantTypeClient   GrantType = "client_credentials"
	GrantTypeRefresh  GrantType = "refresh_token"
	GrantTypeaccess   GrantType = "access_token"
	GrantTypePassword GrantType = "password"
)

GrantType constant

type HttpAuth

type HttpAuth struct {
	Type     AuthType `json:"auth_type"`
	Token    string   `json:"token,omitempty"`
	Username string   `json:"username,omitempty"`
	Password string   `json:"password,omitempty"`
}

func ExtractAuthString

func ExtractAuthString(authStr string) (auth HttpAuth, err error)

type Mail

type Mail struct {
	Host           string
	Port           int
	User           string
	Password       string
	From           string
	To             []string
	Cc             []string
	Bcc            []string
	Subject        string
	Msg            []byte
	AttachmentName string
	Attachment     []byte
}

func (Mail) Builder

func (m Mail) Builder() (body []byte)

func (Mail) Send

func (m Mail) Send() (err error)

type OauthErr

type OauthErr string

OauthErr Oauth error response

const (
	InvalidRequest     OauthErr = "invalid_request"
	UnauthorizedClient OauthErr = "unauthorized_client"
	AccessDenied       OauthErr = "access_denied"
	UnsupportType      OauthErr = "unsupported_response_type"
	InvalidScope       OauthErr = "invalid_scope"
	ServerError        OauthErr = "server_error"
	Unavailable        OauthErr = "temporarily_unavailable"
)

Oauth Error Response

type OauthRequest

type OauthRequest struct {
	APIKey       string `json:"client_id" form:"client_id" query:"client_id"`
	APISecret    string `json:"client_secret" form:"client_secret" query:"client_secret"`
	ResponseType string `json:"response_type" form:"response_type" query:"response_type"`
	RedirectURI  string `json:"redirect_uri" form:"redirect_uri" query:"redirect_uri"`
	Scope        string `json:"scope" form:"scope" query:"scope"`
	State        string `json:"state" form:"state" query:"state"`
	Code         string `json:"code" form:"code" query:"code"`
	GrantType    string `json:"grant_type" form:"grant_type" query:"grant_type"`
	UserName     string `json:"username" form:"username" query:"username"`
	Password     string `json:"password" form:"password" query:"password"`
	RefreshToken string `json:"refresh_token" form:"refresh_token" query:"refresh_token"`
	Token        string `json:"token" form:"token" query:"token"`
}

OauthRequest oauth request by IETF

type OauthResponse

type OauthResponse struct {
	Scope        string      `json:"scope,omitempty"`
	State        string      `json:"state,omitempty"`
	Code         string      `json:"code,omitempty"`
	Error        OauthErr    `json:"error,omitempty"`
	ErrorDesc    string      `json:"error_description,omitempty"`
	ErrorURI     string      `json:"error_uri,omitempty"`
	AccessToken  string      `json:"access_token,omitempty"`
	IDToken      string      `json:"id_token,omitempty"`
	TokenType    string      `json:"token_type,omitempty"`
	ExpiresIn    int         `json:"expires_in,omitempty"`
	RefreshToken string      `json:"refresh_token,omitempty"`
	Data         interface{} `json:"data,omitempty"`
}

OauthResponse oauth request by IETF

type ResponseError added in v0.1.1

type ResponseError Error

ResponseError backward complatible Error

type ResponseForm

type ResponseForm struct {
	Success    bool            `json:"success"`
	Result     interface{}     `json:"result,omitempty"`
	Data       interface{}     `json:"data,omitempty"`
	Messages   []string        `json:"messages,omitempty"`
	Errors     []ResponseError `json:"errors,omitempty"`
	ResultInfo *ResultInfo     `json:"result_info,omitempty"`
}

type ResponseType

type ResponseType string

ResponseType Oauth response type

const (
	ResponseTypeCode  ResponseType = "code"
	ResponseTypeToken ResponseType = "token"
)

Responsetype constant

type ResultInfo

type ResultInfo struct {
	Page      int `json:"page"`
	PerPage   int `json:"per_page"`
	Count     int `json:"count"`
	TotalCont int `json:"total_count"`
}

Jump to

Keyboard shortcuts

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