config

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: Apache-2.0 Imports: 17 Imported by: 0

README

概述

核心库提供配置读取、数据库访问、缓存访问、日志、HTTP服务等基础组件

初始化

环境初始化

env.SetupConfig("./settings.template.yml")

初始化环境之后即可通过env中的对象使用配置、数据库、缓存、日志等功能

HTTP服务器启动

启动HTTP服务器,会自动进行环境初始化,不比再执行env.SetupConfig()方法,只需要执行:

httpserver.InitEngine("./settings.template.yml")

HTTP服务器基于gin进行封装,关于如何使用路由、中间件请看下文中HTTP服务器部分。

配置

获取标准配置

标准配置是预先在核心库定义的一套固定格式的配置,可通过“标准配置对象”读取标准配置。如果不是按照标准配置格式配置的内容,通过“标准配置对象”获取不到。

// 获取数据库连接配置
fmt.Prinln(env.DefaultConfig.Database.Source)

按路径获取配置

标准配置或者用户在yml配置文件中添加的其它非标准配置,均可通过“配置操作对象”获取。

// 获取数据库连接配置
env.ConfigOperator.Get("iot.database.source")

数据库

使用DB对象

核心库根据配置文件中的配置初始化“Db对象”,“Db对象”是一个gorm对象,gorm的标准接口均可使用

// 查询数据表
env.Db.Table("sys_svc").Select("instance_id, path, bin, arg").Rows()

使用gin上下文中的DB对象

如果启动了HTTP服务,核心库会自动将“Db对象”注入到gin的contex中,可在context中获取并使用

 // 在gin上下文中获取db对象
 db := c.Get("db")
 db.Table("sys_svc").Select("instance_id, path, bin, arg").Rows()

缓存

如果配置文件中配置了redis节点,“缓存对象”使用redis管理缓存;如果没有配置redis节点,缓存对象使用内存管理缓存,支持线程安全。

 // 设置一个100s过期的缓存并获取
 cache := env.Cache
 cache.Set("token", "mytoken", 100)
 cache.Get("token")

日志

核心库根据配置文件初始化“日志对象”。如果“driver”为“default”,“日志对象”使用标准库作为底层日志库,如果“driver”为“zap”,日志对象使用“zap”作为底层日志库。

 // 设置一个100s过期的缓存并获取
 myLog := env.Log
 h1 := myLog.WithFields(map[string]interface{}{"key1": "val1"})
 h1.Trace("trace_msg1")
 h1.Warn("warn_msg1")
 h1.Infof("Info message %s", message)

HTTP服务器

启动HTTP服务器非常简单,并且可以设置自定义路由和中间件。

// 如有需要,向内核注册自己需要的中间件,自定义的中间件不在核心库中
// 可选 限流
httpserver.RegisterMiddlewareFunction(middleware.Sentinel())
// 可选 自动增加requestId
httpserver.RegisterMiddlewareFunction(middleware.RequestId(pkg.TrafficKey))
// 可选 请求日志记录到数据库
httpserver.RegisterMiddlewareFunction(middleware.LoggerToFile())
// 可选 自定义错误处理
httpserver.RegisterMiddlewareFunction(middleware.CustomError)
// 可选 http header中的缓存设置
httpserver.RegisterMiddlewareFunction(middleware.NoCache)
// 可选 跨域处理
httpserver.RegisterMiddlewareFunction(middleware.Options)
// 可选 http header中的安全设置
httpserver.RegisterMiddlewareFunction(middleware.Secure)

// 必须,初始化引擎
httpserver.InitEngine(_var.ConfigFile)
// 可选,初始化JWT验证方法组,JWT的部分回调可以自定义,
// 回调方法在iot-device项目的common目录中可以找到
jwtMiddleware, _ := middleware.AuthInit()
// 必须,向内核注册自己的业务模块路由,如不使用中间件,请使用InitRouters方法
httpserver.InitAuthRouters(Routers, jwtMiddleware)
// 必须,启动HTTP服务器
_ = httpserver.Run()

路由定义方式如下:

