contract

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// 生产环境
	EnvProduction = "production"
	// 测试环境
	EnvTesting = "testing"
	// 开发环境
	EnvDevelopment = "development"

	// 环境变量服务的字符串凭证
	EnvKey = "anna:key"
)
View Source
const (
	TraceKeyTraceID  = "trace_id"
	TraceKeySpanID   = "span_id"
	TraceKeyCspanID  = "cspan_id"
	TraceKeyParentID = "parent_id"
	TraceKeyMethod   = "method"
	TraceKeyCaller   = "caller"
	TraceKeyTime     = "time"
)
View Source
const AppKey = "anan:app"

定义app字符串凭证

View Source
const CacheKey = "anan:cache"
View Source
const (
	ConfigKey = "anan:config"
)
View Source
const DistributedKey = "anan:distributed"

字符串凭证

View Source
const KernelKey = "anan:kernel"
View Source
const LogKey = "anan:log"
View Source
const ORMKey = "anan:orm"
View Source
const RedisKey = "anan:redis"
View Source
const TraceKey = "anan:trace"

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	// 表示当前这个app唯一的id
	AppID() string

	// 当前版本
	Version() string

	// 项目的根目录
	BaseFolder() string

	// 配置目录
	ConfigFolder() string

	// 日志目录
	LogFolder() string

	// 服务提供者目录
	ProviderFolder() string

	// 业务中间件目录
	MiddlewareFolder() string

	CommandFolder() string

	RuntimeFolder() string

	TestFolder() string

	// 定义业务代码所在的目录
	AppFolder() string
	// LoadAppConfig 加载新的AppConfig,key为对应的函数转为小写下划线,比如ConfigFolder => config_folder
	LoadAppConfig(kv map[string]string)
}

type CacheService

type CacheService interface {
	// get方法
	Get(ctx context.Context, key string) (string, error)
	GetObj(ctx context.Context, key string, model interface{}) error
	GetMany(ctx context.Context, keys []string) (map[string]string, error)

	// set 方法
	Set(ctx context.Context, key string, val string, timeout time.Duration) error
	SetObj(ctx context.Context, key string, val interface{}, timeout time.Duration) error
	SetMany(ctx context.Context, data map[string]string, timeout time.Duration) error
	SetForever(ctx context.Context, key string, val string) error
	SetForeverObj(ctx context.Context, key string, val interface{}) error

	// 设置某个key的超时时间
	SetTTL(ctx context.Context, key string, timeout time.Duration) error
	GetTTL(ctx context.Context, key string) (time.Duration, error)

	Remember(ctx context.Context, key string, timeout time.Duration, rememberFunc RememberFunc, model interface{}) error

	// 往key对应的值增加step
	Calc(ctx context.Context, key string, step int64) (int64, error)
	Increment(ctx context.Context, key string) (int64, error)
	Decrement(ctx context.Context, key string) (int64, error)

	Del(ctx context.Context, key string) error
	DelMany(ctx context.Context, keys []string) error
}

缓存服务

type Config

type Config interface {
	IsExist(key string) bool

	// 获取一个属性
	Get(key string) interface{}
	GetBool(key string) bool
	GetInt(key string) int
	GetFloat64(key string) float64
	GetTime(key string) time.Time
	GetString(key string) string
	GetIntSlice(key string) []int
	GetStringSlice(key string) []string
	GetStringMap(key string) map[string]interface{}
	GetStringMapString(key string) map[string]string
	GetStringMapStringSlice(key string) map[string][]string

	// 加载配置到某个项目
	Load(key string, val interface{}) error
}

type CtxFielder

type CtxFielder func(ctx context.Context) map[string]interface{}

从context中获取信息的方法

type DBConfig

type DBConfig struct {
	WriteTimeout string `yaml:"write_timeout"` // 写超时时间
	Loc          string `yaml:"log"`           //时区
	Port         int    `yaml:"port"`          //端口
	ReadTimeout  string `yaml:"read_timeout"`  //读超时时间
	Charset      string `yaml:"charset"`       //字符集
	ParseTime    bool   `yaml:"parse_time"`    //是否转换时间类型
	Protocol     string `yaml:"protocol"`      //传输协议
	Dsn          string `yaml:"dsn"`           //
	Database     string `yaml:"database"`
	Collation    string `yaml:"collation"` // 字符序
	Timeout      string `yaml:"timeout"`   //连接超时时间
	Username     string `yaml:"username"`
	Password     string `yaml:"password"`
	Driver       string `yaml:"driver"`
	Host         string `yaml:"host"`

	// 以下配置关于连接池
	ConnMaxIdle     int    `yaml:"conn_max_idle"`      // 最大空闲连接数
	ConnMaxOpen     int    `yaml:"conn_max_open"`      // 最大连接数
	ConnMaxLifetime string `yaml:"conn_max_lifetime"`  // 连接最大生命周期
	ConnMaxIdletime string `yaml:"conn_max_idle_time"` // 空闲最大生命周期

	//配置gorm
	*gorm.Config
}

