zservice

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2025 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Code_Zero         = 0  // 如果出现 0 表示业务未处理状态码
	Code_Succ         = 1  // 成功
	Code_Fail         = 2  // 失败
	Code_Limit        = 3  // 限制/上限
	Code_Auth         = 4  // 鉴权失败/无权
	Code_NotImplement = 5  // 未实现
	Code_Params       = 6  // 参数错误
	Code_NotFound     = 7  // 资源不存在/没找到/数据未查询到
	Code_Again        = 8  // 等待,重试
	Code_Repetition   = 9  // 数据重复, 数据已存在,数据相同
	Code_Reject       = 10 // 拒绝
	Code_Fatal        = 11 // 代码执行阻断执行错误,严重错误,服务断开
)

内置状态码

View Source
const (
	S_S2S_CTX   = "Z-S2S-CTX" // 服务端 链路传输的 Context
	S_C2S_Token = "Z-C2S-TK"  // 客户端 token
	S_C2S_Time  = "Z-C2S-TI"  // 客户端 时间戳
	S_C2S_Sign  = "Z-C2S-SI"  // 客户端 签名
)
View Source
const (
	Time_1m    = time.Minute         // 1 分钟
	Time_10Day = time.Hour * 24 * 10 // 10 天
)

Variables

View Source
var ISDebug = false
View Source
var Version = "0.2.1"

Functions

func BoolToString

func BoolToString(b bool) string

bool 转换字符串

func ContextTODO

func ContextTODO() context.Context

func Convert_RuneToString

func Convert_RuneToString(r rune) string

urne to string

func Erroref

func Erroref(e error, formmat string, v ...any)

func GO_ants added in v0.1.6

func GO_ants(f func())

go ants 协程

func GetServiceName

func GetServiceName() string

获取服务名称

func Getenv

func Getenv(key string) string

获取环境变量,key 不限制大小写

func GetenvBool

func GetenvBool(key string) bool

func GetenvFloat32 added in v0.1.7

func GetenvFloat32(key string) float32

func GetenvFloat64 added in v0.1.7

func GetenvFloat64(key string) float64

func GetenvInt

func GetenvInt(key string) int

func GetenvInt32 added in v0.1.7

func GetenvInt32(key string) int32

func GetenvInt64 added in v0.1.7

func GetenvInt64(key string) int64

func GetenvStringSplit

func GetenvStringSplit(key string, split ...string) []string

json

func GetenvUInt added in v0.1.8

func GetenvUInt(key string) uint

func GetenvUInt32

func GetenvUInt32(key string) uint32

func GetenvUInt64 added in v0.1.7

func GetenvUInt64(key string) uint64

func Go

func Go(f func())

go 原生协程

func Init

func Init(opt ZserviceOption)

zservice 初始化

func Int64ToByte

func Int64ToByte(i int64) []byte

func Int64ToString

func Int64ToString(i int64) string

int64 转字符串

func IntToString

func IntToString(i int) string

int 转字符串

func IntToUint32

func IntToUint32(i int) uint32

int to uint32

func IsInteger

func IsInteger(s string) bool

是否是整数

func IsNum

func IsNum(s string) bool

是否是数字

func JsonMustMarshal

func JsonMustMarshal(v any) []byte

func JsonMustMarshalString

func JsonMustMarshalString(v any) string

func JsonMustUnmarshal

func JsonMustUnmarshal(v []byte) any

func JsonMustUnmarshal_MapAny

func JsonMustUnmarshal_MapAny(v []byte) map[string]any

func JsonMustUnmarshal_StringArray

func JsonMustUnmarshal_StringArray(v []byte) []string

func ListFilter

func ListFilter(arr any, fn func(item any) bool) any

过滤器

func ListFilterString

func ListFilterString(arr []string, fn func(item string) bool) []string

过滤器

func ListHas

func ListHas(arr interface{}, fn func(item any) bool) bool

数组中是否有某个值

func ListRemoveDuplicates

func ListRemoveDuplicates(arr interface{}) interface{}

数组去重

func ListRemoveFirst

func ListRemoveFirst(arr interface{}, i int) interface{}

移除数组中重头开始的指定元素

func LogDebug

func LogDebug(v ...any)

debug

func LogDebugCaller

