dinfra

package
v0.0.0-...-aa3d5c5 Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2023 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HttpHeader_XRequestID = "X-Request-Id"
)

Variables

View Source
var (
	ErrInvalidBase58 = errors.New("invalid base58")
	ErrInvalidBase32 = errors.New("invalid base32")
)
View Source
var (
	LogField_Source string = "Source"
	LogField_Track  string = "Track"
)

Functions

func DI_ConfigOptions

func DI_ConfigOptions[insType any](
	services di.ServiceCollector,
	path string,
	ins insType,
	opts ...func(insType),
)

将 config path 对应的数据绑定给 options 然后注入 IoC 容器

func DerefString

func DerefString(s *string) string

func Http_MiddlewareToEchoFunc

func Http_MiddlewareToEchoFunc(middleware HttpMiddleware) echo.MiddlewareFunc

将自定义 HttpMiddleware 接口转换为 echo 可用的 MiddlewareFunc

func MapToStruct

func MapToStruct[DstType any](from any) (*DstType, error)

func Strings_FirstToLower

func Strings_FirstToLower(str string) string

func Strings_FirstToUpper

func Strings_FirstToUpper(str string) string

func StructToMap

func StructToMap(from any) (map[string]any, error)

Types

type App

type App interface {
	// 启动
	Run() error
}

应用程序

type AppBuilder

type AppBuilder interface {
	// 依赖注入
	ConfigService(configFuncs ...ServiceConfigFunc)

	// 构建一个全新的应用
	Build() (App, error)
}

App 构建

type CanToMap

type CanToMap interface {
	ToMap() map[string]any
}

type Config

type Config interface {
	// 将配置绑定到结构体
	BindStruct(path string, dst any) error
}

配置

type Database

type Database struct {
	*gorm.DB // 简单内嵌 Gorm
}

数据库

type DatabaseProvider

type DatabaseProvider interface {
	// 通过服务 name 获取对应的数据库实例访问对象
	Provide(name string) (*Database, error)
}

数据库 provider

type HttpMiddleware

type HttpMiddleware interface {
	Handle(context echo.Context, next echo.HandlerFunc) error
}

自定义中间件接口,方便实现自定义的中间件( echo 原始的中间件实现方式,看起来比较难看,仅此而已)

type HttpServer

type HttpServer struct {
	*echo.Echo
}

简单包装的一个 http 服务

func (*HttpServer) Start

func (server *HttpServer) Start(address string) error

type ID

type ID int64

func ParseBase2

func ParseBase2(id string) (ID, error)

func ParseBase32

func ParseBase32(b []byte) (ID, error)

func ParseBase36

func ParseBase36(id string) (ID, error)

func ParseBase58

func ParseBase58(b []byte) (ID, error)

func ParseBase64

func ParseBase64(id string) (ID, error)

func ParseBytes

func ParseBytes(id []byte) (ID, error)

func ParseInt64

func ParseInt64(id int64) ID

func ParseIntBytes

func ParseIntBytes(id [8]byte) ID

func ParseString

func ParseString(id string) (ID, error)

func (ID) Base2

func (f ID) Base2() string

func (ID) Base32

func (f ID) Base32() string

func (ID) Base36

func (f ID) Base36() string

func (ID) Base58

func (f ID) Base58() string

func (ID) Base64

func (f ID) Base64() string

func (ID) Bytes

func (f ID) Bytes() []byte

func (ID) Int64

func (f ID) Int64() int64

func (ID) IntBytes

func (f ID) IntBytes() [8]byte

func (ID) String

func (f ID) String() string

type IdProvider

type IdProvider interface {
	Provide() ID
}

type JsonProperty

type JsonProperty struct {
	Name        string           `json:"name"` // 为了方便使用,兼容数组和 MAP 的存储形式
	Type        JsonPropertyType `json:"type"`
	Title       *string          `json:"title"`       // 最好是简短的
	Description *string          `json:"description"` // 说明,更长更加详细

	MaxLength *int64  `json:"maxLength"` // string 最大长度,非负
	MinLength *int64  `json:"minLength"` // string 最小长度,非负
	Pattern   *string `json:"pattern"`   // string 必须符合对应的正则表达式

	MultipleOf *int64 `json:"multipleOf"` // number integer 给定数字的倍数
	Minimum    *int64 `json:"minimum"`    // number integer 最小值 >=
	Maximum    *int64 `json:"maximum"`    // number integer 最大值 <=

	Properties map[string]*JsonProperty `json:"properties"`
	Required   []string                 `json:"required"`   // object 必须属性
	Updateable []string                 `json:"updateable"` // object 可以被更新的属性

	Items       *JsonPropertyType `json:"items"`       // array 列表项的说明
	MinItems    *int64            `json:"minItems"`    // array 数组最小长度
	MaxItems    *int64            `json:"maxItems"`    // array 数组最大长度
	UniqueItems *bool             `json:"uniqueItems"` // array 数组每个元素唯一

	Enum    []any `json:"enum"`
	Default any   `json:"default"` // 该值不用于在验证过程中填充缺失值。文档生成器或表单生成器等非验证工具可能会使用此值提示用户如何使用该值。
	Const   any   `json:"const"`   // 常量,固定值
}

json schema 属性描述

type JsonPropertyType

type JsonPropertyType string // json schema 支持的属性类型
var (
	JPT_Number  JsonPropertyType = "number"  // 整数类型
	JPT_Integer JsonPropertyType = "integer" // 数值类型
	JPT_Boolean JsonPropertyType = "boolean" // 布尔
	JPT_String  JsonPropertyType = "string"  // 字符串
	JPT_Object  JsonPropertyType = "object"  // 对象
	JPT_Array   JsonPropertyType = "array"   // 数组
	JPT_Null    JsonPropertyType = "null"    // 空

	JPT_Datetime JsonPropertyType = "datetime" // 时间(扩展定义)
)

type JsonSchema

type JsonSchema struct {
	*JsonProperty

	Schema *string `json:"$schema"` // 使用的 schema 版本
	ID     *string `json:"$id"`     //
}

json schema

func NewJsonSchema

func NewJsonSchema() *JsonSchema

type Logger

type Logger interface {
	WithField(key string, value interface{}) *logrus.Entry
	WithFields(fields logrus.Fields) *logrus.Entry
	WithError(err error) *logrus.Entry

	Trace(args ...interface{})
	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})
}

type MicroService

type MicroService struct {
	ID       string            // 实例唯一 ID
	Name     string            // 服务名称
	Address  string            // 可访问地址
	Port     int               // 可访问端口
	Env      string            // 归属的环境
	Tags     []string          // 标签
	Metadata map[string]string // 服务注册时附加数据
}

对应微服务一个实例

func (*MicroService) MetaBindToStruct

func (ms *MicroService) MetaBindToStruct(dst any) error

将附加数据绑定到结构体

type OnChangeFunc

type OnChangeFunc func() error

type Options

type Options interface {
	// option 的使用者可以通过注册 listener 来响应 config 的运行时变更
	OnChange(listener OnChangeFunc)
}

各个服务的可选参数

type Result

type Result struct {
	Code int    `json:"code"`
	Data any    `json:"data,omitempty"`
	Msg  string `json:"msg,omitempty"`
}

type ServiceConfigFunc

type ServiceConfigFunc func(di.ServiceCollector)

依赖注入配置整个程序

type ServiceRegister

type ServiceRegister interface {
	// 获取 name 服务对应的信息
	Get(name string) (*MicroService, error)

	// 向注册中心注册服务
	Register(service *MicroService) error
}

注册中心

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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