initialize

package module
v0.0.42 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2024 License: MIT Imports: 29 Imported by: 5

README

initialize

基于反射自动注入的配置及dao注入初始化,并暴露一个全局变量,记录模块信息 initialize

example

go run _example/main.go -c _example/config.toml

一个应用的启动,应该如此简单

config(配置)

支持nacos,local file,http请求作为配置中心,可扩展支持etcd,apollo,viper(获取配置代理,底层是其他配置中心),支持viper支持的所有格式("json", "toml", "yaml", "yml", "properties", "props", "prop", "hcl", "tfvars", "dotenv", "env", "ini")的配置文件, 支持dev,test,prod环境本,启动命令区分

配置模板
# dev | test | stage | prod |...
Env = "dev" # 将会选择与Env名字相同的环境配置
[dev]
ConfigTemplateDir = "." # 模板目录,将会生成配置模板

仅需以上最小配置,点击启动,即可生成配置模板 如果还是麻烦,试试直接用 --format ${配置格式} -e ${环境} -p ${模板路径} 启动吧

启动配置

仅需配置配置中心,后续配置均从配置中心拉取及自动更新

Module = "hoper"
# dev | test | stage | prod |...
Env = "dev" # 将会选择与Env名字相同的环境配置

[dev] 
debug = true
ConfigTemplateDir = "." # 将会生成配置模板
# 上方是一个个初始配置,如果不知道如何进行接下来的配置,可以先启动生成配置模板
[dev.ConfigCenter]
Type = "local"
Watch  = true
NoInject = ["Apollo","Etcd", "Es"]

[dev.ConfigCenter.local]
Debug = true
ConfigPath = "local.toml"
ReloadType = "fsnotify"

[dev.ConfigCenter.http]
Interval = 10000000000
Url = "http://localhost:6666/local.toml"

[dev.ConfigCenter.nacos]
DataId = "pro"
Group = "DEFAULT_GROUP"

[[dev.ConfigCenter.nacos.ServerConfigs]]
Scheme = "http"
IpAddr = "nacos"
Port = 9000
GrpcPort = 10000

[dev.ConfigCenter.nacos.ClientConfig]
NamespaceId = "xxx"
username = "nacos"
password = "nacos"
LogLevel = "debug"

import(
  "github.com/hopeio/cherry"
)
type config struct {
	//自定义的配置
	Customize serverConfig
}
type serverConfig struct{
    TokenMaxAge time.Duration
}

var Conf = &config{}
// 注入配置前初始化
func (c *config) BeforeInject() {
    c.Customize.TokenMaxAge = time.Second * 60 * 60 * 24
}
// 注入配置后初始化
func (c *config) AfterInject() {
	c.Customize.TokenMaxAge = time.Second * 60 * 60 * 24 * c.Customize.TokenMaxAge
}

func main() {
    //配置初始化应该在第一位
    defer initialize.Start(Conf, nil)()
}

如果还有Dao要初始化

import(
    "github.com/hopeio/initialize/conf_dao/gormdb/sqlite"
    initredis "github.com/hopeio/initialize/conf_dao/redis"
)
// dao dao.
type dao struct {
	// GORMDB 数据库连接
	GORMDB   *sqlite.DB
	StdDB    *sql.DB
}
// 注入配置前初始化
func (c *dao) BeforeInject() {
}
// 注入配置后初始化
func (c *dao) AfterInjectConfig() {
}
// 注入dao后初始化
func (d *dao) AfterInject() {
	db := d.GORMDB
	db.Callback().Create().Remove("gorm:save_before_associations")
	db.Callback().Create().Remove("gorm:save_after_associations")
	db.Callback().Update().Remove("gorm:save_before_associations")
	db.Callback().Update().Remove("gorm:save_after_associations")

	d.StdDB, _ = db.DB()
}
func main() {
    defer initialize.Start(Conf, dao)()
}

