gf

package module
v0.0.0-...-bd46050 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2022 License: Apache-2.0 Imports: 15 Imported by: 0

README

gin 再次封装 import gf "xxx/go-gin-frame"

base := gf.GetGf()
go func() {
	//测试环境开放
	if configure.CmSysConf.GinMode != "release" {
		base.Gin.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	}
	defer func() {
		if err := recover(); err != nil {
			logger.Error(xxx, "HttpServer server is panic")
		}
	}()
	// 服务连接
	if err := base.Gin.Run( IP + ":8899"); err != nil && err != http.ErrServerClosed {
		logger.Trace(xxx, "HttpServer is listenning")
	}
}()

Documentation

Index

Constants

View Source
const (
	SuccessCode    = 200
	FailInternal   = 400
	FailCode       = 500
	CodeParamError = 400001
)
View Source
const (
	DEFAULT = "default"
)

Variables

View Source
var CodeMapping = codeMapping{
	FailCode:       "operation failed",
	FailInternal:   "network error",
	SuccessCode:    "successful",
	CodeParamError: "invalid parameter",
}
View Source
var ResponseDataNil = struct {
}{}
View Source
var ServiceName string

Functions

func GetConfig

func GetConfig() *goconfig.ConfigFile

return instance config

func GetDb

func GetDb(name string) *gorm.DB

get db if exists

func GetGin

func GetGin() *gin.Engine

return instance gin core

func GetLog

func GetLog() log.Logger

return instance logger

func InitConfig

func InitConfig(path string)

func InitDb

func InitDb()

func InitRedis

func InitRedis()

func ResponseJson

func ResponseJson(c *Context, data interface{})

func ResponseStr

func ResponseStr(c *Context, str string)

func RunProcess

func RunProcess(controller IController, g *gin.Context)

func RunWithRequest

func RunWithRequest(req interface{}, g *gin.Context)

alias name, If run it, do not use RunProcess

Types

type Context

type Context struct {
	*gin.Context
}

func GetContext

func GetContext(g *gin.Context) *Context

type Controller

type Controller struct {
	TrackId string
	Ctx     *Context
	Code    int
	Msg     string

	Data interface{}
	Req  interface{}
	// middleware functions
	Middleware []ProcessFunc
	// logic functions
	ProcessFun ProcessFunc
	// contains filtered or unexported fields
}

func GetNewController

func GetNewController(g *gin.Context, req interface{}) *Controller

func (*Controller) Decode

func (controller *Controller) Decode() IError

controller default Decode

func (*Controller) GetContext

func (controller *Controller) GetContext() *Context

get gin Content

func (*Controller) GetTrackId

func (controller *Controller) GetTrackId() string

get trackId

func (*Controller) Param

func (controller *Controller) Param(key string) string

router params with gin

func (*Controller) Process

func (controller *Controller) Process() IError

controller default Process

func (*Controller) Query

func (controller *Controller) Query(key string) string

query things with gin

func (*Controller) SetContext

func (controller *Controller) SetContext(c *Context)

set Context

func (*Controller) SetError

func (controller *Controller) SetError(err IError)

set error

func (*Controller) SetTrackId

func (controller *Controller) SetTrackId(id string)

set trackId

func (*Controller) Use

func (controller *Controller) Use(fn func(controller *Controller) IError)

type Error

type Error struct {
	Code int
	Msg  string
	Data interface{}
	Err  interface{}
}

func NewError

func NewError(err error) Error

func NewErrorCode

func NewErrorCode(code int) Error

传语言,I18N国际化

func NewErrorStr

func NewErrorStr(msg string) Error

func NewResponse

func NewResponse(code int, msg string, data interface{}) Error

传语言,I18N国际化

func (Error) Error

func (err Error) Error() string

func (Error) GetCode

func (err Error) GetCode() int

func (Error) GetDetail

func (err Error) GetDetail() interface{}

func (Error) GetMsg

func (err Error) GetMsg() string

func (Error) IsNil

func (err Error) IsNil() bool

type Gf

type Gf struct {
	Gin    *gin.Engine
	Log    log.Logger
	Config *goconfig.ConfigFile
	Db     map[string]*gorm.DB
	Redis  map[string]*redis.Client
	Cache  map[string]ICache
}

