修改数据库连接失败后的处理逻辑为报错,然后退出程序

This commit is contained in:
noreply
2024-10-14 21:20:46 +08:00
parent 392c922130
commit 67206df6a7
7 changed files with 51 additions and 34 deletions
+2 -2
View File
@@ -1,4 +1,4 @@
FROM golang:alpine as builder FROM golang:alpine AS builder
RUN apk add --no-cache --update git build-base RUN apk add --no-cache --update git build-base
ENV CGO_ENABLED=1 ENV CGO_ENABLED=1
@@ -9,7 +9,7 @@ RUN go mod tidy \
&& go build -ldflags "-s -w" \ && go build -ldflags "-s -w" \
-o epusdt . -o epusdt .
FROM alpine:latest as runner FROM alpine:latest AS runner
ENV TZ=Asia/Shanghai ENV TZ=Asia/Shanghai
RUN apk --no-cache add ca-certificates tzdata RUN apk --no-cache add ca-certificates tzdata
+11 -3
View File
@@ -1,7 +1,15 @@
package dao package dao
func Init() { import (
DBInit() "log"
)
RedisInit() func Init() {
if err := DBInit(); err != nil {
log.Fatalf("DBInit err: %v", err)
}
if err := RedisInit(); err != nil {
log.Fatalf("RedisInit err: %v", err)
}
} }
+12 -5
View File
@@ -4,20 +4,27 @@ import (
"strings" "strings"
"github.com/spf13/viper" "github.com/spf13/viper"
"gorm.io/gorm" "gorm.io/gorm"
) )
var Mdb *gorm.DB var Mdb *gorm.DB
func DBInit() { func DBInit() error {
dbType := viper.GetString("db_type") dbType := viper.GetString("db_type")
if strings.EqualFold(dbType, "postgres") { if strings.EqualFold(dbType, "postgres") {
PostgreSQLInit() if err := PostgreSQLInit(); err != nil {
return err
}
} else if strings.EqualFold(dbType, "sqlite") { } else if strings.EqualFold(dbType, "sqlite") {
SqliteInit() if err := SqliteInit(); err != nil {
return err
}
} else { } else {
MysqlInit() if err := MysqlInit(); err != nil {
return err
} }
}
MdbTableInit() MdbTableInit()
return nil
} }
+5 -6
View File
@@ -15,7 +15,7 @@ import (
) )
// MysqlInit 数据库初始化 // MysqlInit 数据库初始化
func MysqlInit() { func MysqlInit() error {
var err error var err error
Mdb, err = gorm.Open(mysql.Open(config.MysqlDns), &gorm.Config{ Mdb, err = gorm.Open(mysql.Open(config.MysqlDns), &gorm.Config{
NamingStrategy: schema.NamingStrategy{ NamingStrategy: schema.NamingStrategy{
@@ -27,9 +27,7 @@ func MysqlInit() {
if err != nil { if err != nil {
color.Red.Printf("[store_db] mysql open DB,err=%s\n", err) color.Red.Printf("[store_db] mysql open DB,err=%s\n", err)
// panic(err) // panic(err)
time.Sleep(10 * time.Second) return err
MysqlInit()
return
} }
if config.AppDebug { if config.AppDebug {
Mdb = Mdb.Debug() Mdb = Mdb.Debug()
@@ -40,7 +38,7 @@ func MysqlInit() {
// panic(err) // panic(err)
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
MysqlInit() MysqlInit()
return return err
} }
sqlDB.SetMaxIdleConns(viper.GetInt("mysql_max_idle_conns")) sqlDB.SetMaxIdleConns(viper.GetInt("mysql_max_idle_conns"))
sqlDB.SetMaxOpenConns(viper.GetInt("mysql_max_open_conns")) sqlDB.SetMaxOpenConns(viper.GetInt("mysql_max_open_conns"))
@@ -51,7 +49,8 @@ func MysqlInit() {
// panic(err) // panic(err)
time.Sleep(10 * time.Second) time.Sleep(10 * time.Second)
MysqlInit() MysqlInit()
return return err
} }
log.Sugar.Debug("[store_db] mysql connDB success") log.Sugar.Debug("[store_db] mysql connDB success")
return nil
} }
+5 -10
View File
@@ -15,7 +15,7 @@ import (
) )
// PostgreSQLInit 数据库初始化 // PostgreSQLInit 数据库初始化
func PostgreSQLInit() { func PostgreSQLInit() error {
var err error var err error
user := viper.GetString("postgres_user") user := viper.GetString("postgres_user")
pass := viper.GetString("postgres_passwd") pass := viper.GetString("postgres_passwd")
@@ -37,9 +37,7 @@ func PostgreSQLInit() {
if err != nil { if err != nil {
color.Red.Printf("[store_db] postgres open DB,err=%s\n", err) color.Red.Printf("[store_db] postgres open DB,err=%s\n", err)
// panic(err) // panic(err)
time.Sleep(10 * time.Second) return err
PostgreSQLInit()
return
} }
if config.AppDebug { if config.AppDebug {
@@ -49,9 +47,7 @@ func PostgreSQLInit() {
if err != nil { if err != nil {
color.Red.Printf("[store_db] postgres get DB,err=%s\n", err) color.Red.Printf("[store_db] postgres get DB,err=%s\n", err)
// panic(err) // panic(err)
time.Sleep(10 * time.Second) return err
PostgreSQLInit()
return
} }
sqlDB.SetMaxIdleConns(viper.GetInt("postgres_max_idle_conns")) sqlDB.SetMaxIdleConns(viper.GetInt("postgres_max_idle_conns"))
sqlDB.SetMaxOpenConns(viper.GetInt("postgres_max_open_conns")) sqlDB.SetMaxOpenConns(viper.GetInt("postgres_max_open_conns"))
@@ -60,9 +56,8 @@ func PostgreSQLInit() {
if err != nil { if err != nil {
color.Red.Printf("[store_db] postgres connDB err:%s", err.Error()) color.Red.Printf("[store_db] postgres connDB err:%s", err.Error())
// panic(err) // panic(err)
time.Sleep(10 * time.Second) return err
PostgreSQLInit()
return
} }
log.Sugar.Debug("[store_db] postgres connDB success") log.Sugar.Debug("[store_db] postgres connDB success")
return nil
} }
+11 -4
View File
@@ -7,6 +7,8 @@ import (
"github.com/assimon/luuu/util/log" "github.com/assimon/luuu/util/log"
"github.com/gookit/color" "github.com/gookit/color"
"github.com/spf13/viper" "github.com/spf13/viper"
// "github.com/glebarez/sqlite"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
@@ -14,7 +16,7 @@ import (
) )
// SqliteInit 数据库初始化 // SqliteInit 数据库初始化
func SqliteInit() { func SqliteInit() error {
var err error var err error
dbFilename := ".db" dbFilename := ".db"
if dbfile := viper.GetString("sqlite_database_filename"); len(dbfile) > 0 { if dbfile := viper.GetString("sqlite_database_filename"); len(dbfile) > 0 {
@@ -29,7 +31,9 @@ func SqliteInit() {
Logger: logger.Default.LogMode(logger.Error), Logger: logger.Default.LogMode(logger.Error),
}) })
if err != nil { if err != nil {
panic(err) color.Red.Printf("[store_db] sqlite open DB,err=%s\n", err)
// panic(err)
return err
} }
if config.AppDebug { if config.AppDebug {
Mdb = Mdb.Debug() Mdb = Mdb.Debug()
@@ -37,7 +41,8 @@ func SqliteInit() {
sqlDB, err := Mdb.DB() sqlDB, err := Mdb.DB()
if err != nil { if err != nil {
color.Red.Printf("[store_db] sqlite get DB,err=%s\n", err) color.Red.Printf("[store_db] sqlite get DB,err=%s\n", err)
panic(err) // panic(err)
return err
} }
// sqlDB.SetMaxIdleConns(viper.GetInt("sqlite_max_idle_conns")) // sqlDB.SetMaxIdleConns(viper.GetInt("sqlite_max_idle_conns"))
// sqlDB.SetMaxOpenConns(viper.GetInt("sqlite_max_open_conns")) // sqlDB.SetMaxOpenConns(viper.GetInt("sqlite_max_open_conns"))
@@ -45,7 +50,9 @@ func SqliteInit() {
err = sqlDB.Ping() err = sqlDB.Ping()
if err != nil { if err != nil {
color.Red.Printf("[store_db] sqlite connDB err:%s", err.Error()) color.Red.Printf("[store_db] sqlite connDB err:%s", err.Error())
panic(err) // panic(err)
return err
} }
log.Sugar.Debug("[store_db] sqlite connDB success") log.Sugar.Debug("[store_db] sqlite connDB success")
return nil
} }
+5 -4
View File
@@ -13,7 +13,7 @@ import (
var Rdb *redis.Client var Rdb *redis.Client
func RedisInit() { func RedisInit() error {
options := redis.Options{ options := redis.Options{
Addr: fmt.Sprintf( Addr: fmt.Sprintf(
"%s:%s", "%s:%s",
@@ -36,10 +36,11 @@ func RedisInit() {
} else if err != nil { } else if err != nil {
color.Red.Printf("[store_redis] redis connRdb err,err=%s", err) color.Red.Printf("[store_redis] redis connRdb err,err=%s", err)
// panic(err) // panic(err)
time.Sleep(10 * time.Second) // time.Sleep(10 * time.Second)
RedisInit() // RedisInit()
return return err
} else { } else {
log.Sugar.Debug("[store_redis] redis connRdb success,suc=%s", pong) log.Sugar.Debug("[store_redis] redis connRdb success,suc=%s", pong)
} }
return nil
} }