Documentation
¶
Index ¶
- Constants
- Variables
- func CatchError(funk interface{}) (err error)
- func ErrCode(err error) (code uint64)
- func ErrMsgs(err error) []string
- func ErrWith(err *Error, msg string) error
- func EventName(name string) events.EventName
- func GetLogger() *logger.Journal
- func IsDebugging() bool
- func Mode() string
- func SetLogger(logger *logger.Journal)
- func SetMode(value string)
- func WithStack(err error) string
- type Bulrush
- type Config
- type ConfigOption
- type Error
- type HTTPContext
- type Hook
- type InjectOption
- type Injects
- func (src *Injects) Acquire(ty reflect.Type) interface{}
- func (src *Injects) Append(target *Injects) *Injects
- func (src *Injects) Get(pos int) interface{}
- func (src *Injects) Has(item interface{}) bool
- func (src *Injects) Put(target interface{}) *Injects
- func (src *Injects) Size() int
- func (src *Injects) Swap(i, j int)
- func (src *Injects) Wire(target interface{}) (err error)
- type Lifecycle
- type Option
- type Plugins
- type PluginsOption
- type ReverseInject
Constants ¶
const ( // DebugMode indicates bul mode is debug. DebugMode = "debug" // ReleaseMode indicates bul mode is release. ReleaseMode = "release" // TestMode indicates bul mode is test. TestMode = "test" )
Variables ¶
var ( // ErrPlugin is used when bul.Use(). ErrPlugin = &Error{Code: uint64(100)} // ErrInject is used when bul.Inject() fails. ErrInject = &Error{Code: uint64(101)} // ErrUnaddressable unaddressable value ErrUnaddressable = &Error{Code: uint64(102)} // ErrPrivate indicates a private error. ErrPrivate = &Error{Code: uint64(103)} // ErrPublic indicates a public error. ErrPublic = &Error{Code: uint64(104)} // ErrNotMatch indicates not match error. ErrNotMatch = &Error{Code: uint64(105)} // ErrAny indicates any other error. ErrAny = &Error{Code: uint64(106)} // ErrNu indicates any other error. ErrNu = &Error{Code: uint64(107)} )
var ( EventsStarting = EventName(utils.ToString(1<<49 + 0)) EventsRunning = EventName(utils.ToString(1<<49 + 1)) EventsShutdown = EventName(utils.ToString(1<<49 + 2)) )
Events defined built-in events
var ( // Starting defined before all plugin Starting = func(event events.EventEmmiter) { event.Emit(EventsStarting, EventsStarting) } // Recovery system rec from panic Recovery = func(httpProxy *gin.Engine, router *gin.RouterGroup) { httpProxy.Use(recovery()) router.Use(recovery()) } // HTTPProxy create http proxy HTTPProxy = func() *gin.Engine { return gin.New() } // GRPCProxy create grpc proxy GRPCProxy = func() *grpc.Server { return grpc.NewServer() } // HTTPRouter create http router HTTPRouter = func(httpProxy *gin.Engine, config *Config) *gin.RouterGroup { return httpProxy.Group(config.Prefix) } // Override http methods Override = func(router *gin.RouterGroup, httpProxy *gin.Engine) { funk.ForEach([]func(...gin.HandlerFunc) gin.IRoutes{router.Use, httpProxy.Use}, func(Use func(...gin.HandlerFunc) gin.IRoutes) { Use(func(c *gin.Context) { if c.Request.Method != "POST" { c.Next() } else { method := c.PostForm("_method") methods := [3]string{"DELETE", "PUT", "PATCH"} if method != "" { for _, target := range methods { if target == strings.ToUpper(method) { c.Request.Method = target httpProxy.HandleContext(c) break } } } } }) }) } // HTTPBooting run http proxy and grpc proxy HTTPBooting = func(lifecycle Lifecycle, httpProxy *gin.Engine, gs *grpc.Server, event events.EventEmmiter, config *Config) { var err error defer func() { if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() addr1 := fixedPortPrefix(strings.TrimSpace(config.Port)) addr2 := fixedPortPrefix(strings.TrimSpace(config.Port), 1) name := config.Name grpc, err := net.Listen("tcp", addr2) http := &http.Server{Addr: addr1, Handler: httpProxy} lifecycle.Append(Hook{ OnStart: func(ctx context.Context) error { go func() { err = http.ListenAndServe() if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() go func() { err = gs.Serve(grpc) if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() rushLogger.Debug("================================") rushLogger.Debug("App: %s", name) rushLogger.Debug("Env: %s", config.Env) rushLogger.Debug("Http Listen on %s", addr1) rushLogger.Debug("Grpc Listen on %s", addr2) rushLogger.Debug("================================") return nil }, OnStop: func(ctx context.Context) (err error) { gs.GracefulStop() http.Shutdown(ctx) return }, }) } // HTTPTLSBooting run http proxy and grpc proxy HTTPTLSBooting = func(lifecycle Lifecycle, httpProxy *gin.Engine, gs *grpc.Server, event events.EventEmmiter, config *Config) { var err error defer func() { if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() addr1 := fixedPortPrefix(strings.TrimSpace(config.Port)) addr2 := fixedPortPrefix(strings.TrimSpace(config.Port), 1) name := config.Name grpc, err := net.Listen("tcp", addr2) http := &http.Server{Addr: addr1, Handler: httpProxy} lifecycle.Append(Hook{ OnStart: func(ctx context.Context) error { go func() { err = http.ListenAndServeTLS(config.TLS.CRT, config.TLS.Key) if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() go func() { err = gs.Serve(grpc) if err != nil { rushLogger.Error(fmt.Sprintf("%v", err)) } }() rushLogger.Debug("================================") rushLogger.Debug("App: %s", name) rushLogger.Debug("Env: %s", config.Env) rushLogger.Debug("Http Listen on %s", addr1) rushLogger.Debug("Grpc Listen on %s", addr2) rushLogger.Debug("================================") return nil }, OnStop: func(ctx context.Context) (err error) { gs.GracefulStop() err = http.Shutdown(&HTTPContext{ DeadLineTime: time.Now().Add(3 * time.Second), Chan: make(chan struct{}, 1), }) return }, }) } // Running defined after all plugin Running = func(event events.EventEmmiter) { event.Emit(EventsRunning, EventsRunning) } )
Recovery plugin defined sys recovery HTTPProxy plugin defined http proxy HTTPRouter plugin defined http router Override plugin defined method override Run plugin defined httpproxy run
var Version = 1.0
Version defined bulrush current version
Functions ¶
func CatchError ¶
func CatchError(funk interface{}) (err error)
CatchError defined catch error from panic
func GetLogger ¶
GetLogger defined get logger for bulrush you can append transport to addition.RushLogger It is not recommended to create new one Journal I recommendation to customize different output from addition.RushLogger, but not SetLogger
func IsDebugging ¶
func IsDebugging() bool
IsDebugging returns true if the framework is running in debug mode. Use SetMode(gin.ReleaseMode) to disable debug mode.
Types ¶
type Bulrush ¶
type Bulrush interface { On(events.EventName, ...events.Listener) Once(events.EventName, ...events.Listener) Emit(events.EventName, ...interface{}) PreUse(...interface{}) Bulrush Use(...interface{}) Bulrush PostUse(...interface{}) Bulrush Config(string) Bulrush Inject(...interface{}) Bulrush Acquire(reflect.Type) interface{} Wire(interface{}) error Inspect() ToJSON() interface{} GET(string, ...gin.HandlerFunc) Bulrush POST(string, ...gin.HandlerFunc) Bulrush DELETE(string, ...gin.HandlerFunc) Bulrush PUT(string, ...gin.HandlerFunc) Bulrush Run(...interface{}) error RunTLS(...interface{}) error ExecWithBooting(interface{}) error Shutdown() error }
Bulrush interface{} defined all framework should be , also sys provide a default Bulrush - `rush`
type Config ¶
type Config struct { Version float64 `json:"version" yaml:"version"` Name string `json:"name" yaml:"name"` Prefix string `json:"prefix" yaml:"prefix"` Port string `json:"port" yaml:"port"` TLS struct { CRT string `json:"crt" yaml:"crt"` Key string `json:"key" yaml:"key"` } `json:"tls" yaml:"tls"` Mode string `json:"mode" yaml:"mode"` Env string `json:"env" yaml:"env"` Log struct { Level string `json:"level" yaml:"level"` Path string `json:"path" yaml:"path"` } // contains filtered or unexported fields }
Config bulrush config struct
func LoadConfig ¶
LoadConfig loads the bulrush tool configuration
func (*Config) UnmarshalByType ¶
UnmarshalByType defined unmarshal by diff file type
type ConfigOption ¶
type ConfigOption interface {
// contains filtered or unexported methods
}
ConfigOption defined cfg option
func ConfigValidOption ¶
func ConfigValidOption(path string) ConfigOption
ConfigValidOption defined Option of valid
func ParseConfigOption ¶
func ParseConfigOption(conf *Config) ConfigOption
ParseConfigOption defined Option of PrePlugin
type Error ¶
Error represents a error's specification.
func (*Error) MarshalJSON ¶
MarshalJSON implements the json.Marshaller interface.
type HTTPContext ¶
HTTPContext defined httpContxt
func (*HTTPContext) Deadline ¶
func (ctx *HTTPContext) Deadline() (time.Time, bool)
Deadline defined Deadline time
func (*HTTPContext) Done ¶
func (ctx *HTTPContext) Done() <-chan struct{}
Done defined http done action
func (*HTTPContext) Value ¶
func (ctx *HTTPContext) Value(key interface{}) interface{}
Value nothing
type Hook ¶
A Hook is a pair of start and stop callbacks, either of which can be nil, plus a string identifying the supplier of the hook.
type InjectOption ¶
type InjectOption interface {
// contains filtered or unexported methods
}
InjectOption defined inject option
func InjectsOption ¶
func InjectsOption(injects ...interface{}) InjectOption
InjectsOption defined Option of Injects
func InjectsValidOption ¶
func InjectsValidOption(injects ...interface{}) InjectOption
InjectsValidOption defined Option of valid
type Injects ¶
type Injects []interface{}
Injects defined some entitys that can be inject to middle , Injects would panic if repetition , Injects can be go base tyle or struct or ptr or interface{}
type Lifecycle ¶
type Lifecycle interface { Start(ctx context.Context) error Stop(ctx context.Context) error Append(h Hook) }
Lifecycle interface coordinates application lifecycle hooks.
type Plugins ¶
type Plugins []interface{}
Plugins defined those that can be call by reflect , Plugins passby func or a struct that has `Plugin` func
type PluginsOption ¶
type PluginsOption interface {
// contains filtered or unexported methods
}
PluginsOption defined plugin option
func MiddlePluginsOption ¶
func MiddlePluginsOption(plugins ...interface{}) PluginsOption
MiddlePluginsOption defined Option of MiddlePlugin
func PluginsValidOption ¶
func PluginsValidOption(plugins ...interface{}) PluginsOption
PluginsValidOption defined Option of valid
func PostPluginsOption ¶
func PostPluginsOption(plugins ...interface{}) PluginsOption
PostPluginsOption defined Option of PostPlugin
func PrePluginsOption ¶
func PrePluginsOption(plugins ...interface{}) PluginsOption
PrePluginsOption defined Option of PrePlugin
type ReverseInject ¶
type ReverseInject struct {
// contains filtered or unexported fields
}
ReverseInject defined a inject , for reverse inject
func (*ReverseInject) Register ¶
func (r *ReverseInject) Register(rFunc interface{})
Register defiend function for Reverse Injects Example:
func Route(router *gin.RouterGroup, event events.EventEmmiter, ri *bulrush.ReverseInject) { ri.Register(RegisterMgo) ri.Register(RegisterCache) ri.Register(RegisterSeq) ri.Register(RegisterMq) ri.Register(RegisterEvent) ri.Register(RegisterMock) ri.Register(RegisterGRPC) event.Emit("hello", "this is my payload to hello router") }