From 3d1f127cd7402d820398cb1ee09c7eccd45027bc Mon Sep 17 00:00:00 2001 From: songyaofeng Date: Thu, 29 Aug 2024 23:13:52 +0800 Subject: [PATCH] feat(db): add gorm db connection --- Makefile | 9 + README.md | 8 +- dbutil/db.go | 71 ++++++++ dbutil/db_test.go | 41 +++++ dbutil/mysqlconfig.go | 63 +++++++ generalutil/general_test.go | 7 +- go.mod | 8 +- go.sum | 18 +- protobuf/conf/conf.go | 5 + protobuf/conf/conf.pb.go | 337 ++++++++++++++++++++++++++++++++++++ protobuf/conf/conf.proto | 24 +++ 11 files changed, 577 insertions(+), 14 deletions(-) create mode 100644 Makefile create mode 100644 dbutil/db.go create mode 100644 dbutil/db_test.go create mode 100644 dbutil/mysqlconfig.go create mode 100644 protobuf/conf/conf.go create mode 100644 protobuf/conf/conf.pb.go create mode 100644 protobuf/conf/conf.proto diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4e64676 --- /dev/null +++ b/Makefile @@ -0,0 +1,9 @@ +CONF_PROTO_FILES=$(shell find ./protobuf/conf/ -name *.proto) + +.PHONY: conf +# generate conf proto +conf: + protoc --proto_path=. \ + --go_out=paths=source_relative:. \ + $(CONF_PROTO_FILES) + diff --git a/README.md b/README.md index 63efd26..e3e8185 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Claymore :tada::tada::tada: :tada::tada::tada: -> 当前包使用的go版本 `go 1.22.6` +> 本包开发使用的go版本 `go 1.22.6` go开发中经常使用一些工具函数,每次新项目或者到了一个新坑位都要重新去写,很是麻烦 所以,这个项目就是封装一些常用的工具函数,方便 Gopher 开发,希望能成为 Gopher 开发中经常使用的 **claymore**。 @@ -49,6 +49,12 @@ go开发中经常使用一些工具函数,每次新项目或者到了一个新 | 001 | JsonEncode() | json 序列化 | | 002 | JsonDecode() | json 反序列化 | +### Gorm(dbutil) ### + +| 编号 | 函数 | 功能 | +|-----|--------------|----------------| +| 001 | New() | 连接数据库,获取Gorm实例 | + ### 其他(generalutil) ### | 编号 | 函数 | 功能 | diff --git a/dbutil/db.go b/dbutil/db.go new file mode 100644 index 0000000..0448438 --- /dev/null +++ b/dbutil/db.go @@ -0,0 +1,71 @@ +package dbutil + +import ( + "log" + "os" + "time" + + "gorm.io/driver/mysql" + "gorm.io/gorm" + "gorm.io/gorm/logger" + + "github.com/lastares/claymore/protobuf/conf" +) + +func New(dbConfig *conf.Data_Database, gormConfig gorm.Config) (*gorm.DB, error) { + mysqlConfig := InitConfig( + WithDriver(dbConfig.Driver), + DSN(dbConfig.Source), + DefaultStringSize(256), // 为字符串(string)字段设置大小。默认情况下,对于没有大小、没有主键、没有定义索引且没有默认值的字段,将使用db类型“longext” + + DisableDatetimePrecision(true), // 禁用日期时间精度支持。但是这在MySQL 5.6之前不支持 + DontSupportRenameColumn(true), // 重命名列时使用change,但是在MySQL 8、MariaDB之前不支持重命名 + DontSupportRenameIndex(true), // 重命名索引时删除并创建索引。但是在MySQL 5.7、MariaDB之前不支持重命名索引 + SkipInitializeWithVersion(true), // 是否根据当前 MySQL 版本自动配置 + ) + db, err := gorm.Open(mysql.New(mysqlConfig), &gormConfig) + if err != nil { + return nil, err + } + sqlDB, err := db.DB() + if err != nil { + return db, err + } + // 连接池配置 + sqlDB.SetConnMaxLifetime(dbConfig.ConnectionLifeTime.AsDuration()) // 每一个连接的生命周期 + sqlDB.SetMaxIdleConns(int(dbConfig.MaxIdleConnections)) // 是设置空闲时的最大连接数 + sqlDB.SetMaxOpenConns(int(dbConfig.MaxOpenConnections)) // 设置与数据库的最大打开连接数 + return db, err +} + +// GormConfig 配置GORM的日志和事务设置。 +// 参数: +// +// app - 提供应用配置,用于判断当前环境以设置日志级别。 +// +// 返回值: +// +// 返回一个gorm.Config对象,用于配置GORM的行为。 +func GormConfig(app *conf.App) gorm.Config { + // 根据应用环境设置日志模式,默认为警告级别 + logMode := logger.Warn + // 如果是开发环境,切换到信息级别日志,记录所有SQL执行 + if app.IsDevelopment() { + logMode = logger.Info + } + // 创建一个新的logger实例,配置GORM日志行为 + newLogger := logger.New( + log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer,将日志输出到标准输出 + logger.Config{ + SlowThreshold: 1 * time.Second, // 慢查询阈值设置为1秒 + LogLevel: logMode, // 设置日志级别 + IgnoreRecordNotFoundError: true, // 忽略记录未找到错误 + }, + ) + // 返回配置的GORM配置对象 + return gorm.Config{ + Logger: newLogger, // 使用新创建的logger实例 + SkipDefaultTransaction: true, // 跳过默认事务,通常用于提高性能 + PrepareStmt: true, // 启用预处理语句,可以提高性能和安全性 + } +} diff --git a/dbutil/db_test.go b/dbutil/db_test.go new file mode 100644 index 0000000..0cca1ee --- /dev/null +++ b/dbutil/db_test.go @@ -0,0 +1,41 @@ +package dbutil + +import ( + "testing" + + "google.golang.org/protobuf/types/known/durationpb" + + "github.com/lastares/claymore/generalutil" + "github.com/lastares/claymore/protobuf/conf" +) + +func TestNewDB(t *testing.T) { + dsn := "root:root@tcp(127.0.0.1:3306)/my_test?charset=utf8mb4&parseTime=True&loc=Local" + databaseConf := &conf.Data_Database{ + Driver: "mysql", + Source: dsn, + MaxOpenConnections: 100, + MaxIdleConnections: 100, + ConnectionLifeTime: &durationpb.Duration{ + Seconds: 3600, + }, + } + app := &conf.App{ + Name: "test", + Env: "dev", + } + gormConfig := GormConfig(app) + gorm, err := New(databaseConf, gormConfig) + if err != nil { + t.Errorf("NewDB error: %v", err) + } + type User struct { + ID int32 + Name string + } + var u *User + if err = gorm.Table("user").Select("*").First(&u).Error; err != nil { + t.Errorf("NewDB error: %v", err) + } + generalutil.PrettyPrintStruct(u) +} diff --git a/dbutil/mysqlconfig.go b/dbutil/mysqlconfig.go new file mode 100644 index 0000000..7ebdd27 --- /dev/null +++ b/dbutil/mysqlconfig.go @@ -0,0 +1,63 @@ +package dbutil + +import ( + "gorm.io/driver/mysql" +) + +type Config func(*mysql.Config) + +// DSN data source name +func DSN(dsn string) Config { + return func(c *mysql.Config) { + c.DSN = dsn + } +} + +func WithDriver(driver string) Config { + return func(c *mysql.Config) { + c.DriverName = driver + } +} + +// DefaultStringSize string 类型字段的默认长度 +func DefaultStringSize(size uint) Config { + return func(c *mysql.Config) { + c.DefaultStringSize = size + } +} + +// DisableDatetimePrecision 禁用 datetime 精度,MySQL 5.6 之前的数据库不支持 +func DisableDatetimePrecision(disabled bool) Config { + return func(c *mysql.Config) { + c.DisableDatetimePrecision = disabled + } +} + +// DontSupportRenameIndex 重命名索引时采用删除并新建的方式,MySQL 5.7 之前的数据库和 MariaDB 不支持重命名索引 +func DontSupportRenameIndex(dontSupport bool) Config { + return func(c *mysql.Config) { + c.DontSupportRenameIndex = dontSupport + } +} + +// DontSupportRenameColumn 用 `change` 重命名列,MySQL 8 之前的数据库和 MariaDB 不支持重命名列 +func DontSupportRenameColumn(dontSupport bool) Config { + return func(c *mysql.Config) { + c.DontSupportRenameColumn = dontSupport + } +} + +// SkipInitializeWithVersion 根据当前 MySQL 版本自动配置 +func SkipInitializeWithVersion(skipped bool) Config { + return func(c *mysql.Config) { + c.SkipInitializeWithVersion = skipped + } +} + +func InitConfig(cfgs ...Config) mysql.Config { + config := mysql.Config{} + for _, cfg := range cfgs { + cfg(&config) + } + return config +} diff --git a/generalutil/general_test.go b/generalutil/general_test.go index feb6177..6b2ca1e 100644 --- a/generalutil/general_test.go +++ b/generalutil/general_test.go @@ -6,9 +6,10 @@ import ( func TestBeautifyPrintStruct(t *testing.T) { type User struct { - Name string - Age int + Name string + Age int + Address []int } - u := User{Name: "John", Age: 30} + u := User{Name: "John", Age: 30, Address: []int{1, 2, 3, 4}} PrettyPrintStruct(u) } diff --git a/go.mod b/go.mod index 5e7a62a..a7cadd5 100644 --- a/go.mod +++ b/go.mod @@ -5,10 +5,12 @@ go 1.22.6 require ( golang.org/x/text v0.17.0 google.golang.org/protobuf v1.34.2 + gorm.io/driver/mysql v1.5.7 + gorm.io/gorm v1.25.11 ) require ( - github.com/tidwall/gjson v1.17.3 // indirect - github.com/tidwall/match v1.1.1 // indirect - github.com/tidwall/pretty v1.2.1 // indirect + github.com/go-sql-driver/mysql v1.7.0 // indirect + github.com/jinzhu/inflection v1.0.0 // indirect + github.com/jinzhu/now v1.1.5 // indirect ) diff --git a/go.sum b/go.sum index 730b1eb..8f23b46 100644 --- a/go.sum +++ b/go.sum @@ -1,15 +1,19 @@ +github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc= +github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/tidwall/gjson v1.17.3 h1:bwWLZU7icoKRG+C+0PNwIKC6FCJO/Q3p2pZvuP0jN94= -github.com/tidwall/gjson v1.17.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= -github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= -github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= -github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= -github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= -github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= +github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= +github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= +github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg= google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw= +gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= +gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= +gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= +gorm.io/gorm v1.25.11 h1:/Wfyg1B/je1hnDx3sMkX+gAlxrlZpn6X0BXRlwXlvHg= +gorm.io/gorm v1.25.11/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ= diff --git a/protobuf/conf/conf.go b/protobuf/conf/conf.go new file mode 100644 index 0000000..2a9d76a --- /dev/null +++ b/protobuf/conf/conf.go @@ -0,0 +1,5 @@ +package conf + +func (a *App) IsDevelopment() bool { + return a.Env == "dev" +} diff --git a/protobuf/conf/conf.pb.go b/protobuf/conf/conf.pb.go new file mode 100644 index 0000000..a51e43e --- /dev/null +++ b/protobuf/conf/conf.pb.go @@ -0,0 +1,337 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.33.0 +// protoc v5.26.1 +// source: protobuf/conf/conf.proto + +package conf + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + durationpb "google.golang.org/protobuf/types/known/durationpb" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type App struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Version string `protobuf:"bytes,2,opt,name=version,proto3" json:"version,omitempty"` + Env string `protobuf:"bytes,3,opt,name=env,proto3" json:"env,omitempty"` +} + +func (x *App) Reset() { + *x = App{} + if protoimpl.UnsafeEnabled { + mi := &file_protobuf_conf_conf_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *App) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*App) ProtoMessage() {} + +func (x *App) ProtoReflect() protoreflect.Message { + mi := &file_protobuf_conf_conf_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use App.ProtoReflect.Descriptor instead. +func (*App) Descriptor() ([]byte, []int) { + return file_protobuf_conf_conf_proto_rawDescGZIP(), []int{0} +} + +func (x *App) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *App) GetVersion() string { + if x != nil { + return x.Version + } + return "" +} + +func (x *App) GetEnv() string { + if x != nil { + return x.Env + } + return "" +} + +type Data struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Database *Data_Database `protobuf:"bytes,1,opt,name=database,proto3" json:"database,omitempty"` +} + +func (x *Data) Reset() { + *x = Data{} + if protoimpl.UnsafeEnabled { + mi := &file_protobuf_conf_conf_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Data) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Data) ProtoMessage() {} + +func (x *Data) ProtoReflect() protoreflect.Message { + mi := &file_protobuf_conf_conf_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Data.ProtoReflect.Descriptor instead. +func (*Data) Descriptor() ([]byte, []int) { + return file_protobuf_conf_conf_proto_rawDescGZIP(), []int{1} +} + +func (x *Data) GetDatabase() *Data_Database { + if x != nil { + return x.Database + } + return nil +} + +type Data_Database struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Driver string `protobuf:"bytes,1,opt,name=driver,proto3" json:"driver,omitempty"` + Source string `protobuf:"bytes,2,opt,name=source,proto3" json:"source,omitempty"` + MaxOpenConnections int32 `protobuf:"varint,3,opt,name=max_open_connections,json=maxOpenConnections,proto3" json:"max_open_connections,omitempty"` + MaxIdleConnections int32 `protobuf:"varint,4,opt,name=max_idle_connections,json=maxIdleConnections,proto3" json:"max_idle_connections,omitempty"` + ConnectionLifeTime *durationpb.Duration `protobuf:"bytes,5,opt,name=connection_life_time,json=connectionLifeTime,proto3" json:"connection_life_time,omitempty"` +} + +func (x *Data_Database) Reset() { + *x = Data_Database{} + if protoimpl.UnsafeEnabled { + mi := &file_protobuf_conf_conf_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Data_Database) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Data_Database) ProtoMessage() {} + +func (x *Data_Database) ProtoReflect() protoreflect.Message { + mi := &file_protobuf_conf_conf_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Data_Database.ProtoReflect.Descriptor instead. +func (*Data_Database) Descriptor() ([]byte, []int) { + return file_protobuf_conf_conf_proto_rawDescGZIP(), []int{1, 0} +} + +func (x *Data_Database) GetDriver() string { + if x != nil { + return x.Driver + } + return "" +} + +func (x *Data_Database) GetSource() string { + if x != nil { + return x.Source + } + return "" +} + +func (x *Data_Database) GetMaxOpenConnections() int32 { + if x != nil { + return x.MaxOpenConnections + } + return 0 +} + +func (x *Data_Database) GetMaxIdleConnections() int32 { + if x != nil { + return x.MaxIdleConnections + } + return 0 +} + +func (x *Data_Database) GetConnectionLifeTime() *durationpb.Duration { + if x != nil { + return x.ConnectionLifeTime + } + return nil +} + +var File_protobuf_conf_conf_proto protoreflect.FileDescriptor + +var file_protobuf_conf_conf_proto_rawDesc = []byte{ + 0x0a, 0x18, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x63, 0x6f, 0x6e, 0x66, 0x2f, + 0x63, 0x6f, 0x6e, 0x66, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x63, 0x6f, 0x6e, 0x66, + 0x1a, 0x1e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, + 0x66, 0x2f, 0x64, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x22, 0x45, 0x0a, 0x03, 0x41, 0x70, 0x70, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x76, + 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x76, 0x65, + 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x76, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x09, 0x52, 0x03, 0x65, 0x6e, 0x76, 0x22, 0xa5, 0x02, 0x0a, 0x04, 0x44, 0x61, 0x74, 0x61, + 0x12, 0x2f, 0x0a, 0x08, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x63, 0x6f, 0x6e, 0x66, 0x2e, 0x44, 0x61, 0x74, 0x61, 0x2e, 0x44, + 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x52, 0x08, 0x64, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, + 0x65, 0x1a, 0xeb, 0x01, 0x0a, 0x08, 0x44, 0x61, 0x74, 0x61, 0x62, 0x61, 0x73, 0x65, 0x12, 0x16, + 0x0a, 0x06, 0x64, 0x72, 0x69, 0x76, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, + 0x64, 0x72, 0x69, 0x76, 0x65, 0x72, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, + 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x12, 0x30, + 0x0a, 0x14, 0x6d, 0x61, 0x78, 0x5f, 0x6f, 0x70, 0x65, 0x6e, 0x5f, 0x63, 0x6f, 0x6e, 0x6e, 0x65, + 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05, 0x52, 0x12, 0x6d, 0x61, + 0x78, 0x4f, 0x70, 0x65, 0x6e, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, + 0x12, 0x30, 0x0a, 0x14, 0x6d, 0x61, 0x78, 0x5f, 0x69, 0x64, 0x6c, 0x65, 0x5f, 0x63, 0x6f, 0x6e, + 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x04, 0x20, 0x01, 0x28, 0x05, 0x52, 0x12, + 0x6d, 0x61, 0x78, 0x49, 0x64, 0x6c, 0x65, 0x43, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, + 0x6e, 0x73, 0x12, 0x4b, 0x0a, 0x14, 0x63, 0x6f, 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, + 0x5f, 0x6c, 0x69, 0x66, 0x65, 0x5f, 0x74, 0x69, 0x6d, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x0b, + 0x32, 0x19, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, + 0x75, 0x66, 0x2e, 0x44, 0x75, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x12, 0x63, 0x6f, 0x6e, + 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x4c, 0x69, 0x66, 0x65, 0x54, 0x69, 0x6d, 0x65, 0x42, + 0x08, 0x5a, 0x06, 0x2e, 0x3b, 0x63, 0x6f, 0x6e, 0x66, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, +} + +var ( + file_protobuf_conf_conf_proto_rawDescOnce sync.Once + file_protobuf_conf_conf_proto_rawDescData = file_protobuf_conf_conf_proto_rawDesc +) + +func file_protobuf_conf_conf_proto_rawDescGZIP() []byte { + file_protobuf_conf_conf_proto_rawDescOnce.Do(func() { + file_protobuf_conf_conf_proto_rawDescData = protoimpl.X.CompressGZIP(file_protobuf_conf_conf_proto_rawDescData) + }) + return file_protobuf_conf_conf_proto_rawDescData +} + +var file_protobuf_conf_conf_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_protobuf_conf_conf_proto_goTypes = []interface{}{ + (*App)(nil), // 0: conf.App + (*Data)(nil), // 1: conf.Data + (*Data_Database)(nil), // 2: conf.Data.Database + (*durationpb.Duration)(nil), // 3: google.protobuf.Duration +} +var file_protobuf_conf_conf_proto_depIdxs = []int32{ + 2, // 0: conf.Data.database:type_name -> conf.Data.Database + 3, // 1: conf.Data.Database.connection_life_time:type_name -> google.protobuf.Duration + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name +} + +func init() { file_protobuf_conf_conf_proto_init() } +func file_protobuf_conf_conf_proto_init() { + if File_protobuf_conf_conf_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_protobuf_conf_conf_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*App); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_protobuf_conf_conf_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Data); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_protobuf_conf_conf_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Data_Database); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_protobuf_conf_conf_proto_rawDesc, + NumEnums: 0, + NumMessages: 3, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_protobuf_conf_conf_proto_goTypes, + DependencyIndexes: file_protobuf_conf_conf_proto_depIdxs, + MessageInfos: file_protobuf_conf_conf_proto_msgTypes, + }.Build() + File_protobuf_conf_conf_proto = out.File + file_protobuf_conf_conf_proto_rawDesc = nil + file_protobuf_conf_conf_proto_goTypes = nil + file_protobuf_conf_conf_proto_depIdxs = nil +} diff --git a/protobuf/conf/conf.proto b/protobuf/conf/conf.proto new file mode 100644 index 0000000..f0b19cc --- /dev/null +++ b/protobuf/conf/conf.proto @@ -0,0 +1,24 @@ +syntax = "proto3"; + +package conf; + +option go_package = ".;conf"; + +import "google/protobuf/duration.proto"; + +message App { + string name = 1; + string version = 2; + string env = 3; +} + +message Data { + message Database { + string driver = 1; + string source = 2; + int32 max_open_connections = 3; + int32 max_idle_connections = 4; + google.protobuf.Duration connection_life_time = 5; + } + Database database = 1; +}