tkit

package module
v0.0.0-...-2baa201 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2024 License: MIT Imports: 13 Imported by: 0

README

tkit

A lightweight framework base on Gin

简易 Demo:https://github.com/FarmerChillax/tkit-layout

工具链

  • 日志
  • 监控告警
  • 链路追踪
  • 异步任务
  • 消息队列

Documentation

Index

Constants

View Source
const (
	VERSION = "v0.0.1"
)

Variables

This section is empty.

Functions

func CommonJsonResult

func CommonJsonResult(ctx *gin.Context, code int, resp any)

通用 json 响应

func Result

func Result(ctx *gin.Context, httpCode, code int, data interface{}, message string, status string)

Result result template

func ResultData

func ResultData(ctx *gin.Context, data interface{})

Result default func Result 方法的默认值包装方法

func ResultError

func ResultError(ctx *gin.Context, err Error)

ResultError result failed

func StartSpan

func StartSpan(ctx context.Context, tracerName, spanName string) (context.Context, trace.Span)

func Wrap

func Wrap[Request any, Response any](handler func(c *gin.Context, requestDTO *Request) (Response, error)) func(c *gin.Context)
type AddUserResponse struct {
	ID int64 `json:"id,omitempty"`
}
func (s *Service) addUser(ctx *gin.Context, req *AddUserRequest) (*AddUserResponse, error) {
    // 处理请求...
}
func main() {
    router := gin.Default()
    router.POST("/user/add", Wrap(s.addUser))
}

Types

type Application

type Application struct {
	Name       string
	Host       string
	Port       int64
	Config     *config.Config
	LoadConfig func() error
	SetupVars  func() error
	// RegisterModule   func() error
	RegisterCallback map[CallbackPosition]CallbackFunc
}

type CallbackFunc

type CallbackFunc func() error

type CallbackPosition

type CallbackPosition int
const (
	// 在 InitGlobalConfig 后调用
	POSITION_GLOBAL_CONFIG CallbackPosition = iota + 1
	// 在 InitGlobalLogger 后调用
	POSITION_INIT_LOGGER
	// 在 Module Register 后调用
	POSITION_MODULE_REGISTER
	// 调用 LoadConfig 方法后
	POSITION_LOAD_CONFIG
	// 调用 SetupVars 方法后
	POSITION_SETUP_VARS
	// 调用 New 方法后
	POSITION_NEW
)

type DatabaseIface

type DatabaseIface interface {
	Get(ctx context.Context) *gorm.DB
}
var Database DatabaseIface

type Error

type Error interface {
	error
	// WithID 设置当前请求的唯一ID
	WithID(id string) Error
	// 设置 http status code
	WithStatusCode(statusCode int) Error
	// 设置错误描述
	WithMsg(msg string) Error

	// 返回业务编码
	Code() int
	// 返回对应的 http 状态码
	StatusCode() int
}

func NewError

func NewError(httpStatusCode, businessCode int, msg string) Error

NewError 返回一个新的自定义错误 NewError 返回一个新的自定义错误

type GinApplication

type GinApplication struct {
	Application                 *Application
	TracerLogger                *logrus.Logger
	RegisterHttpRoute           func(*gin.Engine) error
	RegisterGracefulStopHandler func(sig ...os.Signal) error
}

type LoggerIface

type LoggerIface interface {

	// 普通日志
	Info(ctx context.Context, args ...interface{})
	Infof(ctx context.Context, format string, args ...interface{})

	// 警告日志
	Warn(ctx context.Context, args ...interface{})
	Warnf(ctx context.Context, format string, args ...interface{})

	// 错误日志
	Error(ctx context.Context, args ...interface{})
	Errorf(ctx context.Context, format string, args ...interface{})
}
var Logger LoggerIface

type RedisConn

type RedisConn interface {
	Get(ctx context.Context) *redis.Client
}
var Redis RedisConn

type ResponseDTO

type ResponseDTO struct {
	Code   int    `json:"code"`
	Msg    string `json:"msg"`
	Data   any    `json:"data"`
	Status string `json:"status"`
}

func (*ResponseDTO) GetStatus

func (r *ResponseDTO) GetStatus() string

type ResponseDataIface

type ResponseDataIface interface {
	GetStatus() string
}

Directories

Path Synopsis
examples
internal
pkg

Jump to

Keyboard shortcuts

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