application

package
v0.1.38 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 26 Imported by: 0

Documentation

Overview

*

  • @ Author: Daniel Tan

  • @ Date: 2020-04-07 09:41:11

  • @ LastEditTime: 2020-07-29 19:03:26

  • @ LastEditors: Daniel Tan

  • @ Description:

  • @ FilePath: /trinitygo/application/app.go

  • @

  • @Author: Daniel TAN

  • @Description:

  • @Date: 2021-03-03 15:46:55

  • @LastEditTime: 2021-03-17 01:47:28

  • @LastEditors: Daniel TAN

  • @FilePath: /trinitygo/application/context_pool.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeGRPCRuntimeKey

func DecodeGRPCRuntimeKey(ctx context.Context, runtimeKey []truntime.RuntimeKey) map[string]string

DecodeGRPCRuntimeKey decode runtime key from ctx

func DecodeHTTPRuntimeKey

func DecodeHTTPRuntimeKey(c *gin.Context, runtimeKey []truntime.RuntimeKey) map[string]string

DecodeHTTPRuntimeKey decode http runtime

func DiAllFields

func DiAllFields(dest interface{}, tctx Context, app Application, c *gin.Context, instanceMapping map[string]reflect.Type, injectingMap map[reflect.Type]interface{}) (map[reflect.Type]interface{}, map[reflect.Type]interface{})

DiAllFields di service pool

func DiFree

func DiFree(dest interface{})

DiFree di instance

func DiSelfCheck added in v0.0.22

func DiSelfCheck(destName interface{}, pool *sync.Pool, logger *golog.Logger, instancePool *InstancePool, instanceMapping map[string]reflect.Type)

DiSelfCheck ()map[reflect.Type]interface{} {}

func GetAutoFreeTags added in v0.1.10

func GetAutoFreeTags(object interface{}, index int) bool

GetAutoFreeTags get autofree tags from struct default true

func GetAutowiredTags added in v0.0.22

func GetAutowiredTags(object interface{}, index int) bool

GetAutowiredTags get autowired tags from struct default false

func GetResourceTags added in v0.0.22

func GetResourceTags(object interface{}, index int) string

GetResourceTags get resource tags

func TransactionTag added in v0.0.22

func TransactionTag(object interface{}, index int) bool

TransactionTag get the transaction tag from struct

Types

type AppMock added in v0.0.40

type AppMock struct {
	mock.Mock
}

AppMock for application mock

func (*AppMock) Conf added in v0.0.40

func (a *AppMock) Conf() conf.Conf

Conf mock

func (*AppMock) ContextPool added in v0.0.40

func (a *AppMock) ContextPool() *ContextPool

ContextPool mock

func (*AppMock) ControllerPool added in v0.0.40

func (a *AppMock) ControllerPool() *ControllerPool

ControllerPool mock

func (*AppMock) DB added in v0.0.40

func (a *AppMock) DB() *gorm.DB

DB mock

func (*AppMock) Enforcer added in v0.0.40

func (a *AppMock) Enforcer() *casbin.Enforcer

Enforcer mock

func (*AppMock) GetGRPCServer added in v0.0.40

func (a *AppMock) GetGRPCServer() *grpc.Server

GetGRPCServer mock

func (*AppMock) InitGRPC added in v0.0.40

func (a *AppMock) InitGRPC()

InitGRPC mock

func (*AppMock) InitHTTP added in v0.0.40

func (a *AppMock) InitHTTP()

InitHTTP mock

func (*AppMock) InitRouter added in v0.0.40

func (a *AppMock) InitRouter()

InitRouter mock

func (*AppMock) InstallDB added in v0.0.40

func (a *AppMock) InstallDB(f func() *gorm.DB)

InstallDB mock

func (*AppMock) InstancePool added in v0.0.40

func (a *AppMock) InstancePool() *InstancePool

InstancePool mock

func (*AppMock) IsLogSelfCheck added in v0.0.40

func (a *AppMock) IsLogSelfCheck() bool

IsLogSelfCheck mock