原生集成了redis,gormdb(mysql,postgressql,sqlite),kafka,pebbledb,apollo,badgerdb,etcd,elasticsearch,nsq,ristretto,viper等,并且非常简单的支持自定义扩展,不局限于Dao对象,任何对象都支持根据配置自动注入生成

基于环境的配置

需要配置中心的配置

生成模板

配置中心模板最少配置

env = "dev"

[dev]
ConfigTemplateDir = "."

如果单纯想生成模板,可以不用基本配置,直接用--format ${配置格式} -e ${环境} -p ${模板路径} 启动

单配置文件

如果你的项目不需要分环境,那么不要env这个配置,不要 -e flag,直接在启动目录放一个config.xxx文件即可 或者可以手动指定-c ${配置文件}

生成模板

要为单配置文件生成模板--format ${配置格式} -p ${模板路径}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EmbeddedPresetsType = reflect.TypeOf((*EmbeddedPresets)(nil)).Elem()

Functions

func Defer added in v0.0.35

func Defer()

func GlobalConfig

func GlobalConfig() *globalConfig

func Init added in v0.0.35

func Init(conf Config, dao Dao, configCenter ...conf_center.ConfigCenter)
func init(){
  	initialize.Init(conf, dao)
}

func ParseTagSetting

func ParseTagSetting(str string, sep string, settings any)

ParseTagSetting default sep ;

func RegisterDeferFunc

func RegisterDeferFunc(deferf ...func())

func Start

func Start(conf Config, dao Dao, configCenter ...conf_center.ConfigCenter) func()
func main(){
	defer initialize.Start(conf, dao)()
}

Types

type AfterInject added in v0.0.33

type AfterInject interface {
	AfterInject()
}

type AfterInjectConfig added in v0.0.33

type AfterInjectConfig interface {
	AfterInjectConfig()
}

type AfterInjectConfigWithRoot added in v0.0.33

type AfterInjectConfigWithRoot interface {
	AfterInjectConfigWithRoot(*rootconf.RootConfig)
}

type AfterInjectWithRoot added in v0.0.33

type AfterInjectWithRoot interface {
	AfterInjectWithRoot(*rootconf.RootConfig)
}

type BeforeInject added in v0.0.33

type BeforeInject interface {
	BeforeInject()
}

type BeforeInjectWithRoot added in v0.0.33

type BeforeInjectWithRoot interface {
	BeforeInjectWithRoot(*rootconf.RootConfig)
}

type BuiltinConfig

type BuiltinConfig struct {
	Log log.Config
}

type Config

type Config interface {
	// 注入之前设置默认值
	BeforeInject
	// 注入之后初始化
	AfterInject
}

type Dao

type Dao interface {
	BeforeInject
	// 注入config后执行
	AfterInjectConfig
	// 注入dao后执行
	AfterInject
}

type EmbeddedPresets

type EmbeddedPresets struct {
}

func (EmbeddedPresets) AfterInject added in v0.0.33

func (u EmbeddedPresets) AfterInject()

func (EmbeddedPresets) AfterInjectConfig added in v0.0.33

func (u EmbeddedPresets) AfterInjectConfig()

func (EmbeddedPresets) BeforeInject added in v0.0.33

func (u EmbeddedPresets) BeforeInject()

type Encoder

type Encoder interface {
	Encode(format string, v map[string]any) ([]byte, error)
}

type FlagTagSettings

type FlagTagSettings struct {
	Name    string `meta:"name"`
	Short   string `meta:"short"`
	Env     string `meta:"env" explain:"从环境变量读取"`
	Default string `meta:"default"`
	Usage   string `meta:"usage"`
}

type InitTagSettings

type InitTagSettings struct {
	ConfigName   string `meta:"config"`
	DefaultValue string `meta:"default"`
}

func ParseInitTagSettings

func ParseInitTagSettings(str string) *InitTagSettings

Jump to

Keyboard shortcuts

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