component

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2022 License: MIT Imports: 31 Imported by: 1

Documentation

Index

Constants

View Source
const (
	Sync  = 0
	Async = 1
)

Variables

View Source
var (
	EmptyResponse = errors.New("empty response")
)
View Source
var (
	ErrTokenInvalid = errors.New("token is invalid")
)

Functions

func Initialize

func Initialize(provider IProvider)

Initialize sets the component provider instance

Types

type Cache

type Cache struct {
	Named
	// contains filtered or unexported fields
}

Cache provide cache instance builder function

func NewCache

func NewCache() *Cache

func (*Cache) Build

func (c *Cache) Build() *cache.Cache

Build returns a new cache instance

func (*Cache) BuildWith

func (c *Cache) BuildWith(defaultExpiration, cleanupInterval time.Duration) *cache.Cache

BuildWith returns a new cache instance with the given expiration.

func (*Cache) Default

func (c *Cache) Default() *cache.Cache

Default returns the default cache instance

func (*Cache) WithCleanupInterval

func (c *Cache) WithCleanupInterval(cleanupInterval time.Duration) *Cache

WithCleanupInterval sets default cleanup interval for cache component

func (*Cache) WithDefaultExpiration

func (c *Cache) WithDefaultExpiration(defaultExpiration time.Duration) *Cache

WithDefaultExpiration sets default expiration for cache component

type Component

type Component interface {
	Name() string
}

Component represents a component

type Config

type Config struct {
	Named
	*viper.Viper
}

func NewConfig

func NewConfig() *Config

func (*Config) LoadFile

func (c *Config) LoadFile(paths ...string) error

LoadFile loads the configuration file specified.

type Container

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

func NewContainer

func NewContainer() *Container

func (*Container) Cache

func (c *Container) Cache() *Cache

func (*Container) Config

func (c *Container) Config() *Config

func (*Container) Cron

func (c *Container) Cron() *Cron

func (*Container) Curl

func (c *Container) Curl() *Curl

func (*Container) DI

func (c *Container) DI() *DI

func (*Container) EventDispatcher

func (c *Container) EventDispatcher() *EventDispatcher

func (*Container) Get

func (c *Container) Get(name string) (Component, bool)

func (*Container) GetOrInit

func (c *Container) GetOrInit(name string) Component

func (*Container) Gorm

func (c *Container) Gorm() *Gorm

func (*Container) Jwt

func (c *Container) Jwt() *JWT

func (*Container) Logger

func (c *Container) Logger() *Logger

func (*Container) Mongo

func (c *Container) Mongo() *Mongo

func (*Container) Redis

func (c *Container) Redis() *Redis

func (*Container) Register

func (c *Container) Register(components ...Component)

func (*Container) Tracer

func (c *Container) Tracer() *Tracer

func (*Container) Validator

func (c *Container) Validator() *Validator

type Cron

type Cron struct {
	Named
	*cron.Cron
}

func NewCron

func NewCron() *Cron

type Curl

type Curl struct {
	Named
	// contains filtered or unexported fields
}

Curl simple wrapper of http.Client

func NewCurl

func NewCurl() *Curl

func (*Curl) Delete

func (c *Curl) Delete(url string, body io.Reader) (serializer.Serializer, error)

Delete send delete request

func (*Curl) Get

func (c *Curl) Get(url string) (serializer.Serializer, error)

Get send get request

func (*Curl) Post

func (c *Curl) Post(url string, body io.Reader) (serializer.Serializer, error)

Post send post request

func (*Curl) PostFile

func (c *Curl) PostFile(url string, files map[string]string, params map[string]string) (serializer.Serializer, error)

PostFile send post request with file

func (*Curl) Put

func (c *Curl) Put(url string, body io.Reader) (serializer.Serializer, error)

Put send put request

func (*Curl) ReadResponse

func (c *Curl) ReadResponse(resp *http.Response) (serializer.Serializer, error)

ReadResponse reads body from the *http.Response

func (*Curl) Send

func (c *Curl) Send(request *http.Request) (serializer.Serializer, error)

Send return Response by http.Request

func (*Curl) WithHttpClient

func (c *Curl) WithHttpClient(httpClient *http.Client) *Curl

WithHttpClient sets the http client

type DI

type DI struct {
	Named
	// contains filtered or unexported fields
}

DI implements dependencies injection component by *dig.Container

func NewDI

func NewDI() *DI

func (*DI) Container

func (d *DI) Container() *dig.Container

func (*DI) Inject

func (d *DI) Inject(constructor interface{}) error

func (*DI) Invoke

func (d *DI) Invoke(constructor interface{}) error

type Event

type Event struct {
	// name
	Name string
	// event params
	Params interface{}
}

Event

type EventDispatcher

type EventDispatcher struct {
	Named
	// contains filtered or unexported fields
}

func NewEventDispatcher

func NewEventDispatcher() *EventDispatcher

func (*EventDispatcher) Has

func (instance *EventDispatcher) Has(eventName string) bool

Has return event exist

func (*EventDispatcher) Listen

func (instance *EventDispatcher) Listen(eventName string, handler Handler)

Listen register a sync event

func (*EventDispatcher) Register

func (instance *EventDispatcher) Register(eventName string, listener Listener)

Register

func (*EventDispatcher) Trigger

func (instance *EventDispatcher) Trigger(eventName string, params interface{})

Trigger make event trigger with given name and params

type Gorm

type Gorm struct {
	Named
	*gorm.DB
}

func NewGorm

func NewGorm() *Gorm

func (*Gorm) EnableConnectionPool