func (*AppMock) Keyword added in v0.0.40

func (a *AppMock) Keyword() keyword.Keyword

Keyword mock

func (*AppMock) Logger added in v0.0.40

func (a *AppMock) Logger() *golog.Logger

Logger mock

func (*AppMock) RegRuntimeKey added in v0.0.40

func (a *AppMock) RegRuntimeKey(runtime ...truntime.RuntimeKey) Application

RegRuntimeKey mock

func (*AppMock) ResponseFactory added in v0.0.40

func (a *AppMock) ResponseFactory() func(status int, res interface{}, runtime map[string]string) interface{}

ResponseFactory mock

func (*AppMock) RuntimeKeys added in v0.0.40

func (a *AppMock) RuntimeKeys() []truntime.RuntimeKey

RuntimeKeys mock

func (*AppMock) ServeGRPC added in v0.0.40

func (a *AppMock) ServeGRPC()

ServeGRPC mock

func (*AppMock) ServeHTTP added in v0.0.40

func (a *AppMock) ServeHTTP()

ServeHTTP mock

func (*AppMock) UseInterceptor added in v0.0.40

func (a *AppMock) UseInterceptor(interceptor ...grpc.UnaryServerInterceptor) Application

UseInterceptor mock

func (*AppMock) UseMiddleware added in v0.0.40

func (a *AppMock) UseMiddleware(middleware ...gin.HandlerFunc) Application

UseMiddleware mock

type Application

type Application interface {
	IsLogSelfCheck() bool
	Logger() *golog.Logger
	RuntimeKeys() []truntime.RuntimeKey
	Conf() conf.Conf
	Keyword() keyword.Keyword
	ContextPool() *ContextPool
	DB() *gorm.DB
	Enforcer() *casbin.Enforcer
	InstallDB(f func() *gorm.DB)
	ControllerPool() *ControllerPool
	InstancePool() *InstancePool
	UseInterceptor(interceptor ...grpc.UnaryServerInterceptor) Application
	UseMiddleware(middleware ...gin.HandlerFunc) Application
	RegRuntimeKey(runtime ...truntime.RuntimeKey) Application
	InitGRPC()
	InitHTTP()
	InitRouter()
	GetGRPCServer() *grpc.Server
	ServeGRPC()
	ServeHTTP()
	ResponseFactory() func(status int, res interface{}, runtime map[string]string) interface{}
}

Application global app interface

type Context

type Context interface {
	NewHTTPServiceRequest(serviceName string, method httputil.RequestMethod, path string, body []byte) (int, interface{}, error)
	Application() Application

	Runtime() map[string]string
	DB() *gorm.DB
	DBTx() *gorm.DB
	AutoFreeOn()
	AutoFreeOff()
	SafeCommit()
	SafeRollback()
	DBTxIsOpen() bool

	GinCtx() *gin.Context
	SetDB(*gorm.DB)

	GetCurrentUser() (interface{}, error)
	HTTPStatus(code int)

	HTTPResponseInternalErr(error)

	HTTPResponseOk(interface{}, error)

	HTTPResponse(interface{}, error)
	SetIsConfigured()
	IsConfigured() bool
	AutoFree() bool
	// contains filtered or unexported methods
}

Context record all thing inside one request

func MockContext

func MockContext(app Application, db *gorm.DB, c *gin.Context, runtime map[string]string) Context

MockContext used for unittest , don't use in production

func NewContext

func NewContext(app Application, funcToGetWhoAmI func(app Application, c *gin.Context, db *gorm.DB) (interface{}, error)) Context

NewContext new contedt

type ContextImpl

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

ContextImpl Context impl

func (*ContextImpl) Application

func (c *ContextImpl) Application() Application

Application get app

func (*ContextImpl) AutoFree added in v0.1.12

func (c *ContextImpl) AutoFree() bool

AutoFree set auto free on

func (*ContextImpl) AutoFreeOff added in v0.1.12

func (c *ContextImpl) AutoFreeOff()

AutoFreeOff set auto free off

