Skip to content

Commit

Permalink
Opt: mysql ssl mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zijiren233 committed Nov 17, 2023
1 parent cc23248 commit 1bb24e7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
6 changes: 4 additions & 2 deletions internal/bootstrap/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ func InitDatabase(ctx context.Context) error {
if conf.Conf.Database.CustomDSN != "" {
dsn = conf.Conf.Database.CustomDSN
} else if conf.Conf.Database.Port == 0 {
dsn = fmt.Sprintf("%s:%s@unix(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true",
dsn = fmt.Sprintf("%s:%s@unix(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true&tls=%s",
conf.Conf.Database.User,
conf.Conf.Database.Password,
conf.Conf.Database.Host,
conf.Conf.Database.DBName,
conf.Conf.Database.SslMode,
)
log.Infof("mysql database unix socket: %s", conf.Conf.Database.Host)
} else {
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true",
dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local&interpolateParams=true&tls=%s",
conf.Conf.Database.User,
conf.Conf.Database.Password,
conf.Conf.Database.Host,
conf.Conf.Database.Port,
conf.Conf.Database.DBName,
conf.Conf.Database.SslMode,
)
log.Infof("mysql database tcp: %s:%d", conf.Conf.Database.Host, conf.Conf.Database.Port)
}
Expand Down
9 changes: 4 additions & 5 deletions internal/conf/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type DatabaseConfig struct {
User string `yaml:"user" env:"DATABASE_USER"`
Password string `yaml:"password" env:"DATABASE_PASSWORD"`
DBName string `yaml:"db_name" lc:"default: synctv" hc:"when type is sqlite3, it will use sqlite db file or memory" env:"DATABASE_DB_NAME"`
SslMode string `yaml:"ssl_mode" env:"DATABASE_SSL_MODE"`
SslMode string `yaml:"ssl_mode" env:"DATABASE_SSL_MODE" hc:"mysql: true, false, skip-verify, preferred, <name> postgres: disable, require, verify-ca, verify-full"`

CustomDSN string `yaml:"custom_dsn" hc:"when not empty, it will ignore other config" env:"DATABASE_CUSTOM_DSN"`

Expand All @@ -27,10 +27,9 @@ type DatabaseConfig struct {

func DefaultDatabaseConfig() DatabaseConfig {
return DatabaseConfig{
Type: DatabaseTypeSqlite3,
Host: "",
DBName: "synctv",
SslMode: "disable",
Type: DatabaseTypeSqlite3,
Host: "",
DBName: "synctv",

MaxIdleConns: 4,
MaxOpenConns: 64,
Expand Down

0 comments on commit 1bb24e7

Please sign in to comment.