core

package
v0.0.0-...-d64eb31 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DbTypeUser = iota
	DbTypeAuth
	DbTypeStream
	DbTypeCategory
	DbTypeChannel
	DbTypeSystemSetting
)
View Source
const (
	KeyCompMySQL    = "mysql"
	KeyCompGIN      = "gin"
	KeyCompJWT      = "jwt"
	KeyCompConf     = "config"
	KeyCompRabbitMQ = "rabbitmq"
	KeyRedis        = "redis"
	KeyS3           = "s3"
)
View Source
const (
	TopicCreateChannel         = "TopicCreateChannel"
	TopicStreamCreate          = "TopicStreamCreate"
	TopicStreamStart           = "TopicStreamStart"
	TopicStreamEnded           = "TopicStreamEnded"
	TopicStreamError           = "TopicStreamError"
	TopicUpdateStreamViewCount = "TopicUpdateStreamViewCount"
	TopicUpdateUserName        = "TopicUpdateUserName"
	TopicForgotPassword        = "TopicForgotPassword"
	TopicUpdateChannelImage    = "TopicUpdateChannelImage"
)
View Source
const KeyRequester = "requester"
View Source
const StreamDomain = "stream"

Variables

View Source
var ErrBadRequest = DefaultError{
	StatusField: http.StatusText(http.StatusBadRequest),
	ErrorField:  "The request was malformed or contained invalid parameters",
	CodeField:   http.StatusBadRequest,
}
View Source
var ErrConflict = DefaultError{
	StatusField: http.StatusText(http.StatusConflict),
	ErrorField:  "The resource could not be created due to a conflict",
	CodeField:   http.StatusConflict,
}
View Source
var ErrForbidden = DefaultError{
	StatusField: http.StatusText(http.StatusForbidden),
	ErrorField:  "The requested action was forbidden",
	CodeField:   http.StatusForbidden,
}
View Source
var ErrInternalServerError = DefaultError{
	StatusField: http.StatusText(http.StatusInternalServerError),
	ErrorField:  "An internal server error occurred, please contact the system administrator",
	CodeField:   http.StatusInternalServerError,
}
View Source
var ErrInvalidInput = func(field string) DefaultError {
	return DefaultError{
		StatusField: http.StatusText(http.StatusBadRequest),
		ErrorField:  fmt.Sprintf("Invalid %s", field),
		CodeField:   http.StatusBadRequest,
	}
}
View Source
var ErrNotFound = DefaultError{
	StatusField: http.StatusText(http.StatusNotFound),
	ErrorField:  "The requested resource could not be found",
	CodeField:   http.StatusNotFound,
}
View Source
var ErrRecordNotFound = errors.New("record not found")

ErrRecordNotFound is used to make our application logic independent of other libraries errors

View Source
var ErrUnauthorized = DefaultError{
	StatusField: http.StatusText(http.StatusUnauthorized),
	ErrorField:  "The request could not be authorized",
	CodeField:   http.StatusUnauthorized,
}
View Source
var ErrUnsupportedMediaType = DefaultError{
	StatusField: http.StatusText(http.StatusUnsupportedMediaType),
	ErrorField:  "The request is using an unknown content type",
	CodeField:   http.StatusUnsupportedMediaType,
}
View Source
var Validator = &validate{
	validator.New(),
}

Functions

func AppRecover

func AppRecover()

AppRecover is an intelligent function to recover from panic

func ContextWithRequester

func ContextWithRequester(ctx context.Context, requester Requester) context.Context

func GenSalt

func GenSalt(length int) string

GenSalt function generates a random string of a specified length using the randSequence function. If the provided length is less than 0, it defaults to a length of 50. This function is typically used to generate a salt for cryptographic operations

func GetLocalIDFromBase58

func GetLocalIDFromBase58(s string) (int, error)

func GetServerAddress

func GetServerAddress() (string, error)

func NewConfig

func NewConfig() *config

func NewRequester

func NewRequester(sub, tid string) *requesterData

func NewSha256Hash

func NewSha256Hash() *sha256Hash

func ResponseData

func ResponseData(data interface{}) *successResponse

func SuccessResponse

func SuccessResponse(data, paging, extra interface{}) *successResponse

func WriteErrorResponse

func WriteErrorResponse(c *gin.Context, err error)

Types

type AppContext

type AppContext interface {
	GetLogger() srvctx.Logger
}

type BaseFilter

type BaseFilter struct {
	GtCreatedAt *time.Time `json:"gtCreatedAt,omitempty" form:"gtCreatedAt,omitempty"`
	GtUpdatedAt *time.Time `json:"gtUpdatedAt,omitempty" form:"gtUpdatedAt,omitempty"`
	LtCreatedAt *time.Time `json:"ltCreatedAt,omitempty" form:"ltCreatedAt,omitempty"`
	LtUpdatedAt *time.Time `json:"ltUpdatedAt,omitempty" form:"ltUpdatedAt,omitempty"`
}

type BaseModel

