contract

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 2021 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

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

	// EnvKey 是环境变量服务字符串凭证
	EnvKey = "gocore:env"
)
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 = "gocore:app"
View Source
const CacheKey = "gocore:cache"
View Source
const (
	ConfigKey = "gocore:config"
)
View Source
const DistributedKey = "gocore:distributed"
View Source
const IDKey = "gocore:id"
View Source
const KernelKey = "gocore:kernel"
View Source
const LogKey = "gocore:log"
View Source
const ORMKey = "gocore:orm"

ORMKey 代表 ORM的服务

View Source
const RedisKey = "gocore:redis"
View Source
const SSHKey = "gocore:ssh"
View Source
const TraceKey = "gocore:trace"

Variables

This section is empty.

Functions

This section is empty.

Types

type App

type App interface {
	// AppId 表示当前这个app的唯一id,可以用于分布式锁
	AppId() string
	// Version 定义当前版本
	Version() string
	// BaseFolder 定义项目基础地址
	BaseFolder() string
	// ConfigFolder 定义配置文件的地址
	ConfigFolder() string
	// LogFolder 定义了日志所在路径
	LogFolder() string
	// ProviderFolder 定义业务自己定义的服务提供者位置
	ProviderFolder() string
	// MiddlewareFolder 定义业务自己定义的中间件
	MiddlewareFolder() string
	// CommandFolder 定义业务定义的命令
	CommandFolder() string
	// RuntimeFolder 定义业务运行中间件信息
	RuntimeFolder() string
	// TestFolder 存放测试所需要的信息
	TestFolder() string
	// DeployFolder 存储部署的时候创建的文件夹
	DeployFolder() string

	AppFolder() string // 定义业务代码所在的目录,用于监控文件变更使用

	// LoadAppConfig 加载新的AppConfig,key为对应的函数转为小写下划线,比如ConfigFolder => config_folder
	LoadAppConfig(kv map[string]string)
}

App 定义接口

type CacheService added in v1.1.0

type CacheService interface {
	// Get 获取某个key对应的值
	Get(ctx context.Context, key string) (string, error)
	// GetObj 获取某个key对应的对象, 对象必须实现 https://pkg.golang.ir/encoding#BinaryUnMarshaler
	GetObj(ctx context.Context, key string, model interface{}) error
	// GetMany 获取某些key对应的值
	GetMany(ctx context.Context, keys []string) (map[string]string, error)

	// Set 设置某个key和值到缓存,带超时时间
	Set(ctx context.Context, key string, val string, timeout time.Duration) error
	// SetObj 设置某个key和对象到缓存, 对象必须实现 https://pkg.golang.ir/encoding#BinaryMarshaler
	SetObj(ctx context.Context, key string, val interface{}, time time.Duration) error
	// SetMany 设置多个key和值到缓存
	SetMany(ctx context.Context, data map[string]string, timeout time.Duration) error
	// SetForever 设置某个key和值到缓存,不带超时时间
	SetForever(ctx context.Context, key string, val string) error
	// SetForeverObj 设置某个key和对象到缓存,不带超时时间,对象必须实现 https://pkg.golang.ir/encoding#BinaryMarshaler
	SetForeverObj(ctx context.Context, key string, val interface{}) error

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

	// Remember 实现缓存的Cache-Aside模式, 先去缓存中根据key获取对象,如果有的话,返回,如果没有,调用RememberFunc 生成
	Remember(ctx context.Context, key string, timeout time.Duration, rememberFunc RememberFunc, model interface{}) error

	// Calc 往key对应的值中增加step计数
	Calc(ctx context.Context, key string, step int64) (int64, error)
	// Increment 往key对应的值中增加1
	Increment(ctx context.Context, key string) (int64, error)
	// Decrement 往key对应的值中减去1
	Decrement(ctx context.Context, key string) (int64, error)

	// Del 删除某个key
	Del(ctx context.Context, key string) error

	// DelMany 删除某些key
	DelMany(ctx context.Context, keys []string) error
}

CacheService 缓存服务

type Config

