config

package
v0.3.11 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// UnknownNodeType 未知节点类型
	UnknownNodeType = -1
)

Variables

View Source
var (
	// AlarmOn 全局报警开关 (区别是否被排除报警, 如测试节点)
	AlarmOn atomic.Bool

	// Whitelist 接口 IP 白名单配置
	Whitelist map[*net.IPNet]int64

	// Blacklist 接口 IP 黑名单配置
	Blacklist map[*net.IPNet]int64
)
View Source
var (
	// BinName !!! 这个变量决定启动时加载的默认配置名, 日志名等
	// 可以由 LDFLAGS 编译时注入
	// 可以在应用程序 func init() { BinName = "yourapp" ... }
	// 也可启动程序时命令行参数指定主配置文件
	BinName = "ffapp"

	AppName = "FF.App"
	DebName = "ff-app"
)
View Source
var (
	// RootPath 程序运行绝对路径 !!! 决定加载的默认配置目录, 环境文件目录, 日志目录
	RootPath        string
	DefaultRootPath = utils.ExecutableDir(true)
	DefaultLogPath  = filepath.Join(DefaultRootPath, "..", "log")

	// LogDaemon 守护进程日志, 路径为空时不记录日志
	LogDaemon = filepath.Join(DefaultLogPath, "daemon.log")

	// LogPath 日志路径
	LogPath string
	LogFile string

	// ConfigPath 主配置文件绝对路径, .env 配置文件路径
	ConfigPath  string
	ConfigFile  string
	EnvFilePath string
	EnvMainFile string

	// WhitelistConfigFile IP 白名单配置文件路径
	WhitelistConfigFile        string
	DefaultWhitelistConfigFile string
	// BlacklistConfigFile IP 黑名单配置文件路径
	BlacklistConfigFile        string
	DefaultBlacklistConfigFile string

	// ReqUserAgent Request 请求名称
	ReqUserAgent string
)
View Source
var (
	Debug bool

	// LogLevel 日志级别: -1Trace 0Debug 1Info 2Warn(默认) 3Error 4Fatal 5Panic 6NoLevel 7Off
	LogLevel = 2
	// LogSamplePeriodDur 抽样日志设置 (每秒最多 10 个日志)
	LogSamplePeriodDur = time.Second
	LogSampleBurst     = 10
	// LogFileMaxSize 每 100M 自动切割, 保留 30 天内最近 10 个日志文件
	LogFileMaxSize    = 100
	LogFileMaxBackups = 10
	LogFileMaxAge     = 30
	// LogPostIntervalDuration 日志推送时间间隔(秒)
	LogPostIntervalDuration = 2 * time.Second
	// LogPostBatchNum 单次批量提交数据最大条数或最大字节数
	LogPostBatchNum   = 2000
	LogPostBatchBytes = 2 << 20

	// BaseSecretValue 项目基础密钥值, 与 config.Config().SYSConf.BaseSecretValue 相同
	BaseSecretValue string
	// BaseSecretEnvName 项目基础密钥 (环境变量名)
	BaseSecretEnvName = "BASE_SECRET_KEY"
	// BaseSecretKeyNameEnvName 用于在环境变量中指定上一行设置的值的键名, 而不是使用默认的: BASE_SECRET_KEY
	BaseSecretKeyNameEnvName = "BASE_SECRET_KEY_NAME"

	// BaseSecretSalt 用于解密基础密钥值的密钥的前半部分, 盐 (编译在程序中), 后半部分为 AppName 值
	BaseSecretSalt = "Fufu↑777發彡 " // #nosec G101
	// BaseSecretSaltEnvName 环境变量中读取上一行设置的值的键名, 而不是使用上一行中设置的固定值
	BaseSecretSaltEnvName = "BASE_SECRET_SALT"

	// WatcherIntervalDuration 文件变化监控时间间隔
	WatcherIntervalDuration = 2 * time.Minute

	// DefaultLoadConfigInterval 通用配置定时加载时间, 各类 Sender 运行中加载最新配置
	DefaultLoadConfigInterval = 1 * time.Minute

	// DefaultRandomWait 执行前随机等待最大秒数
	DefaultRandomWait = 120

	// ReqTimeoutDuration 作为客户端发起请求默认超时时间
	ReqTimeoutDuration      = 30 * time.Second
	ReqTimeoutShortDuration = 3 * time.Second

	// ChanxInitCap 无限缓冲信道默认初始化缓冲大小
	ChanxInitCap = 50
	// ChanxMaxBufCap 无限缓冲信道最大缓冲数量, 0 为无限, 超过限制(ChanxInitCap + ChanxMaxBufCap)丢弃数据
	ChanxMaxBufCap = 500000

	// ServiceNameSuffix systemctl 服务名后缀 (Ubuntu)
	ServiceNameSuffix = ".service"
)
View Source
var (
	// WebServerAddr 缺省的 HTTP 接口端口
	WebServerAddr = ":12366"
	// WebServerHttpsAddr 缺省的 HTTPS 接口端口
	WebServerHttpsAddr = ":12377"

	// WebCertFileEnvName 默认证书路径环境变量名
	WebCertFileEnvName = "WEB_CERT_FILE"
	WebKeyFileEnvName  = "WEB_KEY_FILE"

	// WebSignKeyEnvName 接口签名密钥加密串环境变量名
	WebSignKeyEnvName = "WEB_SIGN_KEY"
	// WebSignTTLDefault 接口签名有效生命周期(秒数, 默认: 60, 最小 5)
	WebSignTTLDefault = 60
	WebSignTTLMin     = 5

	// WebLogInfoKey 接口日志附带请求信息, 上下文键名
	WebLogInfoKey = "_WLIK_"

	// WebLogSlowResponse 慢日志条件
	WebLogSlowResponse = 5 * time.Second

	// WebLogMinStatusCode Web 日志响应码条件值
	WebLogMinStatusCode = 500

	// WebTimeout Web 请求超时
	WebTimeout = 30 * time.Second

	// WebBodyLimit POST 最大 8M, 超过该值影响: 413 Request Entity Too Large
	WebBodyLimit = 8 << 20

	// WebTokenSalt 用于加密代理客户端请求 IP 的盐, 默认为 BaseSecretValue 项目基础密钥值
	WebTokenSalt string
)
View Source
var (
	// DefaultTimeZone 缺省时区名称
	DefaultTimeZone string

	// DefaultTimeLocation 默认时区
	// 优先尝试解析中国时区 (GMT+8), 失败(Windows)后使用本地时区
	DefaultTimeLocation *time.Location
)
View Source
var (
	Version   = "v0.0.1"
	GoVersion = ""
	GitCommit = ""

	// DebVersion 当前的包版本信息
	DebVersion = ""
)
View Source
var DefaultGOMAXPROCS = utils.MaxInt(runtime.NumCPU(), 4)