type BaseModel struct {
	Id        int        `json:"-" gorm:"column:id;" `
	Uid       *UID       `json:"id" gorm:"-"`
	CreatedAt *time.Time `json:"createdAt,omitempty" gorm:"column:created_at;" `
	UpdatedAt *time.Time `json:"updatedAt,omitempty" gorm:"column:updated_at;" `
}

func NewBaseModel

func NewBaseModel() BaseModel

func (*BaseModel) Mask

func (model *BaseModel) Mask(objectId int)

type Config

type Config interface {
	GetGRPCPort() int
	GetGRPCServerAddress() string

	GetGRPCUserAddress() string
	GetGRPCAuthAddress() string
	GetGRPCHlsAddress() string
	GetGRPCRtmpAddress() string
	GetGRPCAnalyticAddress() string
	GetGRPCCommunicationAddress() string
	GetGRPCVideoAddress() string
}

type DebugCarrier

type DebugCarrier interface {
	// Debug returns debugging information for the error, if applicable.
	Debug() string
}

DebugCarrier can be implemented by an error to support error contexts.

type DefaultError

type DefaultError struct {
	// The error ID
	//
	// Useful when trying to identify various errors in application logic.
	IDField string `json:"id,omitempty"`

	// The status code
	//
	// example: 404
	CodeField int `json:"code,omitempty"`

	// The status description
	//
	// example: Not Found
	StatusField string `json:"status,omitempty"`

	// The request ID
	//
	// The request ID is often exposed internally in order to trace
	// errors across service architectures. This is often a UUID.
	//
	// example: d7ef54b1-ec15-46e6-bccb-524b82c035e6
	RIDField string `json:"request,omitempty"`

	// A human-readable reason for the error
	//
	// example: User with ID 1234 does not exist.
	ReasonField string `json:"reason,omitempty"`

	// Debug information
	//
	// This field is often not exposed to protect against leaking
	// sensitive information.
	//
	// example: SQL field "foo" is not a bool.
	DebugField string `json:"debug,omitempty"`

	// Error message
	//
	// The error's message.
	//
	// example: The resource could not be found
	// required: true
	ErrorField string `json:"message"`

	// Further error details
	DetailsField map[string]interface{} `json:"details,omitempty"`
	// contains filtered or unexported fields
}

func ToDefaultError

func ToDefaultError(err error, requestID string) *DefaultError

func (DefaultError) Debug

func (e DefaultError) Debug() string

func (DefaultError) Details

func (e DefaultError) Details() map[string]interface{}

func (DefaultError) Error

func (e DefaultError) Error() string

func (DefaultError) Format

func (e DefaultError) Format(s fmt.State, verb rune)

func (DefaultError) ID

func (e DefaultError) ID() string

func (DefaultError) Is

func (e DefaultError) Is(err error) bool

func (DefaultError) Reason

func (e DefaultError) Reason() string

func (DefaultError) RequestID

func (e DefaultError) RequestID() string

func (*DefaultError) StackTrace

func (e *DefaultError) StackTrace() (trace errors.StackTrace)

StackTrace returns the error's stack trace.

func (DefaultError) Status

func (e DefaultError) Status() string

func (DefaultError) StatusCode

func (e DefaultError) StatusCode() int

func (DefaultError) String

func (e DefaultError) String() string

func (DefaultError) Unwrap

func (e DefaultError) Unwrap() error

func (DefaultError) WithDebug

func (e DefaultError) WithDebug(debug string) *DefaultError

func (DefaultError) WithDebugf

func (e DefaultError) WithDebugf(debug string, args ...interface{}) *DefaultError

func (DefaultError) WithDetail

func (e DefaultError) WithDetail(key string, detail interface{}) *DefaultError

func (DefaultError) WithDetailf

func (e DefaultError) WithDetailf(key string, message string, args ...interface{}) *DefaultError

func (DefaultError) WithError

func (e DefaultError) WithError(message string) *DefaultError

func (DefaultError) WithErrorf

func (e DefaultError) WithErrorf(message string, args ...interface{}) *DefaultError

func (DefaultError) WithID

func (e DefaultError) WithID(id string) *DefaultError

func (DefaultError) WithReason

func (e DefaultError) WithReason(reason string) *DefaultError

func (DefaultError) WithReasonf

func (e DefaultError) WithReasonf(reason string, args ...interface{}) *DefaultError

func (*DefaultError) WithTrace

func (e *DefaultError) WithTrace(err error) *DefaultError

func (DefaultError) WithWrap

func (e DefaultError) WithWrap(err error) *DefaultError

func (*DefaultError) Wrap

func (e *DefaultError) Wrap(err error)

type DetailsCarrier

type DetailsCarrier interface {
	// Details returns details on the error, if applicable.
	Details() map[string]interface{}
}

DetailsCarrier can be implemented by an error to support error contexts.

type File

type File struct {
	Id        int    `json:"id" gorm:"column:id"`
	Url       string `json:"url" gorm:"column:url"`
	CloudName string `json:"cloud_name,omitempty" gorm:"-"`
	Extension string `json:"extension,omitempty" gorm:"-"`
}

func (*File) Scan

func (j *File) Scan(value interface{}) error

Scan scan value into Jsonb, decode jsonb in db into struct implements sql.Scanner interface

func (File) TableName

