core

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2023 License: Apache-2.0 Imports: 24 Imported by: 29

Documentation

Index

Constants

View Source
const KeyRequester = "requester"

Variables

View Source
var ErrBadRequest = DefaultError{
	StatusField:   http.StatusText(http.StatusBadRequest),
	ErrorField:    "The request was invalid or contained malformed parameters",
	CodeField:     http.StatusBadRequest,
	GRPCCodeField: codes.InvalidArgument,
}
View Source
var ErrConflict = DefaultError{
	StatusField:   http.StatusText(http.StatusConflict),
	ErrorField:    "A conflict occurred with the current state of the resource",
	CodeField:     http.StatusConflict,
	GRPCCodeField: codes.FailedPrecondition,
}
View Source
var ErrForbidden = DefaultError{
	StatusField:   http.StatusText(http.StatusForbidden),
	ErrorField:    "Access to the requested page or resource is forbidden",
	CodeField:     http.StatusForbidden,
	GRPCCodeField: codes.PermissionDenied,
}
View Source
var ErrInternalServerError = DefaultError{
	StatusField:   http.StatusText(http.StatusInternalServerError),
	ErrorField:    "An internal server error occurred. Please try again later",
	CodeField:     http.StatusInternalServerError,
	GRPCCodeField: codes.Internal,
}
View Source
var ErrNotFound = DefaultError{
	StatusField:   http.StatusText(http.StatusNotFound),
	ErrorField:    "The requested page or resource could not be found",
	CodeField:     http.StatusNotFound,
	GRPCCodeField: codes.NotFound,
}
View Source
var ErrTimeout = DefaultError{
	StatusField:   http.StatusText(http.StatusRequestTimeout),
	ErrorField:    "The request timed out",
	CodeField:     http.StatusRequestTimeout,
	GRPCCodeField: codes.DeadlineExceeded,
}
View Source
var ErrUnauthorized = DefaultError{
	StatusField:   http.StatusText(http.StatusUnauthorized),
	ErrorField:    "Access denied. Please authenticate with valid credentials",
	CodeField:     http.StatusUnauthorized,
	GRPCCodeField: codes.Unauthenticated,
}
View Source
var ErrUnsupportedMediaType = DefaultError{
	StatusField:   http.StatusText(http.StatusUnsupportedMediaType),
	ErrorField:    "The media type of the requested resource is not supported",
	CodeField:     http.StatusUnsupportedMediaType,
	GRPCCodeField: codes.InvalidArgument,
}

Functions

func ContextWithRequester

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

func ErrorResponse

func ErrorResponse(ctx *gin.Context, err error)

func NewDataResponse

func NewDataResponse(data interface{}) *successResponse

func NewRequester

func NewRequester(id, uid string) *requesterData

func NewResponse

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

func NewSubscriberEngine

func NewSubscriberEngine(name string, ps PubSubComponent, ac appctx.AppContext) *subscriberEngine

func SuccessResponse

func SuccessResponse(ctx *gin.Context, data *successResponse)

Types

type CacheComponent

type CacheComponent interface {
	Set(ctx context.Context, key string, value interface{}, ttl time.Duration) error
	Get(ctx context.Context, key string, value interface{}) error
	Delete(ctx context.Context, key string) error
}

type DBMigrationComponent

type DBMigrationComponent interface {
	MigrateUp()
	MigrateDown()
}

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"`

	// Further error details
	DetailsField map[string]interface{} `json:"details,omitempty"`

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

	GRPCCodeField codes.Code `json:"-"`
	// 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) GRPCStatus

func (e DefaultError) GRPCStatus() *status.Status

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) 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 EmailComponent

type EmailComponent interface {
	SendEmail(
		subject string,
		content string,
		to []string,
		cc []string,
		bcc []string,
		attachments []string,
	) error
}

type GRPCClientComponent

type GRPCClientComponent interface {
	WithPrefix(prefix string)
	WithAddress(address string)
	GetAddress() string
	GetLogger() appctx.Logger
	Dial(options ...grpc.DialOption) *grpc.ClientConn
	DialContext(ctx context.Context, options ...grpc.DialOption) *grpc.ClientConn
}

type GRPCServerComponent