func (*ContextImpl) AutoFreeOn added in v0.1.12

func (c *ContextImpl) AutoFreeOn()

AutoFreeOn set auto free on

func (*ContextImpl) DB

func (c *ContextImpl) DB() *gorm.DB

DB get db instance

func (*ContextImpl) DBTx

func (c *ContextImpl) DBTx() *gorm.DB

DBTx get db instance

func (*ContextImpl) DBTxIsOpen

func (c *ContextImpl) DBTxIsOpen() bool

DBTxIsOpen return is transaction is open

func (*ContextImpl) GetCurrentUser added in v0.0.35

func (c *ContextImpl) GetCurrentUser() (interface{}, error)

GetCurrentUser get current User

func (*ContextImpl) GinCtx

func (c *ContextImpl) GinCtx() *gin.Context

GinCtx get gin ctx

func (*ContextImpl) HTTPResponse

func (c *ContextImpl) HTTPResponse(res interface{}, err error)

HTTPResponse handle response

func (*ContextImpl) HTTPResponseInternalErr

func (c *ContextImpl) HTTPResponseInternalErr(err error)

HTTPResponseInternalErr handle http 400 response

func (*ContextImpl) HTTPResponseOk

func (c *ContextImpl) HTTPResponseOk(res interface{}, err error)

HTTPResponseOk handle http http.StatusOK 200 response

func (*ContextImpl) HTTPStatus added in v0.1.3

func (c *ContextImpl) HTTPStatus(code int)

HTTPStatus set http status

func (*ContextImpl) IsConfigured added in v0.1.12

func (c *ContextImpl) IsConfigured() bool

IsConfigured get app

func (*ContextImpl) NewHTTPServiceRequest added in v0.0.35

func (c *ContextImpl) NewHTTPServiceRequest(serviceName string, method httputil.RequestMethod, path string, body []byte) (int, interface{}, error)

NewHTTPServiceRequest new http service request

func (*ContextImpl) Runtime

func (c *ContextImpl) Runtime() map[string]string

Runtime get runtime info

func (*ContextImpl) SafeCommit

func (c *ContextImpl) SafeCommit()

SafeCommit safe commit

func (*ContextImpl) SafeRollback

func (c *ContextImpl) SafeRollback()

SafeRollback safe rollback

func (*ContextImpl) SetDB added in v0.1.34

func (c *ContextImpl) SetDB(db *gorm.DB)

setDB set db

func (*ContextImpl) SetIsConfigured added in v0.1.12

func (c *ContextImpl) SetIsConfigured()

SetIsConfigured get app

type ContextMock added in v0.0.40

type ContextMock struct {
	mock.Mock
}

ContextMock mock impl for Context

func (*ContextMock) Application added in v0.0.40

func (c *ContextMock) Application() Application

Application application mock

func (*ContextMock) AutoFree added in v0.1.12

func (c *ContextMock) AutoFree() bool

func (*ContextMock) AutoFreeOff added in v0.1.12

func (c *ContextMock) AutoFreeOff()

AutoFreeOff set auto free off

func (*ContextMock) AutoFreeOn added in v0.1.12

func (c *ContextMock) AutoFreeOn()

AutoFreeOn set auto free on

func (*ContextMock) DB added in v0.0.40

func (c *ContextMock) DB() *gorm.DB

DB mock

func (*ContextMock) DBTx added in v0.0.40

func (c *ContextMock) DBTx() *gorm.DB

DBTx mock

func (*ContextMock) DBTxIsOpen added in v0.0.40

func (c *ContextMock) DBTxIsOpen() bool

DBTxIsOpen mock

func (*ContextMock) GetCurrentUser added in v0.0.40

func (c *ContextMock) GetCurrentUser() (interface{}, error)

GetCurrentUser get current user

func (*ContextMock) GinCtx added in v0.0.40

func (c *ContextMock) GinCtx() *gin.Context

GinCtx mock

func (*ContextMock) HTTPResponse added in v0.0.40

func (c *ContextMock) HTTPResponse(interface{}, error)