func LogDebugCaller(caller int, v ...any)

func LogDebugCallerf

func LogDebugCallerf(caller int, format string, v ...any)

func LogDebugf

func LogDebugf(format string, v ...any)

func LogError

func LogError(v ...any)

错误

func LogErrorCaller

func LogErrorCaller(caller int, v ...any)

func LogErrorCallerf

func LogErrorCallerf(caller int, format string, v ...any)

func LogErrore

func LogErrore(e error)

func LogErrorf

func LogErrorf(format string, v ...any)

func LogInfo

func LogInfo(v ...any)

info 消息

func LogInfoCaller

func LogInfoCaller(caller int, v ...any)

func LogInfoCallerf

func LogInfoCallerf(caller int, format string, v ...any)

func LogInfof

func LogInfof(format string, v ...any)

func LogPanic

func LogPanic(v ...any)

panic

func LogPanicCaller

func LogPanicCaller(caller int, v ...any)

func LogPanicCallerf

func LogPanicCallerf(caller int, format string, v ...any)

func LogPanicf

func LogPanicf(format string, v ...any)

func LogWarn

func LogWarn(v ...any)

警告

func LogWarnCaller

func LogWarnCaller(caller int, v ...any)

func LogWarnCallerf

func LogWarnCallerf(caller int, format string, v ...any)

func LogWarnf

func LogWarnf(format string, v ...any)

func MD5String

func MD5String(str string) string

字符串 md5 编码

func MaxInt

func MaxInt(nums ...int) int

func MaxInt64

func MaxInt64(nums ...int64) int64

func Md5Bytes

func Md5Bytes(data []byte) string

字节流 md5 编码

func MergeEnv

func MergeEnv(envs map[string]string)

合并环境变量

func MinInt

func MinInt(nums ...int) int

func MinInt64

func MinInt64(nums ...int64) int64

func ProtobufMustMarshal added in v0.1.7

func ProtobufMustMarshal(pb proto.Message) []byte

func ProtobufMustUnmarshal added in v0.1.7

func ProtobufMustUnmarshal(bt []byte, pb proto.Message)

func RandomFloat32Range

func RandomFloat32Range(min float32, max float32) float32

随机数字范围

func RandomFloat64Range

func RandomFloat64Range(min float64, max float64) float64

随机数字范围

func RandomInt

func RandomInt(count int) int

随机数字

func RandomInt64

func RandomInt64(count int64) int64

随机数字

func RandomInt64Range

func RandomInt64Range(min int64, max int64) int64

随机数字范围

func RandomIntRange

func RandomIntRange(min int, max int) int

随机数字范围

func RandomMD5

func RandomMD5() string

md5

func RandomMD5_XID added in v0.1.10

func RandomMD5_XID() string

md5 xid

func RandomMD5_XID_Random added in v0.1.10

func RandomMD5_XID_Random() string

md5 xid + random string

func RandomString

func RandomString(count int) string

随机字符串

func RandomStringLow

func RandomStringLow(count int) string

随机小写字符串

func RandomStringUP

func RandomStringUP(count int) string

随机大写字符串

func RandomString_AllCharset added in v0.1.10

func RandomString_AllCharset(count int) string

func RandomUInt32Range

func RandomUInt32Range(min uint32, max uint32) uint32

随机数字范围

func RandomUint32

func RandomUint32(count uint32) uint32

随机数字

func RandomXID

func RandomXID() string

随机 xid

func Setenv

func Setenv(key string, value string)

func Sprint

func Sprint(v ...any) string

格式化

func SprintQuote

func SprintQuote(v ...any) string

去掉换行的格式化

func Stop

func Stop()

func StringSplit

func StringSplit(s string, sep string, clearEmpty ...bool) []string

func StringToBoolean

func StringToBoolean(s string) bool

string to boolean

func StringToFloat

func StringToFloat(str string) float32

string to float32

func StringToFloat32

func StringToFloat32(str string) float32

string to float32

func StringToFloat64

func StringToFloat64(str string) float64

string to float64

func StringToInt

func StringToInt(str string) int

string to int, err return 0

func StringToInt32

func StringToInt32(str string) int32

string to int32

func StringToInt64

func StringToInt64(str string) int64

string to int64

