From 9a2792ac5d5ee843f8f423f6681bb0b6655d886b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E6=9E=97=E7=91=9E?= <30717069+OldSmokeGun@users.noreply.github.com> Date: Fri, 24 Nov 2023 18:49:05 +0800 Subject: [PATCH] refactor: refactor the key of the configuration file --- deploy/kubernetes/values.yaml.example | 11 ++-- etc/config.yaml.example | 9 ++- internal/app/pkg/casbin/adapter/adapter.go | 2 +- internal/app/pkg/casbin/adapter/ent.go | 2 +- internal/app/pkg/casbin/casbin.go | 2 +- internal/app/pkg/casbin/provide.go | 2 +- internal/app/pkg/db/db.go | 2 +- internal/app/pkg/db/provide.go | 2 +- internal/app/pkg/ent/ent.go | 2 +- internal/app/pkg/ent/provide.go | 2 +- internal/app/pkg/gorm/dialector.go | 2 +- internal/app/pkg/gorm/gorm.go | 14 ++--- internal/app/pkg/gorm/provide.go | 2 +- internal/app/tests/db.go | 2 +- internal/command/base.go | 3 +- internal/command/migrate.go | 2 +- internal/command/wire.go | 2 +- internal/command/wire_gen.go | 16 +++--- internal/config/config.go | 26 ++++----- internal/config/db.go | 64 +++++++++++----------- internal/config/otel.go | 16 ------ internal/config/redis.go | 2 +- internal/config/trace.go | 11 ++++ 23 files changed, 95 insertions(+), 103 deletions(-) delete mode 100644 internal/config/otel.go create mode 100644 internal/config/trace.go diff --git a/deploy/kubernetes/values.yaml.example b/deploy/kubernetes/values.yaml.example index c22fdd37..8b4515cf 100644 --- a/deploy/kubernetes/values.yaml.example +++ b/deploy/kubernetes/values.yaml.example @@ -90,7 +90,7 @@ configMap: timeout: 5 ##################### server ##################### - + ##################### gRPC service address ##################### services: @@ -114,7 +114,7 @@ configMap: ##################### database ##################### - # db: + # database: # driver: "mysql" # dsn: "root:root@tcp(127.0.0.1:3306)/go-scaffold?charset=utf8mb4&parseTime=True&loc=Local" # maxIdleConn: 20 @@ -170,10 +170,9 @@ configMap: ##################### trace ##################### - # otpl: - # trace: - # protocol: "http" - # endpoint: "localhost:4318" + # trace: + # protocol: "http" + # endpoint: "localhost:4318" ##################### trace ##################### diff --git a/etc/config.yaml.example b/etc/config.yaml.example index ded7f5dd..9c5c8577 100644 --- a/etc/config.yaml.example +++ b/etc/config.yaml.example @@ -53,7 +53,7 @@ services: ##################### database ##################### -# db: +# database: # driver: "mysql" # dsn: "root:root@tcp(127.0.0.1:3306)/go-scaffold?charset=utf8mb4&parseTime=True&loc=Local" # maxIdleConn: 20 @@ -109,9 +109,8 @@ kafka: ##################### trace ##################### -# otpl: -# trace: -# protocol: "http" -# endpoint: "localhost:4318" +# trace: +# protocol: "http" +# endpoint: "localhost:4318" ##################### trace ##################### diff --git a/internal/app/pkg/casbin/adapter/adapter.go b/internal/app/pkg/casbin/adapter/adapter.go index b4b61d6c..d982ef89 100644 --- a/internal/app/pkg/casbin/adapter/adapter.go +++ b/internal/app/pkg/casbin/adapter/adapter.go @@ -22,7 +22,7 @@ type Adapter interface { func New( env config.Env, conf config.CasbinAdapter, - dbConf config.DBConn, + dbConf config.DatabaseConn, logger *slog.Logger, db *gorm.DB, sdb *sql.DB, diff --git a/internal/app/pkg/casbin/adapter/ent.go b/internal/app/pkg/casbin/adapter/ent.go index 6db0a084..05b02460 100644 --- a/internal/app/pkg/casbin/adapter/ent.go +++ b/internal/app/pkg/casbin/adapter/ent.go @@ -13,7 +13,7 @@ import ( ) // NewEntAdapter build casin ent adapter -func NewEntAdapter(env config.Env, dbConf config.DBConn, logger *slog.Logger, sdb *sql.DB) (*entadapter.Adapter, error) { +func NewEntAdapter(env config.Env, dbConf config.DatabaseConn, logger *slog.Logger, sdb *sql.DB) (*entadapter.Adapter, error) { driver := entsql.OpenDB(dbConf.Driver.String(), sdb) options := []ent.Option{ diff --git a/internal/app/pkg/casbin/casbin.go b/internal/app/pkg/casbin/casbin.go index f4bc6df7..413d8af5 100644 --- a/internal/app/pkg/casbin/casbin.go +++ b/internal/app/pkg/casbin/casbin.go @@ -16,7 +16,7 @@ import ( func New( env config.Env, conf config.Casbin, - dbConf config.DBConn, + dbConf config.DatabaseConn, logger *slog.Logger, gdb *gorm.DB, sdb *sql.DB, diff --git a/internal/app/pkg/casbin/provide.go b/internal/app/pkg/casbin/provide.go index 5bbde58e..735afc4a 100644 --- a/internal/app/pkg/casbin/provide.go +++ b/internal/app/pkg/casbin/provide.go @@ -14,7 +14,7 @@ import ( func Provide( env config.Env, conf config.Casbin, - dbConf config.DBConn, + dbConf config.DatabaseConn, logger *slog.Logger, gdb *gorm.DB, sdb *sql.DB, diff --git a/internal/app/pkg/db/db.go b/internal/app/pkg/db/db.go index 141fdeac..bf1b1a37 100644 --- a/internal/app/pkg/db/db.go +++ b/internal/app/pkg/db/db.go @@ -17,7 +17,7 @@ import ( var ErrUnsupportedDriver = errors.New("unsupported database driver") // New build database connection -func New(ctx context.Context, conf config.DBConn) (*sql.DB, error) { +func New(ctx context.Context, conf config.DatabaseConn) (*sql.DB, error) { if !conf.Driver.IsSupported() { return nil, ErrUnsupportedDriver } diff --git a/internal/app/pkg/db/provide.go b/internal/app/pkg/db/provide.go index 2407ba28..3af2d1d2 100644 --- a/internal/app/pkg/db/provide.go +++ b/internal/app/pkg/db/provide.go @@ -8,7 +8,7 @@ import ( ) // Provide database connection -func Provide(ctx context.Context, conf config.DBConn) (db *sql.DB, cleanup func(), err error) { +func Provide(ctx context.Context, conf config.DatabaseConn) (db *sql.DB, cleanup func(), err error) { db, err = New(ctx, conf) if err != nil { return nil, nil, err diff --git a/internal/app/pkg/ent/ent.go b/internal/app/pkg/ent/ent.go index 7c0543c4..a70f7fcb 100644 --- a/internal/app/pkg/ent/ent.go +++ b/internal/app/pkg/ent/ent.go @@ -15,7 +15,7 @@ import ( ) // New build db client -func New(env config.Env, dbConf config.DBConn, logger *slog.Logger, sdb *sql.DB) (*ent.Client, error) { +func New(env config.Env, dbConf config.DatabaseConn, logger *slog.Logger, sdb *sql.DB) (*ent.Client, error) { driver := entsql.OpenDB(dbConf.Driver.String(), sdb) options := []ent.Option{ diff --git a/internal/app/pkg/ent/provide.go b/internal/app/pkg/ent/provide.go index 0c56e037..ea9fa279 100644 --- a/internal/app/pkg/ent/provide.go +++ b/internal/app/pkg/ent/provide.go @@ -9,7 +9,7 @@ import ( ) // Provide db client -func Provide(env config.Env, conf config.DBConn, logger *slog.Logger, db *sql.DB) (*ent.Client, func(), error) { +func Provide(env config.Env, conf config.DatabaseConn, logger *slog.Logger, db *sql.DB) (*ent.Client, func(), error) { client, err := New(env, conf, logger, db) if err != nil { return nil, nil, err diff --git a/internal/app/pkg/gorm/dialector.go b/internal/app/pkg/gorm/dialector.go index a8794e55..384c9d3e 100644 --- a/internal/app/pkg/gorm/dialector.go +++ b/internal/app/pkg/gorm/dialector.go @@ -11,7 +11,7 @@ import ( ) // NewDialect build dialect -func NewDialect(driver config.DBDriver, conn gorm.ConnPool) (gorm.Dialector, error) { +func NewDialect(driver config.DatabaseDriver, conn gorm.ConnPool) (gorm.Dialector, error) { var dialect gorm.Dialector switch driver { case config.MySQL: diff --git a/internal/app/pkg/gorm/gorm.go b/internal/app/pkg/gorm/gorm.go index a7c593a3..c2359367 100644 --- a/internal/app/pkg/gorm/gorm.go +++ b/internal/app/pkg/gorm/gorm.go @@ -19,8 +19,8 @@ import ( var ErrUnsupportedResolverType = errors.New("unsupported resolver type") // New build gorm -func New(ctx context.Context, conf config.DB, logger *slog.Logger) (gdb *gorm.DB, err error) { - sdb, err := db.New(ctx, conf.DBConn) +func New(ctx context.Context, conf config.Database, logger *slog.Logger) (gdb *gorm.DB, err error) { + sdb, err := db.New(ctx, conf.DatabaseConn) if err != nil { return nil, err } @@ -50,11 +50,11 @@ func New(ctx context.Context, conf config.DB, logger *slog.Logger) (gdb *gorm.DB return gdb, nil } -func registerResolver(ctx context.Context, gdb *gorm.DB, conf config.DB) error { +func registerResolver(ctx context.Context, gdb *gorm.DB, conf config.Database) error { rcs := conf.Resolvers - conn := conf.DBConn + conn := conf.DatabaseConn - resolvers := make([]*config.DBResolver, 0, len(rcs)) + resolvers := make([]*config.DatabaseResolver, 0, len(rcs)) for _, rc := range rcs { rc.Driver = conn.Driver @@ -81,14 +81,14 @@ func registerResolver(ctx context.Context, gdb *gorm.DB, conf config.DB) error { return gdb.Use(plugin) } -func buildResolver(ctx context.Context, resolvers []*config.DBResolver) (gorm.Plugin, error) { +func buildResolver(ctx context.Context, resolvers []*config.DatabaseResolver) (gorm.Plugin, error) { var ( sources = make([]gorm.Dialector, 0, len(resolvers)) replicas = make([]gorm.Dialector, 0, len(resolvers)) ) for _, resolver := range resolvers { - sdb, err := db.New(ctx, resolver.DBConn) + sdb, err := db.New(ctx, resolver.DatabaseConn) if err != nil { return nil, err } diff --git a/internal/app/pkg/gorm/provide.go b/internal/app/pkg/gorm/provide.go index 37152ec7..df5283fd 100644 --- a/internal/app/pkg/gorm/provide.go +++ b/internal/app/pkg/gorm/provide.go @@ -10,7 +10,7 @@ import ( ) // Provide gorm -func Provide(ctx context.Context, conf config.DB, logger *slog.Logger) (db *gorm.DB, cleanup func(), err error) { +func Provide(ctx context.Context, conf config.Database, logger *slog.Logger) (db *gorm.DB, cleanup func(), err error) { db, err = New(ctx, conf, logger) if err != nil { return nil, nil, err diff --git a/internal/app/tests/db.go b/internal/app/tests/db.go index 43c88632..10a1406b 100644 --- a/internal/app/tests/db.go +++ b/internal/app/tests/db.go @@ -33,7 +33,7 @@ func NewDB(logger *slog.Logger) (*DB, func(), error) { LogInfo: true, }) - dialect, err := igorm.NewDialect(config.DBDriver(dbDriver), mdb) + dialect, err := igorm.NewDialect(config.DatabaseDriver(dbDriver), mdb) if err != nil { return nil, nil, err } diff --git a/internal/command/base.go b/internal/command/base.go index 80899d1e..20641bc4 100644 --- a/internal/command/base.go +++ b/internal/command/base.go @@ -139,14 +139,13 @@ func (c *baseCmd) initConfig(cmd *cobra.Command, enableRemote bool) { func (c *baseCmd) initTrace(cmd *cobra.Command) { c.mustConfig() - otplConfig, err := config.GetOTPL() + traceConfig, err := config.GetTrace() if config.IsNotConfigured(err) { return } else if err != nil { panic(err) } - traceConfig := otplConfig.Trace appName := getAppName(cmd) appEnv := getAppEnvironment(cmd) diff --git a/internal/command/migrate.go b/internal/command/migrate.go index 89db70f4..274cbcd9 100644 --- a/internal/command/migrate.go +++ b/internal/command/migrate.go @@ -58,7 +58,7 @@ func newMigrateCmd() *migrateCmd { func (c *migrateCmd) initMigrate(cmd *cobra.Command) { c.mustConfig() - dbConfig, err := config.GetDBConn() + dbConfig, err := config.GetDatabaseConn() if err != nil { panic(err) } diff --git a/internal/command/wire.go b/internal/command/wire.go index 279d85b9..7b007e72 100644 --- a/internal/command/wire.go +++ b/internal/command/wire.go @@ -58,7 +58,7 @@ func initKafka( func initDB( context.Context, - config.DBConn, + config.DatabaseConn, *slog.Logger, ) (*sql.DB, func(), error) { panic(wire.Build( diff --git a/internal/command/wire_gen.go b/internal/command/wire_gen.go index be7f7065..b0c1be8e 100644 --- a/internal/command/wire_gen.go +++ b/internal/command/wire_gen.go @@ -42,15 +42,15 @@ func initServer(contextContext context.Context, appName config.AppName, env conf if err != nil { return nil, nil, err } - dbConn, err := config.GetDBConn() + databaseConn, err := config.GetDatabaseConn() if err != nil { return nil, nil, err } - sqlDB, cleanup, err := db.Provide(contextContext, dbConn) + sqlDB, cleanup, err := db.Provide(contextContext, databaseConn) if err != nil { return nil, nil, err } - entClient, cleanup2, err := ent.Provide(env, dbConn, logger, sqlDB) + entClient, cleanup2, err := ent.Provide(env, databaseConn, logger, sqlDB) if err != nil { cleanup() return nil, nil, err @@ -61,19 +61,19 @@ func initServer(contextContext context.Context, appName config.AppName, env conf cleanup() return nil, nil, err } - configDB, err := config.GetDB() + database, err := config.GetDatabase() if err != nil { cleanup2() cleanup() return nil, nil, err } - gormDB, cleanup3, err := gorm.Provide(contextContext, configDB, logger) + gormDB, cleanup3, err := gorm.Provide(contextContext, database, logger) if err != nil { cleanup2() cleanup() return nil, nil, err } - enforcer, err := casbin.Provide(env, configCasbin, dbConn, logger, gormDB, sqlDB) + enforcer, err := casbin.Provide(env, configCasbin, databaseConn, logger, gormDB, sqlDB) if err != nil { cleanup3() cleanup2() @@ -177,8 +177,8 @@ func initKafka(contextContext context.Context, appName config.AppName, env confi }, nil } -func initDB(contextContext context.Context, dbConn config.DBConn, logger *slog.Logger) (*sql.DB, func(), error) { - sqlDB, cleanup, err := db.Provide(contextContext, dbConn) +func initDB(contextContext context.Context, databaseConn config.DatabaseConn, logger *slog.Logger) (*sql.DB, func(), error) { + sqlDB, cleanup, err := db.Provide(contextContext, databaseConn) if err != nil { return nil, nil, err } diff --git a/internal/config/config.go b/internal/config/config.go index c863d5c8..506dc3db 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -16,11 +16,11 @@ var ProviderSet = wire.NewSet( GetGRPCServer, GetServices, GetDiscovery, - GetDB, - GetDBConn, + GetDatabase, + GetDatabaseConn, GetRedis, GetKafka, - GetOTPL, + GetTrace, ) type Configure interface { @@ -36,10 +36,10 @@ type Config struct { GRPC *GRPC `json:"grpc"` Services *Services `json:"services"` Discovery *Discovery `json:"discovery"` - DB *DB `json:"db"` + Database *Database `json:"database"` Redis *Redis `json:"redis"` Kafka *Kafka `json:"kafka"` - OTPL *OTLP `json:"otpl"` + Trace *Trace `json:"trace"` } // SetConfig set configuration @@ -93,16 +93,16 @@ func GetDiscovery() (Discovery, error) { return getEntry(config.Discovery) } -func GetDB() (DB, error) { - return getEntry(config.DB) +func GetDatabase() (Database, error) { + return getEntry(config.Database) } -func GetDBConn() (DBConn, error) { - dbConfig, err := GetDB() +func GetDatabaseConn() (DatabaseConn, error) { + dbConfig, err := GetDatabase() if err != nil { - return DBConn{}, err + return DatabaseConn{}, err } - return dbConfig.DBConn, nil + return dbConfig.DatabaseConn, nil } func GetRedis() (Redis, error) { @@ -113,8 +113,8 @@ func GetKafka() (Kafka, error) { return getEntry(config.Kafka) } -func GetOTPL() (OTLP, error) { - return getEntry(config.OTPL) +func GetTrace() (Trace, error) { + return getEntry(config.Trace) } func getHTTP() (HTTP, error) { diff --git a/internal/config/db.go b/internal/config/db.go index 8aec0226..3e761a77 100644 --- a/internal/config/db.go +++ b/internal/config/db.go @@ -7,31 +7,31 @@ import ( "github.com/samber/lo" ) -var supportedDrivers = []DBDriver{MySQL, Postgres, SQLite} +var supportedDrivers = []DatabaseDriver{MySQL, Postgres, SQLite} -// DB database config -type DB struct { - DBConn - LogInfo bool `json:"logInfo"` - Resolvers []*DBResolver `json:"resolvers"` +// Database config +type Database struct { + DatabaseConn + LogInfo bool `json:"logInfo"` + Resolvers []*DatabaseResolver `json:"resolvers"` } -func (DB) GetName() string { - return "db" +func (Database) GetName() string { + return "database" } -// DBConn database connection config -type DBConn struct { - Driver DBDriver `json:"driver"` - DSN string `json:"dsn"` - MaxIdleConn int `json:"maxIdleConn"` - MaxOpenConn int `json:"maxOpenConn"` - ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"` - ConnMaxLifeTime time.Duration `json:"connMaxLifeTime"` +// DatabaseConn connection config +type DatabaseConn struct { + Driver DatabaseDriver `json:"driver"` + DSN string `json:"dsn"` + MaxIdleConn int `json:"maxIdleConn"` + MaxOpenConn int `json:"maxOpenConn"` + ConnMaxIdleTime time.Duration `json:"connMaxIdleTime"` + ConnMaxLifeTime time.Duration `json:"connMaxLifeTime"` } // EnableMultiStatement enable the execution of multi sql statement -func (d *DBConn) EnableMultiStatement() error { +func (d *DatabaseConn) EnableMultiStatement() error { if d.Driver != MySQL { return nil } @@ -51,34 +51,34 @@ func (d *DBConn) EnableMultiStatement() error { return nil } -// DBDriver database driver type -type DBDriver string +// DatabaseDriver database driver type +type DatabaseDriver string -func (d DBDriver) String() string { +func (d DatabaseDriver) String() string { return string(d) } // IsSupported check that the driver is supported -func (d DBDriver) IsSupported() bool { +func (d DatabaseDriver) IsSupported() bool { return lo.Contains(supportedDrivers, d) } const ( - MySQL DBDriver = "mysql" - Postgres DBDriver = "postgres" - SQLite DBDriver = "sqlite3" + MySQL DatabaseDriver = "mysql" + Postgres DatabaseDriver = "postgres" + SQLite DatabaseDriver = "sqlite3" ) -// DBResolver database resolver config -type DBResolver struct { - Type DBResolverType `json:"type"` - DBConn +// DatabaseResolver database resolver config +type DatabaseResolver struct { + Type DatabaseResolverType `json:"type"` + DatabaseConn } -// DBResolverType database resolver type -type DBResolverType string +// DatabaseResolverType database resolver type +type DatabaseResolverType string const ( - Source DBResolverType = "source" - Replica DBResolverType = "replica" + Source DatabaseResolverType = "source" + Replica DatabaseResolverType = "replica" ) diff --git a/internal/config/otel.go b/internal/config/otel.go deleted file mode 100644 index 4a157335..00000000 --- a/internal/config/otel.go +++ /dev/null @@ -1,16 +0,0 @@ -package config - -// OTLP the otpl config -type OTLP struct { - Trace Trace `json:"trace"` -} - -func (OTLP) GetName() string { - return "otpl" -} - -// Trace the otpl trace config -type Trace struct { - Protocol string `json:"protocol"` - Endpoint string `json:"endpoint"` -} diff --git a/internal/config/redis.go b/internal/config/redis.go index 66476e0f..677b87de 100644 --- a/internal/config/redis.go +++ b/internal/config/redis.go @@ -7,7 +7,7 @@ type Redis struct { Addr string `json:"addr"` Username string `json:"username"` Password string `json:"password"` - DB int `json:"DB"` + DB int `json:"db"` MaxRetries int `json:"maxRetries"` MinRetryBackoff time.Duration `json:"minRetryBackoff"` MaxRetryBackoff time.Duration `json:"maxRetryBackoff"` diff --git a/internal/config/trace.go b/internal/config/trace.go new file mode 100644 index 00000000..f87398e5 --- /dev/null +++ b/internal/config/trace.go @@ -0,0 +1,11 @@ +package config + +// Trace the trace config +type Trace struct { + Protocol string `json:"protocol"` + Endpoint string `json:"endpoint"` +} + +func (Trace) GetName() string { + return "trace" +}