HTTPResponse mock

func (*ContextMock) HTTPResponseInternalErr added in v0.0.40

func (c *ContextMock) HTTPResponseInternalErr(error)

HTTPResponseInternalErr mock

func (*ContextMock) HTTPResponseOk added in v0.0.40

func (c *ContextMock) HTTPResponseOk(interface{}, error)

HTTPResponseOk mock

func (*ContextMock) HTTPStatus added in v0.1.3

func (c *ContextMock) HTTPStatus(code int)

HTTPStatus mock

func (*ContextMock) IsConfigured added in v0.1.12

func (c *ContextMock) IsConfigured() bool

func (*ContextMock) NewHTTPServiceRequest added in v0.0.40

func (c *ContextMock) NewHTTPServiceRequest(serviceName string, method httputil.RequestMethod, path string, body []byte) (int, interface{}, error)

NewHTTPServiceRequest Mock

func (*ContextMock) Runtime added in v0.0.40

func (c *ContextMock) Runtime() map[string]string

Runtime runtime mock

func (*ContextMock) SafeCommit added in v0.0.40

func (c *ContextMock) SafeCommit()

SafeCommit mock

func (*ContextMock) SafeRollback added in v0.0.40

func (c *ContextMock) SafeRollback()

SafeRollback mock

func (*ContextMock) SetDB added in v0.1.34

func (c *ContextMock) SetDB(*gorm.DB)

func (*ContextMock) SetIsConfigured added in v0.1.12

func (c *ContextMock) SetIsConfigured()

type ContextPool

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

ContextPool is the context pool, it's used inside router and the framework by itself.

It's the only one real implementation inside this package because it used widely.

func New

func New(newFunc func() Context) *ContextPool

New creates and returns a new context pool.

func (*ContextPool) Acquire

func (c *ContextPool) Acquire(app Application, runtime map[string]string, ginCtx *gin.Context) Context

Acquire returns a Context from pool. See Release.

func (*ContextPool) Attach

func (c *ContextPool) Attach(newFunc func() Context)

Attach changes the pool's return value Context.

func (*ContextPool) Release

func (c *ContextPool) Release(ctx Context)

Release puts a Context back to its pull, this function releases its resources. See Acquire.

func (*ContextPool) ReleaseLight

func (c *ContextPool) ReleaseLight(ctx Context)

ReleaseLight will just release the object back to the pool, but the clean method is caller's responsibility now

type ControllerPool

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

ControllerPool service pool if grpc string is the full method of method if http os the GET@/ping/:id need to filter controllerFuncMap to filter funcname

func NewControllerPool

func NewControllerPool() *ControllerPool

NewControllerPool new pool with init map

func (*ControllerPool) ControllerFuncSelfCheck added in v0.0.22

func (s *ControllerPool) ControllerFuncSelfCheck(instancePool *InstancePool, isLog bool, logger *golog.Logger)

ControllerFuncSelfCheck self check http request registered func exist or not

func (*ControllerPool) GetController

func (s *ControllerPool) GetController(controllerName string, tctx Context, app Application, c *gin.Context) (interface{}, map[reflect.Type]interface{}, map[reflect.Type]interface{})

GetController from pool

func (*ControllerPool) GetControllerFuncName

func (s *ControllerPool) GetControllerFuncName(controllerName string) (string, bool)

GetControllerFuncName get controller func name

func (*ControllerPool) GetControllerMap

func (s *ControllerPool) GetControllerMap() []string

GetControllerMap get controller map

func (*ControllerPool) GetControllerValidators

func (s *ControllerPool) GetControllerValidators(controllerName string) []Validator

GetControllerValidators get controller func name

func (*ControllerPool) NewController

func (s *ControllerPool) NewController(controllerType string, instance reflect.Type)

NewController add new service

func (*ControllerPool) NewControllerFunc

func (s *ControllerPool) NewControllerFunc(controllerType string, funcName string)

NewControllerFunc register funcname for controllertype

func (*ControllerPool) NewControllerValidators