type Config interface {
	// IsExist 检查一个属性是否存在
	IsExist(key string) bool

	// Get 获取一个属性值
	Get(key string) interface{}

	// GetBool 获取一个bool属性
	GetBool(key string) bool

	// GetInt  获取一个bool属性
	GetInt(key string) int

	// GetFloat64 获取一个float64属性
	GetFloat64(key string) float64

	// GetTime 获取一个time属性
	GetTime(key string) time.Time

	// GetString 获取一个string属性
	GetString(key string) string

	// GetIntSlice 获取一个int数组属性
	GetIntSlice(key string) []int

	// GetStringSlice 获取一个string数组
	GetStringSlice(key string) []string

	// GetStringMap 获取一个string为key,interface为val的map
	GetStringMap(key string) map[string]interface{}

	// GetStringMapString 获取一个string为key,string为val的map
	GetStringMapString(key string) map[string]string
	// GetStringMapStringSlice 获取一个string为key,数组string为val的map
	GetStringMapStringSlice(key string) map[string][]string

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

Config 定义了配置文件服务,读取配置文件,支持点分割的路径读取 例如: .Get("app.name") 表示从app文件中读取name属性 建议使用 yaml 属性, https://yaml.org/spec/1.2/spec.html

type CtxFielder

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

CtxFielder 定义了从context中获取信息的方法 因为在具体业务开发中,我们很有可能把一些通用信息,比如 trace_id 等放在 context 里,这一部分信息也会希望取出放在日志的上下文字段中。 所以这里有一个从 context 中获取日志上下文字段的方法。在 framework/contract/log.go 中定义其为 CtxFielder

type DBConfig added in v1.1.0

type DBConfig struct {
	// 以下配置关于dsn
	WriteTimeout string `yaml:"write_timeout"` // 写超时时间
	Loc          string `yaml:"loc"`           // 时区
	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"`           // 直接传递dsn,如果传递了,其他关于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_idletime"` // 空闲最大生命周期

	// 以下配置关于gorm
	*gorm.Config // 集成gorm的配置
}

DBConfig 代表数据库连接的所有配置

func (*DBConfig) FormatDSN added in v1.1.0

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

type DBOption added in v1.1.0

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

DBOption 代表初始化时候的选项

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 获取当前的环境,建议分为developent/testing/production
	AppEnv() string

	// IsExist 判断一个环境变量是否被设置
	IsExist(string) bool

	// Get 获取环境变量, 如果没有设置,返回""
	Get(string) string

	// All 获取所有的的环境变量
	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 IDService

type IDService interface {
	NewID() string
}

type IORMService added in v1.1.0

type IORMService interface {
	GetDB(option ...DBOption) (*gorm.DB, error)
}

IORMService 表示传入的参数

type Kernel

type Kernel interface {
	// HttpEngine 提供gin的Engine结构
	HttpEngine() http.Handler
}

Kernel 接口提供框架最核心的架构

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 设置日志级别
	SetLevel(level LogLevel)
	// SetCtxFielder 从context中获取上下文字段field
	SetCtxFielder(handler CtxFielder)
	// SetFormatter 设置输出格式
	SetFormatter(formatter Formatter)
	// SetOutput 设置输出管道
	SetOutput(out io.Writer)
}

type LogLevel

type LogLevel uint32
const (
	// UnknownLevel 表示未知的日志级别
	UnknownLevel 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 RedisConfig added in v1.1.0

type RedisConfig struct {
	*redis.Options
}

RedisConfig 为框架定义Redis配置接口

func (*RedisConfig) Uniqkey added in v1.1.0

func (config *RedisConfig) Uniqkey() string

Uniqkey 用来唯一标识一个RedisConfig配置

type RedisOption added in v1.1.0

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

RedisOption 代表初始化时候的选项

type RedisService added in v1.1.0

type RedisService interface {
	// GetClient 获取redis连接实例
	GetClient(option ...RedisOption) (*redis.Client, error)
}

RedisService 标识一个redis服务

type RememberFunc added in v1.1.0

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

type SSHConfig added in v1.1.0

type SSHConfig struct {
	NetWork string
	Host    string
	Port    string
	*ssh.ClientConfig
}

SSHConfig 为ssh定义的配置结构

func (*SSHConfig) UniqKey added in v1.1.0

func (config *SSHConfig) UniqKey() string

UniqKey 用来唯一标识一个SSHConfig配置

type SSHOption added in v1.1.0

type SSHOption func(container framework.Container, config *SSHConfig) error

SSHOption 代表初始化时候的选项

type SSHService added in v1.1.0

type SSHService interface {
	// GetClient 获取ssh连接实例
	GetClient(option ...SSHOption) (*ssh.Client, error)
}

SSHService 表示一个ssh服务

type Trace

type Trace interface {
	WithTrace(c context.Context, trace *TraceContext) context.Context // 注册新地跟踪路由
	GetTrace(c context.Context) *TraceContext                         // 从当前上下文中获取traceContext
	NewTrace() *TraceContext                                          // 生成一个新的traceContext

	StartSpan(trace *TraceContext) *TraceContext // 为调用者生成traceContext
	ToMap(trace *TraceContext) map[string]string // 将traceContext 转成map

	ExtractHTTP(req *http.Request) *TraceContext                     // 从http.Request中获取traceContext
	InjectHTTP(req *http.Request, trace *TraceContext) *http.Request // 为http设置traceContext
}

type TraceContext

type TraceContext struct {
	TraceID  string // 唯一ID
	ParentID string // 父节点spanID
	SpanID   string // 当前节点 SpanID
	CSpanID  string // 子节点调用的SpanID,由调用方指定

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

TraceContext 根据 Google Dapper 定义

Jump to

Keyboard shortcuts

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