func (g *Gorm) EnableConnectionPool(maxIdleConns int, maxOpenConns int, connMaxLifetime time.Duration) error

EnableConnectionPool enables connection pool

func (*Gorm) Open

func (g *Gorm) Open(dialector gorm.Dialector, config *gorm.Config) (err error)

Open connect to database

func (*Gorm) OpenMySQL

func (g *Gorm) OpenMySQL(dsn string) error

OpenMySQL connect to mysql data

func (*Gorm) RegisterResolverConfig

func (g *Gorm) RegisterResolverConfig(config dbresolver.Config, tables ...interface{})

RegisterResolverConfig registers resolver configuration for current connection

func (*Gorm) UseResolver

func (g *Gorm) UseResolver(resolver *dbresolver.DBResolver) error

UseResolver use resolver for gorm component.include set many db connection configuration with different model or table name

type Handler

type Handler func(ev Event)

Handler process event

type IProvider

type IProvider interface {
	Logger() *Logger
	Cache() *Cache
	Config() *Config
	Curl() *Curl
	Jwt() *JWT
	Tracer() *Tracer
	EventDispatcher() *EventDispatcher
	Redis() *Redis
	Validator() *Validator
	Gorm() *Gorm
	Cron() *Cron
	Register(components ...Component)
	Get(name string) (Component, bool)
}

IProvider is a component provider

func Provider

func Provider() IProvider

Provider returns the component provider singleton instance

type JWT

type JWT struct {
	Named
	// contains filtered or unexported fields
}

JWT provide the jwt algorithm component

func NewJWT

func NewJWT() *JWT

func (JWT) GenerateToken

func (impl JWT) GenerateToken(claims jwt.Claims) (string, error)

CreateToken 生成token

func (JWT) ParseToken

func (impl JWT) ParseToken(token string) (jwt.Claims, error)

ParseToken return jwt.MapClaims and error

func (*JWT) WithSignKey

func (impl *JWT) WithSignKey(key []byte) *JWT

WithSignKey sets the jwt algorithm signature key

type Listener

type Listener struct {
	Mode    int
	Handler Handler
}

Listener

type LogLevel

type LogLevel string
const (
	LevelInfo  LogLevel = "info"
	LevelDebug LogLevel = "debug"
	LevelWarn  LogLevel = "warn"
	LevelError LogLevel = "error"
	LevelFatal LogLevel = "fatal"
	LevelPanic LogLevel = "panic"
)

type Logger

type Logger struct {
	Named
}

func NewLogger

func NewLogger() *Logger

func (*Logger) Debug

func (l *Logger) Debug(message string)

func (*Logger) Debugf

func (l *Logger) Debugf(format string, args ...interface{})

func (*Logger) Error

func (l *Logger) Error(message string)

func (*Logger) Errorf

func (l *Logger) Errorf(format string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(message string)

func (*Logger) Fatalf

func (l *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Info

func (l *Logger) Info(message string)

func (*Logger) Infof

func (l *Logger) Infof(format string, args ...interface{})

func (*Logger) Log

func (l *Logger) Log(level LogLevel, message string)

func (*Logger) Logf

func (l *Logger) Logf(level LogLevel, format string, a ...interface{})

func (*Logger) Panic

func (l *Logger) Panic(message string)

func (*Logger) Warn

func (l *Logger) Warn(message string)

func (*Logger) Warnf

func (l *Logger) Warnf(format string, args ...interface{})

type Mongo

type Mongo struct {
	Named
	*mongo.Client
	// contains filtered or unexported fields
}

func NewMongo

func NewMongo() *Mongo

func (*Mongo) Close

func (m *Mongo) Close() error

Close closes the connection to the

func (*Mongo) Collection

func (m *Mongo) Collection(name string, opts ...*options.CollectionOptions) *mongo.Collection

Collection use current database for the provide collection

func (*Mongo) Connect

func (m *Mongo) Connect(config *MongoConfig, timeout time.Duration) error

Connect connects to the server

func (*Mongo) Database

func (m *Mongo) Database(database string, opts ...*options.DatabaseOptions) *mongo.Database

Database use the other database

type MongoConfig

type MongoConfig struct {
	Address    string // mongo address, like: mongodb://127.0.0.1:27017
	Username   string // username
	Password   string
	Database   string
	AuthSource string        // default is admin
	Timeout    time.Duration // default is 30 seconds
}

MongoConfig represents the configuration of a MongoDB connection

type Named

type Named string

func (Named) Name

func (n Named) Name() string

type Redis

type Redis struct {
	Named
	redis.UniversalClient
}

func NewRedis

func NewRedis() *Redis

func (*Redis) Connect

func (r *Redis) Connect(options *redis.Options) error

type Tracer

type Tracer struct {
	Named
	// contains filtered or unexported fields
}

Tracer generate the uuid for per goroutine, use to mark user requests.

func NewTracer

func NewTracer() *Tracer

func (*Tracer) Get

func (tracer *Tracer) Get() string

Get returns the uuid for this goroutine, it will generate a unique string if it doesn't exist'

func (*Tracer) Release

func (tracer *Tracer) Release()

Release remove the uuid of this goroutine

func (*Tracer) Set

func (tracer *Tracer) Set(id string)

Set sets the uuid for this goroutine

type Validator

type Validator struct {
	Named
	// contains filtered or unexported fields
}

func NewValidator

func NewValidator() *Validator

func (*Validator) ValidateStruct

func (v *Validator) ValidateStruct(obj interface{}) error

ValidateStruct validate struct

Jump to

Keyboard shortcuts

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