func StringToUint

func StringToUint(str string) uint

string to uint

func StringToUint32

func StringToUint32(str string) uint32

string to uint32

func StringToUint64

func StringToUint64(str string) uint64

string to uint64

func StructTo_MapAny

func StructTo_MapAny(v any) map[string]any

func TestAction

func TestAction(name string, cb func())

func UInt64ToString

func UInt64ToString(u uint64) string

uint64 to string

func Uint32ToInt

func Uint32ToInt(u uint32) int

uint32 to int

func Uint32ToString

func Uint32ToString(u uint32) string

uint32 to string

func UintToString

func UintToString(u uint) string

uint to string

func WaitStop

func WaitStop()

等待停止

Types

type Context

type Context struct {
	ContextS2S
	StartTime  time.Time // 当前上下文启动时间
	Service    *ZService // 服务
	GinCtx     *gin.Context
	CTX_mu     sync.Mutex
	CTX_done   atomic.Value
	CTX_err    error
	CTX_values sync.Map
}

集成链路、日志、错误功能

func NewContext

func NewContext(in ...string) *Context

创建上下文

func (*Context) Deadline

func (ctx *Context) Deadline() (deadline time.Time, ok bool)

Deadline implements context.Context.

func (*Context) Done

func (ctx *Context) Done() <-chan struct{}

Done implements context.Context.

func (*Context) Err

func (ctx *Context) Err() error

Err implements context.Context.

func (*Context) GetS2S

func (ctx *Context) GetS2S() string

获取链路信息

func (*Context) LogDebug

func (ctx *Context) LogDebug(v ...any)

func (*Context) LogDebugf

func (ctx *Context) LogDebugf(f string, v ...any)

func (*Context) LogError

func (ctx *Context) LogError(v ...any)

func (*Context) LogErrorf

func (ctx *Context) LogErrorf(f string, v ...any)

func (*Context) LogInfo

func (ctx *Context) LogInfo(v ...any)

func (*Context) LogInfof

func (ctx *Context) LogInfof(f string, v ...any)

func (*Context) LogPanic

func (ctx *Context) LogPanic(v ...any)

func (*Context) LogPanicf

func (ctx *Context) LogPanicf(f string, v ...any)

func (*Context) LogWarn

func (ctx *Context) LogWarn(v ...any)

func (*Context) LogWarnf

func (ctx *Context) LogWarnf(f string, v ...any)

func (*Context) Since

func (ctx *Context) Since() time.Duration

获取上下文创建到现在的时间

func (*Context) SinceTrace

func (ctx *Context) SinceTrace() time.Duration

获取链路创建到现在的时间

func (*Context) Value

func (ctx *Context) Value(key any) any

Value implements context.Context.

type ContextS2S

type ContextS2S struct {
	TraceTime     time.Time `json:"tt,omitempty"`  // 链路初始化时间
	TraceID       string    `json:"ti,omitempty"`  // 链路ID
	TraceSpanID   int       `json:"tsi,omitempty"` // 链路 , 自增处理
	TraceService  string    `json:"ts,omitempty"`  // 链路服务, 之前的服务
	RequestIP     string    `json:"ip,omitempty"`  // 请求IP
	NowService    string    `json:"ns,omitempty"`  // 当前服务
	AuthToken     string    `json:"at,omitempty"`  // token
	AuthTokenSign string    `json:"ats,omitempty"` // 授权的签名
	ClientSign    string    `json:"cs,omitempty"`  // 客户端签名
	ClientTime    uint32    `json:"ct,omitempty"`  // 客户端签名的时间
}

上下文内部交互信息

type Error

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

------------- Error -------------

func GetIp

func GetIp() ([]string, *Error)

func HttpGet

func HttpGet(ctx *Context, url string, params map[string]any, header map[string]string) ([]byte, *Error)

发送 HttpGet 请求

func HttpPost

func HttpPost(ctx *Context, url string, params map[string]any, header map[string]string) (body []byte, e *Error)

发送 post 请求

func HttpPostJson

func HttpPostJson(ctx *Context, url string, params map[string]any, header map[string]string) (body []byte, e *Error)

发送 json 请求

func HttpRequestSend

func HttpRequestSend(ctx *Context, in *http.Request) ([]byte, *Error)