func GetGf

func GetGf() *Gf

return instance

func (*Gf) GetCache

func (gf *Gf) GetCache(name string) ICache

get cache with name if not exists, fatal err

func (*Gf) GetDb

func (gf *Gf) GetDb(name string) *gorm.DB

get cache with name now only support gorm if not exists, fatal err

func (*Gf) GetDefaultCache

func (gf *Gf) GetDefaultCache() ICache

get default cache, if config set MYSQL_DSN

func (*Gf) GetDefaultDb

func (gf *Gf) GetDefaultDb() *gorm.DB

get default db, if config set MYSQL_DSN

func (*Gf) GetDefaultRedis

func (gf *Gf) GetDefaultRedis() *redis.Client

get default redis

func (*Gf) GetRedis

func (gf *Gf) GetRedis(name string) *redis.Client

get cache with name now only support gorm

func (*Gf) Group

func (gf *Gf) Group(path string, handlers ...gin.HandlerFunc) *gin.RouterGroup

alias gin group

func (*Gf) Handle

func (gf *Gf) Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes

alias gin Handle

func (*Gf) Use

func (gf *Gf) Use(middleware ...gin.HandlerFunc) gin.IRoutes

alias gin Use

type ICache

type ICache interface {
	GetBytes(key string) []byte
	GetString(key string) string
	GetMap(key string) map[string]string
	GetList(key string) []string
	GetValue(key string) (string, error)

	SetValue(key string, val interface{}, ex time.Duration) error
	SetList(key string, val ...interface{}) error
	SetString(key, val string, ex time.Duration) error
	SetMap(key string, m map[string]string, ex time.Duration) error
	SetBytes(key string, b []byte, ex time.Duration) error
	Expire(key string, ex time.Duration)

	Command(args ...string) error
}

type IController

type IController interface {

	//set context(gin)
	SetContext(ctx *Context)
	//get context(gin)
	GetContext() *Context
	//self decode request obj
	//example:
	//get: ?a=b&c=d  => struct{A:b string `url:"a"`,C:d string `url:"c"`}
	//post(json): {"a":"b","c":"d"} struct{A:b string `url:"a"`,C:d string `url:"c"`}
	Decode() IError

	// processing business
	Process() IError

	//defer set error
	SetError(err IError)

	// Used to track distributed service link logs
	// recommend set it in query string
	GetTrackId() string

	// set trackId
	SetTrackId(id string)
	// contains filtered or unexported methods
}

It's a controller with a request process

type IError

type IError interface {
	GetCode() int
	GetMsg() string
	Error() string
	IsNil() bool
	GetDetail() interface{}
}

type IGetterSetter

type IGetterSetter interface {
	GetAttribute(key string) interface{}
	SetAttribute(key string, value interface{}) bool
	SetAttributes(mp map[string]interface{}) bool
	GetAttributes() map[string]interface{}

	GetAttrInt(key string) (int, error)
	GetAttrInt64(key string) (int64, error)
	GetAttrFloat(key string) (float32, error)
	GetAttrFloat64(key string) (float64, error)
	GetAttrUInt(key string) (uint, error)
	GetAttrUInt64(key string) (uint64, error)
	GetAttrBool(key string) (bool, error)
	GetAttrString(key string) (string, error)

	GetAttrTime(key string) (time.Time, error)
}

type IModel

type IModel interface {
	GetBytes() []byte
	GetString() string

	Update() bool
	Create() int64
	Delete() bool
	One(sql string, args ...interface{}) IGetterSetter
	List(sql string, args ...interface{}) []IGetterSetter

	TableName() string
	GetKeyName() string
	GetKey() int64
	GetName() string
}

type IModelStruct

type IModelStruct interface {
	TableName() string
	GetKeyName() string
}

type IResponse

type IResponse interface {
	GetBytes() []byte
	GetString() string
}

type IRouter

type IRouter interface {
}

func GetRouter

func GetRouter() IRouter

alias return instance gin core

type ProcessFunc

type ProcessFunc func(controller *Controller) IError

type Response

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (Response) GetBytes

func (res Response) GetBytes() []byte

func (Response) GetString

func (res Response) GetString() string

Jump to

Keyboard shortcuts

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