DefaultGOMAXPROCS 缺省的并发配置, 最少 4

View Source
var ExtraEnvFiles []string
View Source
var (
	NodeInfoFile = ""
)

Functions

func GetDataSource

func GetDataSource(args any) error

GetDataSource 获取源数据配置 可在应用级重定义该函数, 覆盖 common.Funcs["GetDataSource"]

func GetDataSourceBody added in v0.1.1

func GetDataSourceBody(params DataSourceArgs) (string, error)

func GetDevName

func GetDevName(name string) string

GetDevName 去除服务名称后缀, 得到包名

func GetNodeIPFromAPI added in v0.0.20

func GetNodeIPFromAPI(api string) string

GetNodeIPFromAPI 请求 API, 返回 IP 结果

func IsSkipRemoteConfig

func IsSkipRemoteConfig() bool

IsSkipRemoteConfig 是否只使用本地配置文件

func LoadConfig added in v0.0.16

func LoadConfig() error

LoadConfig 加载配置

func ParseDuration added in v0.2.0

func ParseDuration(s string, defDur time.Duration, minDur ...time.Duration) (time.Duration, error)

ParseDuration 解析时间间隔字符串

func ParseRemoteFileConfig added in v0.2.0

func ParseRemoteFileConfig(cfg *FilesConf, secret string) error

ParseRemoteFileConfig 解析远端配置获取配置项

Types

type DataSourceArgs

type DataSourceArgs struct {
	Time time.Time
	Conf FilesConf
}

type FilesConf

type FilesConf struct {
	Path            string `json:"path"`
	Method          string `json:"method"`
	SecretName      string `json:"secret_name"`
	API             string `json:"api"`
	Interval        int    `json:"interval"`
	RandomWait      int    `json:"random_wait"`
	SecretValue     string `json:"-"`
	GetConfDuration time.Duration
}

type LogConf

type LogConf struct {
	NoColor              bool   `json:"no_color"`
	Level                int    `json:"level"`
	File                 string `json:"file"`
	Period               uint32 `json:"period"`
	Burst                uint32 `json:"burst"`
	MaxSize              int64  `json:"max_size"`
	MaxBackups           int    `json:"max_backups"`
	MaxAge               int    `json:"max_age"`
	PostAPI              string `json:"post_api"`
	PostAPIEnv           string `json:"post_api_env"`
	PostAlarmAPI         string `json:"post_alarm_api"`
	PostAlarmAPIEnv      string `json:"post_alarm_api_env"`
	AlarmCode            string `json:"alarm_code"`
	AlarmCodeEnv         string `json:"alarm_code_env"`
	PostInterval         int    `json:"post_interval"`
	PostBatchNum         int    `json:"post_batch_num"`
	PostBatchMB          int    `json:"post_batch_mb"`
	PeriodDuration       time.Duration
	PostIntervalDuration time.Duration
	PostBatchBytes       int
}