func Routers(r *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
	router := r.Group("/auth")
	{
		router.POST("/modify", handler)
		router.POST("/delete", handler)
		router.GET("/get", handler)
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DatabaseConfig  = new(Database)
	DatabasesConfig = make(map[string]*Database)
)
View Source
var CacheConfig = new(Cache)

CacheConfig cache配置

View Source
var (
	ExtendConfig interface{}
)
View Source
var GenConfig = new(Gen)
View Source
var HttpConfig = new(Http)
View Source
var I18nConfig = new(I18n)
View Source
var JwtConfig = new(Jwt)
View Source
var LockerConfig = new(Locker)
View Source
var LoggerConfig = new(Logger)
View Source
var MqttConfig = new(Mqtt)
View Source
var QueueConfig = new(Queue)
View Source
var SslConfig = new(Ssl)

Functions

func GetRedisClient

func GetRedisClient() *redis.Client

GetRedisClient 获取redis客户端

func SetRedisClient

func SetRedisClient(c *redis.Client)

SetRedisClient 设置redis客户端

func Setup

func Setup(s source.Source,
	fs ...func())

Setup 载入配置文件

Types

type Cache

type Cache struct {
	Redis  *RedisConnectOptions
	Memory interface{}
}

func (Cache) Setup

func (e Cache) Setup() (storage.AdapterCache, error)

Setup 构造cache 顺序 redis > 其他 > memory

type Config

type Config struct {
	Http      *Http                 `yaml:"http"`
	Mqtt      *Mqtt                 `yaml:"mqtt"`
	Ssl       *Ssl                  `yaml:"ssl"`
	Logger    *Logger               `yaml:"logger"`
	Jwt       *Jwt                  `yaml:"jwt"`
	Database  *Database             `yaml:"database"`
	Databases *map[string]*Database `yaml:"databases"`
	Gen       *Gen                  `yaml:"gen"`
	Cache     *Cache                `yaml:"cache"`
	Queue     *Queue                `yaml:"queue"`
	Locker    *Locker               `yaml:"locker"`
	Extend    interface{}           `yaml:"extend"`
	I18n      *I18n                 `yaml:"i18n"`
}

Config 配置集合

type DBResolverConfig

type DBResolverConfig struct {
	Sources  []string
	Replicas []string
	Policy   string
	Tables   []string
}

type Database

type Database struct {
	Driver          string
	Source          string
	ConnMaxIdleTime int
	ConnMaxLifeTime int
	MaxIdleConns    int
	MaxOpenConns    int
	Registers       []DBResolverConfig
}

type Gen

type Gen struct {
	DBName    string
	FrontPath string
}

type Http

type Http struct {
	ReadTimeout   int
	WriterTimeout int
	Host          string
	Port          int64
	Name          string
	JwtSecret     string
	Mode          string
	DemoMsg       string
	EnableDP      bool
}

type I18n

type I18n struct {
	Path          string
	DefaultLocale string
}

type Jwt

type Jwt struct {
	Secret  string
	Timeout int64
}

type Locker

type Locker struct {
	Redis *RedisConnectOptions
}

func (Locker) Empty

func (e Locker) Empty() bool

Empty 空设置

func (Locker) Setup

func (e Locker) Setup() (storage.AdapterLocker, error)

Setup 启用顺序 redis > 其他 > memory

type Logger

type Logger struct {
	Type      string
	Path      string
	Level     string
	Stdout    string
	EnabledDB bool
	Cap       uint
}

func (Logger) Setup

func (e Logger) Setup()

Setup 设置logger

type Mqtt

type Mqtt struct {
	Host    string
	Port    int64
	User    string
	Passwd  string
	Timeout int
}

type NSQOptions

type NSQOptions struct {
	DialTimeout time.Duration `opt:"dial_timeout" default:"1s"`

	// Deadlines for network reads and writes
	ReadTimeout  time.Duration `opt:"read_timeout" min:"100ms" max:"5m" default:"60s"`
	WriteTimeout time.Duration `opt:"write_timeout" min:"100ms" max:"5m" default:"1s"`

	// Addresses is the local address to use when dialing an nsqd.
	Addresses []string `opt:"addresses"`

	// Duration between polling lookupd for new producers, and fractional jitter to add to
	// the lookupd pool loop. this helps evenly distribute requests even if multiple consumers
	// restart at the same time
	//
	// NOTE: when not using nsqlookupd, LookupdPollInterval represents the duration of time between
	// reconnection attempts
	LookupdPollInterval time.Duration `opt:"lookupd_poll_interval" min:"10ms" max:"5m" default:"60s"`
	LookupdPollJitter   float64       `opt:"lookupd_poll_jitter" min:"0" max:"1" default:"0.3"`

	// Maximum duration when REQueueing (for doubling of deferred requeue)
	MaxRequeueDelay     time.Duration `opt:"max_requeue_delay" min:"0" max:"60m" default:"15m"`
	DefaultRequeueDelay time.Duration `opt:"default_requeue_delay" min:"0" max:"60m" default:"90s"`

	// Maximum amount of time to backoff when processing fails 0 == no backoff
	MaxBackoffDuration time.Duration `opt:"max_backoff_duration" min:"0" max:"60m" default:"2m"`
	// Unit of time for calculating consumer backoff
	BackoffMultiplier time.Duration `opt:"backoff_multiplier" min:"0" max:"60m" default:"1s"`

	// Maximum number of times this consumer will attempt to process a message before giving up
	MaxAttempts uint16 `opt:"max_attempts" min:"0" max:"65535" default:"5"`

	// Duration to wait for a message from an nsqd when in a state where RDY
	// counts are re-distributed (e.g. max_in_flight < num_producers)
	LowRdyIdleTimeout time.Duration `opt:"low_rdy_idle_timeout" min:"1s" max:"5m" default:"10s"`
	// Duration to wait until redistributing RDY for an nsqd regardless of LowRdyIdleTimeout
	LowRdyTimeout time.Duration `opt:"low_rdy_timeout" min:"1s" max:"5m" default:"30s"`
	// Duration between redistributing max-in-flight to connections
	RDYRedistributeInterval time.Duration `opt:"rdy_redistribute_interval" min:"1ms" max:"5s" default:"5s"`

	// Identifiers sent to nsqd representing this client
	// UserAgent is in the spirit of HTTP (default: "<client_library_name>/<version>")
	ClientID  string `opt:"client_id"` // (defaults: short hostname)
	Hostname  string `opt:"hostname"`
	UserAgent string `opt:"user_agent"`

	// Duration of time between heartbeats. This must be less than ReadTimeout
	HeartbeatInterval time.Duration `opt:"heartbeat_interval" default:"30s"`
	// Integer percentage to sample the channel (requires nsqd 0.2.25+)
	SampleRate int32 `opt:"sample_rate" min:"0" max:"99"`

	Tls *Tls `yaml:"tls" json:"tls"`

	// Compression Settings
	Deflate      bool `opt:"deflate"`
	DeflateLevel int  `opt:"deflate_level" min:"1" max:"9" default:"6"`
	Snappy       bool `opt:"snappy"`

	// Size of the buffer (in bytes) used by nsqd for buffering writes to this connection
	OutputBufferSize int64 `opt:"output_buffer_size" default:"16384"`
	// Timeout used by nsqd before flushing buffered writes (set to 0 to disable).
	//
	// WARNING: configuring clients with an extremely low
	// (< 25ms) output_buffer_timeout has a significant effect
	// on nsqd CPU usage (particularly with > 50 clients connected).
	OutputBufferTimeout time.Duration `opt:"output_buffer_timeout" default:"250ms"`

	// Maximum number of messages to allow in flight (concurrency knob)
	MaxInFlight int `opt:"max_in_flight" min:"0" default:"1"`

	// The server-side message timeout for messages delivered to this client
	MsgTimeout time.Duration `opt:"msg_timeout" min:"0"`

	// secret for nsqd authentication (requires nsqd 0.2.29+)
	AuthSecret string `opt:"auth_secret"`
}

func (NSQOptions) GetNSQOptions

func (e NSQOptions) GetNSQOptions() (*nsq.Config, error)

type Queue

type Queue struct {
	Redis  *QueueRedis
	Memory *QueueMemory
	NSQ    *QueueNSQ `json:"nsq" yaml:"nsq"`
}

func (Queue) Empty

func (e Queue) Empty() bool

Empty 空设置

func (Queue) Setup

func (e Queue) Setup() (storage.AdapterQueue, error)

Setup 启用顺序 redis > 其他 > memory

type QueueMemory

type QueueMemory struct {
	PoolSize uint
}

type QueueNSQ

type QueueNSQ struct {
	NSQOptions
	ChannelPrefix string
}

type QueueRedis

type QueueRedis struct {
	RedisConnectOptions
	Producer *redisqueue.ProducerOptions
	Consumer *redisqueue.ConsumerOptions
}

type RedisConnectOptions

type RedisConnectOptions struct {
	Network    string `yaml:"network" json:"network"`
	Addr       string `yaml:"addr" json:"addr"`
	Username   string `yaml:"username" json:"username"`
	Password   string `yaml:"password" json:"password"`
	DB         int    `yaml:"db" json:"db"`
	PoolSize   int    `yaml:"pool_size" json:"pool_size"`
	Tls        *Tls   `yaml:"tls" json:"tls"`
	MaxRetries int    `yaml:"max_retries" json:"max_retries"`
}

func (RedisConnectOptions) GetRedisOptions

func (e RedisConnectOptions) GetRedisOptions() (*redis.Options, error)

type Settings

type Settings struct {
	Iot Config `yaml:"iot"`
	// contains filtered or unexported fields
}

Settings 兼容原先的配置结构

func (*Settings) Init

func (e *Settings) Init()

func (*Settings) OnChange

func (e *Settings) OnChange()

type Ssl

type Ssl struct {
	KeyStr string
	Pem    string
	Enable bool
	Domain string
}

type Tls

type Tls struct {
	Cert string `yaml:"cert" json:"cert"`
	Key  string `yaml:"key" json:"key"`
	Ca   string `yaml:"ca" json:"ca"`
}

Directories

Path Synopsis
Package config is an interface for dynamic configuration.
Package config is an interface for dynamic configuration.
encoder
Package encoder handles source encoding formats
Package encoder handles source encoding formats
loader
package loader manages loading from multiple sources
package loader manages loading from multiple sources
reader
Package reader parses change sets and provides config values
Package reader parses change sets and provides config values
secrets
Package secrets is an interface for encrypting and decrypting secrets
Package secrets is an interface for encrypting and decrypting secrets
secrets/box
Package box is an asymmetric implementation of config/secrets using nacl/box
Package box is an asymmetric implementation of config/secrets using nacl/box
secrets/secretbox
Package secretbox is a config/secrets implementation that uses nacl/secretbox to do symmetric encryption / verification
Package secretbox is a config/secrets implementation that uses nacl/secretbox to do symmetric encryption / verification
source
Package source is the interface for sources
Package source is the interface for sources
source/file
Package file is a file source.
Package file is a file source.
source/memory
Package memory is a memory source
Package memory is a memory source

Jump to

Keyboard shortcuts

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