func LoadFileEnv

func LoadFileEnv(envFile string) *Error

加载本地文件环境变量

func LoadStringEnv

func LoadStringEnv(envStr string) *Error

加载字符串中的环境变量

func Md5File

func Md5File(path string) (string, *Error)

根据传入的文件路径对文件进行 MD5 计算

func NewError

func NewError(v ...any) *Error

func NewErrorCaller

func NewErrorCaller(skip int, str string, e error) *Error

func NewErrore

func NewErrore(e error) *Error

func NewErroref

func NewErroref(e error, f string, v ...any) *Error

func NewErrorf

func NewErrorf(f string, v ...any) *Error

func StructDeepcopy added in v0.1.2

func StructDeepcopy(a any, b any) *Error

结构体深拷贝, 将 b 的内容拷贝给 a

func WriteFileToTempDir added in v0.1.2

func WriteFileToTempDir(name string, data []byte) *Error

写入文件到临时目录

func (*Error) AddCaller

func (e *Error) AddCaller(skips ...int) *Error

添加路径记录 skip 一般不需要填写,涉及到 caller 跳层问题

func (*Error) Error

func (e *Error) Error() string

func (*Error) GetCode

func (e *Error) GetCode() uint32

获取错误码

func (*Error) GetMsg added in v0.1.2

func (e *Error) GetMsg() string

获取客户端消息

func (*Error) Is

func (e *Error) Is(target error) bool

func (*Error) SetCode

func (e *Error) SetCode(code uint32) *Error

设置错误码

func (*Error) SetMsg added in v0.1.2

func (e *Error) SetMsg(msg ...any) *Error

设置客户端消息

func (*Error) String

func (e *Error) String() string

type Time

type Time sql.NullTime

扩展 time 包

func NewTime

func NewTime(ti time.Time) Time

func TimeDate