func (*DBConfig) FormatDsn

func (conf *DBConfig) FormatDsn() (string, error)

type DBoption

type DBoption func(container framework.Container, config *DBConfig) error

type Distributed

type Distributed interface {
	// Select 分布式选择器, 所有节点对某个服务进行抢占,只选择其中一个节点
	// ServiceName 服务名字
	// appID 当前的AppID
	// holdTime 分布式选择器hold住的时间
	// 返回值
	// selectAppID 分布式选择器最终选择的App
	// err 异常才返回,如果没有被选择,不返回err
	Select(serviceName string, appID string, holdTime time.Duration) (selectAppID string, err error)
}

分布式服务

type Env

type Env interface {
	AppEnv() string         // 获取当前的环境 development/testing/production
	IsExist(string) bool    //判断一个变量是否存在
	Get(string) string      // 获取一个环境变量
	All() map[string]string // 获取所有环境变量 .env文件中的 和运行环境融合后的
}

type Formatter

type Formatter func(level LogLevel, t time.Time, msg string, fields map[string]interface{}) ([]byte, error)

Formatter 定义了将日志信息组织成字符串的通用方法

type Kernel

type Kernel interface {
	HttpEngine() http.Handler
}

type Log

type Log interface {
	// Panic 表示会导致整个程序出现崩溃的日志信息
	Panic(ctx context.Context, msg string, fields map[string]interface{})
	// Fatal 表示会导致当前这个请求出现提前终止的错误信息
	Fatal(ctx context.Context, msg string, fields map[string]interface{})
	// Error 表示出现错误,但是不一定影响后续请求逻辑的错误信息
	Error(ctx context.Context, msg string, fields map[string]interface{})
	// Warn 表示出现错误,但是一定不影响后续请求逻辑的报警信息
	Warn(ctx context.Context, msg string, fields map[string]interface{})
	// Info 表示正常的日志信息输出
	Info(ctx context.Context, msg string, fields map[string]interface{})
	// Debug 表示在调试状态下打印出来的日志信息
	Debug(ctx context.Context, msg string, fields map[string]interface{})
	// Trace 表示最详细的信息,一般信息量比较大,可能包含调用堆栈等信息
	Trace(ctx context.Context, msg string, fields map[string]interface{})

	// 设置日志级别
	SetLevel(level LogLevel)

	SetCtxFielder(handler CtxFielder)

	SetFormatter(formatter Formatter)

	SetOutput(out io.Writer)
}

type LogLevel

type LogLevel uint32
const (
	UnknowLevel LogLevel = iota
	// PanicLevel level, panic 表示会导致整个程序出现崩溃的日志信息
	PanicLevel
	// FatalLevel level. fatal 表示会导致当前这个请求出现提前终止的错误信息
	FatalLevel
	// ErrorLevel level. error 表示出现错误,但是不一定影响后续请求逻辑的错误信息
	ErrorLevel
	// WarnLevel level. warn 表示出现错误,但是一定不影响后续请求逻辑的报警信息
	WarnLevel
	// InfoLevel level. info 表示正常的日志信息输出
	InfoLevel
	// DebugLevel level. debug 表示在调试状态下打印出来的日志信息
	DebugLevel
	// TraceLevel level. trace 表示最详细的信息,一般信息量比较大,可能包含调用堆栈等信息
	TraceLevel
)

type ORMService

type ORMService interface {
	GetDB(option ...DBoption) (*gorm.DB, error)
}

type RedisConfig

type RedisConfig struct {
	*redis.Options
}

func (*RedisConfig) Uniqkey

func (rc *RedisConfig) Uniqkey() string

type RedisOption

type RedisOption func(container framework.Container, config *RedisConfig) error

修改option选项的方法

type RedisService

type RedisService interface {
	GetClient(option ...RedisOption) (*redis.Client, error)
}

一个redis服务

type RememberFunc

type RememberFunc func(ctx context.Context, container framework.Container) (interface{}, error)

Cache-aside模式对应的对象生成方法

type Trace

type Trace interface {

	// WithContext register new trace to context
	WithTrace(c context.Context, trace *TraceContext) context.Context
	// GetTrace From trace context
	GetTrace(c context.Context) *TraceContext
	// NewTrace generate a new trace
	NewTrace() *TraceContext
	// StartSpan generate cspan for child call
	StartSpan(trace *TraceContext) *TraceContext

	// traceContext to map for logger
	ToMap(trace *TraceContext) map[string]string

	// GetTrace By Http
	ExtractHTTP(req *http.Request) *TraceContext
	// Set Trace to Http
	InjectHTTP(req *http.Request, trace *TraceContext) *http.Request
}

type TraceContext

type TraceContext struct {
	TraceID  string // traceID global unique
	ParentID string // 父节点SpanID
	SpanID   string // 当前节点SpanID
	CspanID  string // 子节点调用的SpanID, 由调用方指定

	Annotation map[string]string // 标记各种信息
}

Trace define struct according Google Dapper

Jump to

Keyboard shortcuts

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