func (s *ControllerPool) NewControllerValidators(controllerType string, validator ...Validator)

NewControllerValidators register funcname for controllertype

type DBLogger

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

DBLogger db logger

func NewDBLogger

func NewDBLogger(app Application, runtime map[string]string, config ...*LogConfig) *DBLogger

NewDBLogger new db logger

func (*DBLogger) Print

func (l *DBLogger) Print(v ...interface{})

Print db logger func

type InstancePool added in v0.0.25

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

InstancePool service pool

func NewInstancePool added in v0.0.25

func NewInstancePool() *InstancePool

NewInstancePool new pool with init map

func (*InstancePool) CheckInstanceNameIfExist added in v0.0.25

func (s *InstancePool) CheckInstanceNameIfExist(instanceName reflect.Type) bool

CheckInstanceNameIfExist check contain name if exist

func (*InstancePool) GetInstance added in v0.0.25

func (s *InstancePool) GetInstance(instanceType reflect.Type, tctx Context, app Application, c *gin.Context, injectingMap map[reflect.Type]interface{}) (interface{}, map[reflect.Type]interface{}, map[reflect.Type]interface{})

GetInstance get service with di

func (*InstancePool) GetInstanceType added in v0.0.25

func (s *InstancePool) GetInstanceType(tags string) []reflect.Type

GetInstanceType get all service type

func (*InstancePool) InstanceDISelfCheck added in v0.0.25

func (s *InstancePool) InstanceDISelfCheck(app Application)

InstanceDISelfCheck self check di request registered func exist or not

func (*InstancePool) InstanceMapping added in v0.0.25

func (s *InstancePool) InstanceMapping() map[string]reflect.Type

InstanceMapping instance mapping

func (*InstancePool) NewInstance added in v0.0.25

func (s *InstancePool) NewInstance(instanceType reflect.Type, instancePool *sync.Pool, instanceTags []string)

NewInstance add new instance

func (*InstancePool) Release added in v0.0.25

func (s *InstancePool) Release(instance interface{})

Release release service

type LogConfig

type LogConfig struct {
	ProjectName    bool
	ProjectVersion bool

	// Method displays the request path (bool).
	// Defaults to true.
	Path bool

	// used in http request , show http method
	// default true
	Method bool

	// ClientIP
	// default true
	ClientIP bool

	// used in http request , show http status
	Status bool

	// used in http request , show body size
	BodySize bool

	// Req show request info
	// Defaults to true.
	Request bool
	// Response show response info
	// Defaults to true.
	Response bool
	// Error show error messagee
	// Defaults to true.
	Error bool

	// Latency show latency
	// Default to true
	Latency bool

	// Runtime
	// Default to true
	Runtime bool

	// Defaults nil
	LogFunc func(now time.Time, projectName, projectVersion, method string, latency time.Duration, runtimeValue map[string]string)

	// Skippers will according method to see if skip the logger middleware
	Skippers []SkipperFunc
}

LogConfig middleware config

func DefaultLogConfig

func DefaultLogConfig() *LogConfig

DefaultLogConfig default log middleware setting

type Logger

type Logger interface {
	Interceptor() func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error)
	Middleware() gin.HandlerFunc
}

Logger for log middleware

func NewLogLogger

func NewLogLogger(app Application, config *LogConfig) Logger

NewLogLogger new log logger

type RequestMap

type RequestMap struct {
	Method     httputil.RequestMethod
	SubPath    string
	FuncName   string
	Validators []Validator
}

RequestMap request map to register request

func NewRequestMapping

func NewRequestMapping(method httputil.RequestMethod, path string, funcName string, validators ...Validator) *RequestMap

NewRequestMapping request mapping @funcName if funcname is "" , trinitygoo will use the default http method name to find the method e.g : http method "GET" ==> find method "GET"

type SkipperFunc

type SkipperFunc func(method string) bool

SkipperFunc check if method need to be skip

type Validator

type Validator func(Context) error

Validator to validator request

Jump to

Keyboard shortcuts

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