type M added in v0.0.10

type M struct{}

func (*M) Runtime added in v0.0.10

func (m *M) Runtime() error

Runtime 重新加载配置时运行

func (*M) Start added in v0.0.10

func (m *M) Start() error

Start 程序启动时初始化

func (*M) Stop added in v0.0.10

func (m *M) Stop() error

Stop 程序退出时运行

type MainConf

type MainConf struct {
	SYSConf       SYSConf   `json:"sys_conf"`
	MainConf      FilesConf `json:"main_conf"`
	LogConf       LogConf   `json:"log_conf"`
	NodeConf      NodeConf  `json:"node_conf"`
	WebConf       WebConf   `json:"web_conf"`
	Whitelist     []string  `json:"whitelist"`
	Blacklist     []string  `json:"blacklist"`
	WhitelistConf FilesConf `json:"whitelist_conf"`
	BlacklistConf FilesConf `json:"blacklist_conf"`
}

MainConf 接口配置

func Config

func Config() *MainConf

Config 获取全局配置

type NodeConf

type NodeConf struct {
	NodeInfoFile string `json:"node_info_file"`
	IPAPI        string `json:"ip_api"`
	NodeInfo     NodeInfo
}

type NodeInfo

type NodeInfo struct {
	// 主机名和网卡 IP
	Hostname string `json:"hostname"`
	HostIP   string `json:"host_ip"`

	NodeID   int    `json:"node_id"`
	NodeIP   string `json:"service_ip"`
	NodeName string `json:"node_name"`
	NodeDesc string `json:"node_desc"`
	NodeType int    `json:"node_type"`
}

NodeInfo 节点信息

type RespDataSource added in v0.1.1

type RespDataSource struct {
	OK   int              `json:"ok"`
	Msg  string           `json:"msg"`
	Data []map[string]any `json:"data"`
}

RespDataSource 数据源响应结构体

type SYSConf

type SYSConf struct {
	RestartMain             bool     `json:"restart_main"`
	TimeSyncType            string   `json:"time_sync_type"`
	WatcherInterval         string   `json:"watcher_interval"`
	ReqTimeout              string   `json:"req_timeout"`
	ReqMaxRetries           int      `json:"req_max_retries"`
	DebVersion              string   `json:"deb_version"`
	CanaryDeployment        uint64   `json:"canary_deployment"`
	SkipRemoteConfig        string   `json:"skip_remote_config"`
	EnvFiles                []string `json:"env_files"`
	BaseSecretValue         string   `json:"-"`
	WatcherIntervalDuration time.Duration
	ReqTimeoutDuration      time.Duration
}

SYSConf 主配置, 变量意义见配置文件中的描述及 default.go 中的默认值

type WebConf added in v0.0.4

type WebConf struct {
	PProfAddr       string   `json:"pprof_addr"`
	ServerAddr      string   `json:"server_addr"`
	ServerHttpsAddr string   `json:"server_https_addr"`
	StatsPath       string   `json:"stats_path"`
	TrustedProxies  []string `json:"trusted_proxies"`

	// Gin
	TrustedPlatform string `json:"trusted_platform"`

	// Fiber
	EnableTrustedProxyCheck bool   `json:"enable_trusted_proxy_check"`
	ProxyHeader             string `json:"proxy_header"`

	// Fiber 默认不减少内存占用, 这里改为默认减少内存占用(可能增加 CPU 占用)
	DisableReduceMemoryUsage bool `json:"disable_reduce_memory_usage"`

	// Fiber 短连接模式
	DisableKeepalive bool `json:"disable_keepalive"`

	// Fiber 请求体大小限制, 0 为默认: 8 * 1024 * 1024, -1 表示不限制
	BodyLimit int `json:"body_limit"`

	// 黑白名单中间件缓存容量配置, 键生命周期秒数
	WhitelistLRUCapacity uint32 `json:"whitelist_lru_capacity"`
	WhitelistLRULifetime uint32 `json:"whitelist_lru_lifetime"`
	BlacklistLRUCapacity uint32 `json:"blacklist_lru_capacity"`
	BlacklistLRULifetime uint32 `json:"blacklist_lru_lifetime"`

	// 接口签名密钥生命周期和签名密钥值
	SignTTL int64  `json:"sign_ttl"`
	SignKey string `json:"-"`

	CertFile string `json:"-"`
	KeyFile  string `json:"-"`
}

Jump to

Keyboard shortcuts

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