type GRPCServerComponent interface {
	WithAddress(address string)
	WithListener(lis net.Listener)
	WithServerOptions(serverOpts ...grpc.ServerOption)
	WithServeMuxOptions(muxOpts ...runtime.ServeMuxOption)
	WithUnaryInterceptors(interceptors ...grpc.UnaryServerInterceptor)
	WithStreamInterceptors(interceptors ...grpc.StreamServerInterceptor)
	GetLogger() appctx.Logger
	GetServer() *grpc.Server
	GetGateway() *runtime.ServeMux
	Start(ctx context.Context)
}

type GinComponent

type GinComponent interface {
	GetAddress() string
	GetRouter() *gin.Engine
	Start()
	StartGracefully()
}

type GormDBComponent

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

type GroupSubJob

type GroupSubJob interface {
	Run(ctx context.Context) error
}

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;" db:"id"`
	Name     string `json:"name" gorm:"column:name;" db:"name"`
	Path     string `json:"path" gorm:"column:path;" db:"path"`
	URL      string `json:"url" gorm:"-" db:"-"`
	Width    int    `json:"width" gorm:"column:width;" db:"width"`
	Height   int    `json:"height" gorm:"column:height;" db:"height"`
	Provider string `json:"provider,omitempty" gorm:"column:provider;" db:"provider"`
}

func (*Image) Fulfill

func (img *Image) Fulfill(domain string)

func (*Image) Scan

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

func (Image) TableName

func (Image) TableName() string

func (*Image) Value

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

type Images

type Images []Image

func (*Images) Scan

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

func (*Images) Value

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

type Paging

type Paging struct {
	Page       int    `json:"page" form:"page"`
	Limit      int    `json:"limit" form:"limit"`
	Total      int64  `json:"total" form:"-"`
	FakeCursor string `json:"cursor" form:"cursor"`
	NextCursor string `json:"next_cursor"`
}

func (*Paging) Process

func (p *Paging) Process()

type PubSubComponent

type PubSubComponent interface {
	Publish(ctx context.Context, topic pubsub.Topic, msg *pubsub.Message) error
	Subscribe(ctx context.Context, topic pubsub.Topic) (ch <-chan *pubsub.Message, unsubscribeFunc func())
}

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 RedisDBComponent

type RedisDBComponent interface {
	GetDB() *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 {
	GetID() string
	GetUID() string
}

func GetRequester

func GetRequester(ctx context.Context) Requester

type SQLModel

type SQLModel struct {
	ID        int        `json:"-" gorm:"column:id;" db:"id"`
	FakeID    *util.UID  `json:"id,omitempty" gorm:"-"`
	CreatedAt *time.Time `json:"created_at,omitempty" gorm:"column:created_at;" db:"created_at"`
	UpdatedAt *time.Time `json:"updated_at,omitempty" gorm:"column:updated_at;" db:"updated_at"`
}

func NewSQLModel

func NewSQLModel() SQLModel

func (*SQLModel) Mask

func (sqlModel *SQLModel) Mask(objectId int)

type SimpleUser

type SimpleUser struct {
	SQLModel
	FirstName string `json:"first_name" gorm:"column:first_name;" db:"first_name"`
	LastName  string `json:"last_name" gorm:"column:last_name;" db:"last_name"`
	Avatar    *Image `json:"avatar" gorm:"column:avatar;" db:"avatar"`
}

func NewSimpleUser

func NewSimpleUser(id int, firstName, lastName string, avatar *Image) SimpleUser

func (SimpleUser) TableName

func (SimpleUser) TableName() string

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 StorageComponent

type StorageComponent interface {
	UploadFile(ctx context.Context, data []byte, key string, contentType string) (url string, storageName string, err error)
	GetPresignedURL(ctx context.Context, key string, expiration time.Duration) (string, error)
	GetPresignedURLs(ctx context.Context, keys []string, expiration time.Duration) (map[string]string, error)
	DeleteFiles(ctx context.Context, keys []string) error
}

type SubJob

type SubJob struct {
	Name string
	Hdl  func(ctx context.Context, msg *pubsub.Message) error
}

type TokenMakerComponent

type TokenMakerComponent interface {
	CreateToken(tokenType token.TokenType, uid string, duration ...time.Duration) (string, *token.Payload, error)
	VerifyToken(token string) (*token.Payload, error)
}

Jump to

Keyboard shortcuts

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