Skip to content

Commit

Permalink
refactor: refactor the key of the configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
OldSmokeGun committed Nov 24, 2023
1 parent a92a4fe commit 9a2792a
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 103 deletions.
11 changes: 5 additions & 6 deletions deploy/kubernetes/values.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ configMap:
timeout: 5

##################### server #####################


##################### gRPC service address #####################
services:
Expand All @@ -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
Expand Down Expand Up @@ -170,10 +170,9 @@ configMap:

##################### trace #####################

# otpl:
# trace:
# protocol: "http"
# endpoint: "localhost:4318"
# trace:
# protocol: "http"
# endpoint: "localhost:4318"

##################### trace #####################

Expand Down
9 changes: 4 additions & 5 deletions etc/config.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,9 +109,8 @@ kafka:

##################### trace #####################

# otpl:
# trace:
# protocol: "http"
# endpoint: "localhost:4318"
# trace:
# protocol: "http"
# endpoint: "localhost:4318"

##################### trace #####################
2 changes: 1 addition & 1 deletion internal/app/pkg/casbin/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/casbin/adapter/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/casbin/casbin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/casbin/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/db/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/ent/ent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/ent/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/gorm/dialector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions internal/app/pkg/gorm/gorm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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

Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/app/pkg/gorm/provide.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/app/tests/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 1 addition & 2 deletions internal/command/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/command/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/command/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func initKafka(

func initDB(
context.Context,
config.DBConn,
config.DatabaseConn,
*slog.Logger,
) (*sql.DB, func(), error) {
panic(wire.Build(
Expand Down
16 changes: 8 additions & 8 deletions internal/command/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ var ProviderSet = wire.NewSet(
GetGRPCServer,
GetServices,
GetDiscovery,
GetDB,
GetDBConn,
GetDatabase,
GetDatabaseConn,
GetRedis,
GetKafka,
GetOTPL,
GetTrace,
)

type Configure interface {
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading

0 comments on commit 9a2792a

Please sign in to comment.