func (File) TableName() string

func (*File) Value

func (j *File) Value() (driver.Value, error)

Value return json value; encode struct to []byte aka jsonb ;implement driver.Valuer interface

type Files

type Files []File

func (*Files) Scan

func (j *Files) Scan(value interface{}) error

func (*Files) Value

func (j *Files) Value() (driver.Value, error)

type GINComponent

type GINComponent interface {
	GetPort() int
	GetRouter() *gin.Engine
}

type GormComponent

type GormComponent interface {
	GetDB() *gorm.DB
}

type IDCarrier

type IDCarrier interface {
	// ID returns application error ID on the error, if applicable.
	ID() string
}

IDCarrier can be implemented by an error to support error contexts.

type Image

type Image struct {
	Id        int    `json:"id" gorm:"column:id"`
	Url       string `json:"url" gorm:"column:url"`
	Width     int    `json:"width" gorm:"column:width"`
	Height    int    `json:"height" gorm:"column:height"`
	CloudName string `json:"cloud_name,omitempty" gorm:"-"`
	Extension string `json:"extension,omitempty" gorm:"-"`
}

func (*Image) Scan

func (j *Image) Scan(value interface{}) error

Scan scan value into Jsonb, decode jsonb in db into struct implements sql.Scanner interface

func (Image) TableName

func (Image) TableName() string

func (*Image) Value

func (j *Image) Value() (driver.Value, error)

Value return json value; encode struct to []byte aka jsonb ;implement driver.Valuer interface

type Images

type Images []Image

func (*Images) Scan

func (j *Images) Scan(value interface{}) error

func (*Images) Value

func (j *Images) Value() (driver.Value, error)

type JWTProvider

type JWTProvider interface {
	IssueToken(ctx context.Context, id, sub string) (token string, expSecs int, err error)
	ParseToken(ctx context.Context, tokenString string) (claims *jwt.RegisteredClaims, err error)
}

type Paging

type Paging struct {
	Limit int   `json:"limit" form:"limit"`
	Page  int   `json:"page" form:"page"`
	Total int64 `json:"total" form:"-"`

	// Support cursor with UID
	FakeCursor string `json:"cursor" form:"cursor"`
	NextCursor string `json:"next_cursor"`
}

func (*Paging) GetOffSet

func (p *Paging) GetOffSet() int

func (*Paging) Process

func (p *Paging) Process()

type ReasonCarrier

type ReasonCarrier interface {
	// Reason returns the reason for the error, if applicable.
	Reason() string
}

ReasonCarrier can be implemented by an error to support error contexts.

type RedisComponent

type RedisComponent interface {
	GetClient() *redis.Client
}

type RequestIDCarrier

type RequestIDCarrier interface {
	// RequestID returns the ID of the request that caused the error, if applicable.
	RequestID() string
}

RequestIDCarrier can be implemented by an error to support error contexts.

type Requester

type Requester interface {
	GetSubject() string
	GetTokenId() string
	GetUserId() int
	GetRole() string
	SetRole(string)
}

func GetRequester

func GetRequester(ctx context.Context) Requester

type StatusCarrier

type StatusCarrier interface {
	// ID returns the error id, if applicable.
	Status() string
}

StatusCarrier can be implemented by an error to support error contexts.

type StatusCodeCarrier

type StatusCodeCarrier interface {
	// StatusCode returns the status code of this error.
	StatusCode() int
}

StatusCodeCarrier can be implemented by an error to support setting status codes in the error itself.

type StreamState

type StreamState struct {
	State     string `json:"state"`
	Uid       string `json:"id"`
	StreamKey string `json:"stream_key,omitempty"`
}

type UID

type UID struct {
	// contains filtered or unexported fields
}

UID is method to generate a virtual unique identifier for whole system its structure contains 62 bits: LocalID - ObjectType - ShareID 32 bits for Local ID, max (2^32) - 1 10 bits for Object Type 18 bits for Shard ID

func DecomposeUID

func DecomposeUID(s string) (UID, error)

func FromBase58

func FromBase58(s string) (UID, error)

func NewUID

func NewUID(localID uint32, objType int, shardID uint32) UID

func NewUIDP

func NewUIDP(localID uint32, objType int, shardID uint32) *UID

func (UID) GetLocalID

func (uid UID) GetLocalID() uint32

func (UID) GetObjectType

func (uid UID) GetObjectType() int

func (UID) GetShardID

func (uid UID) GetShardID() uint32

func (UID) MarshalJSON

func (uid UID) MarshalJSON() ([]byte, error)

func (*UID) Scan

func (uid *UID) Scan(value interface{}) error

func (UID) String

func (uid UID) String() string

func (*UID) UnmarshalJSON

func (uid *UID) UnmarshalJSON(data []byte) error

func (*UID) Value

func (uid *UID) Value() (driver.Value, error)

type UIDS

type UIDS []UID

func (UIDS) MarshalJSON

func (uids UIDS) MarshalJSON() ([]byte, error)

func (*UIDS) UnmarshalJSON

func (uids *UIDS) UnmarshalJSON(data []byte) error

Jump to

Keyboard shortcuts

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