func TimeDate(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time

func TimeNow

func TimeNow() Time

func TimeNull

func TimeNull() Time

func TimeUnix

func TimeUnix(sec int64, nsec int64) Time

func TimeUnixMicro

func TimeUnixMicro(usec int64) Time

func TimeUnixMilli

func TimeUnixMilli(msec int64) Time

func (Time) Add

func (ex Time) Add(d time.Duration) Time

func (Time) AddDate

func (ex Time) AddDate(years int, months int, days int) Time

func (Time) After

func (ex Time) After(u time.Time) bool

func (Time) AfterNow

func (ex Time) AfterNow() bool

func (Time) Before

func (ex Time) Before(u time.Time) bool

func (Time) BeforeNow

func (ex Time) BeforeNow() bool

func (Time) Clock

func (ex Time) Clock() (h, m, s int)

func (Time) Compare

func (ex Time) Compare(u time.Time) int

func (Time) Date

func (ex Time) Date() (year int, month time.Month, day int)

func (Time) Day

func (ex Time) Day() int

func (Time) Equal

func (ex Time) Equal(u time.Time) bool

func (Time) GobDecode

func (ex Time) GobDecode(data []byte) error

func (Time) GobEncode

func (ex Time) GobEncode() ([]byte, error)

func (Time) Hour

func (ex Time) Hour() int

func (Time) ISOWeek

func (ex Time) ISOWeek() (year int, week int)

func (Time) In

func (ex Time) In(loc *time.Location) Time

func (Time) IsDST

func (ex Time) IsDST() bool

func (Time) IsZero

func (ex Time) IsZero() bool

func (Time) Local

func (ex Time) Local() Time

func (Time) Location

func (ex Time) Location() *time.Location

func (Time) MarshalBinary

func (ex Time) MarshalBinary() ([]byte, error)

func (Time) MarshalJSON

func (ex Time) MarshalJSON() ([]byte, error)

func (Time) MarshalText

func (ex Time) MarshalText() ([]byte, error)

func (Time) Minute

func (ex Time) Minute() int

func (Time) Month

func (ex Time) Month() time.Month

func (Time) Nanosecond

func (ex Time) Nanosecond() int

func (Time) Round

func (ex Time) Round(d time.Duration) Time

func (*Time) Scan

func (ex *Time) Scan(value any) error

func (Time) Second

func (ex Time) Second() int

func (Time) Sub

func (ex Time) Sub(u time.Time) time.Duration

func (Time) Truncate

func (ex Time) Truncate(d time.Duration) Time

func (Time) UTC

func (ex Time) UTC() Time

func (Time) Unix

func (ex Time) Unix() int64

func (Time) UnixMicro

func (ex Time) UnixMicro() int64

如果时间为 0 则返回 0

func (Time) UnixMilli

func (ex Time) UnixMilli() int64

如果时间为 0 则返回 0

func (Time) UnixNano

func (ex Time) UnixNano() int64

如果时间为 0 则返回 0

func (Time) UnmarshalBinary

func (ex Time) UnmarshalBinary(data []byte) error

func (Time) UnmarshalJSON

func (ex Time) UnmarshalJSON(data []byte) error

func (Time) UnmarshalText

func (ex Time) UnmarshalText(data []byte) error

func (Time) Value

func (ex Time) Value() (driver.Value, error)

Value implements the driver Valuer interface.

func (Time) Weekday

func (ex Time) Weekday() time.Weekday

func (Time) Year

func (ex Time) Year() int

func (Time) YearDay

func (ex Time) YearDay() int

func (Time) Zone

func (ex Time) Zone() (name string, offset int)

func (Time) ZoneBounds

func (ex Time) ZoneBounds() (start, end time.Time)

type Vector3

type Vector3 struct {
	X, Y, Z float64
}

func (Vector3) Distance

func (v Vector3) Distance(v2 Vector3) float64

type ZService

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

func AddDependService

func AddDependService(s ...*ZService) *ZService

添加依赖服务

func GetMainService

func GetMainService() *ZService

获取主服务

func NewService

func NewService(opt ZserviceOption) *ZService

外部创建服务入口

func Start

func Start() *ZService

func WaitStart

func WaitStart() *ZService

等待启动

func (*ZService) AddDependService

func (z *ZService) AddDependService(sArr ...*ZService) *ZService

添加依赖

func (*ZService) GetState

func (z *ZService) GetState() uint32

func (*ZService) LogDebug added in v0.2.0

func (z *ZService) LogDebug(v ...any)

func (*ZService) LogDebugCaller added in v0.2.0

func (z *ZService) LogDebugCaller(caller int, v ...any)

func (*ZService) LogDebugf added in v0.2.0

func (z *ZService) LogDebugf(f string, v ...any)

func (*ZService) LogError

func (z *ZService) LogError(v ...any)

func (*ZService) LogErrorCaller

func (z *ZService) LogErrorCaller(caller int, v ...any)

func (*ZService) LogErrorf

func (z *ZService) LogErrorf(f string, v ...any)

func (*ZService) LogInfo

func (z *ZService) LogInfo(v ...any)

func (*ZService) LogInfoCaller

func (z *ZService) LogInfoCaller(caller int, v ...any)

func (*ZService) LogInfof

func (z *ZService) LogInfof(f string, v ...any)

func (*ZService) LogPanic

func (z *ZService) LogPanic(v ...any)

func (*ZService) LogPanicCaller

func (z *ZService) LogPanicCaller(caller int, v ...any)

func (*ZService) LogPanicf

func (z *ZService) LogPanicf(f string, v ...any)

func (*ZService) LogWarn

func (z *ZService) LogWarn(v ...any)

func (*ZService) LogWarnCaller

func (z *ZService) LogWarnCaller(caller int, v ...any)

func (*ZService) LogWarnf

func (z *ZService) LogWarnf(f string, v ...any)

func (*ZService) Start

func (z *ZService) Start() *ZService

启动服务

func (*ZService) StartDone

func (z *ZService) StartDone()

启动完成

func (*ZService) Stop

func (z *ZService) Stop() error

停止服务

func (*ZService) WaitStart

func (z *ZService) WaitStart() *ZService

等待启动

func (*ZService) WaitStop

func (z *ZService) WaitStop()

等待停止

type ZserviceOption added in v0.2.0

type ZserviceOption struct {
	Name    string // 显示在日志中的名称
	Version string // 版本号
	Debug   bool   // 是否开启调试模式

	// 服务启动回调
	OnStart func(*ZService)
}

初始化配置

Jump to

Keyboard shortcuts

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