diff --git a/Dockerfile b/Dockerfile index 4bcbccf..dd0c758 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:alpine as builder +FROM golang:alpine AS builder RUN apk add --no-cache --update git build-base ENV CGO_ENABLED=1 @@ -9,7 +9,7 @@ RUN go mod tidy \ && go build -ldflags "-s -w" \ -o epusdt . -FROM alpine:latest as runner +FROM alpine:latest AS runner ENV TZ=Asia/Shanghai RUN apk --no-cache add ca-certificates tzdata diff --git a/src/model/dao/init.go b/src/model/dao/init.go index 66c8b51..920b6cb 100644 --- a/src/model/dao/init.go +++ b/src/model/dao/init.go @@ -1,7 +1,15 @@ package dao -func Init() { - DBInit() +import ( + "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) + } } diff --git a/src/model/dao/mdb.go b/src/model/dao/mdb.go index 99887c1..f345ac6 100644 --- a/src/model/dao/mdb.go +++ b/src/model/dao/mdb.go @@ -4,20 +4,27 @@ import ( "strings" "github.com/spf13/viper" - "gorm.io/gorm" ) var Mdb *gorm.DB -func DBInit() { +func DBInit() error { dbType := viper.GetString("db_type") if strings.EqualFold(dbType, "postgres") { - PostgreSQLInit() + if err := PostgreSQLInit(); err != nil { + return err + } } else if strings.EqualFold(dbType, "sqlite") { - SqliteInit() + if err := SqliteInit(); err != nil { + return err + } } else { - MysqlInit() + if err := MysqlInit(); err != nil { + return err + } } + MdbTableInit() + return nil } diff --git a/src/model/dao/mdb_mysql.go b/src/model/dao/mdb_mysql.go index bfcf63f..3e27f37 100644 --- a/src/model/dao/mdb_mysql.go +++ b/src/model/dao/mdb_mysql.go @@ -15,7 +15,7 @@ import ( ) // MysqlInit 数据库初始化 -func MysqlInit() { +func MysqlInit() error { var err error Mdb, err = gorm.Open(mysql.Open(config.MysqlDns), &gorm.Config{ NamingStrategy: schema.NamingStrategy{ @@ -27,9 +27,7 @@ func MysqlInit() { if err != nil { color.Red.Printf("[store_db] mysql open DB,err=%s\n", err) // panic(err) - time.Sleep(10 * time.Second) - MysqlInit() - return + return err } if config.AppDebug { Mdb = Mdb.Debug() @@ -40,7 +38,7 @@ func MysqlInit() { // panic(err) time.Sleep(10 * time.Second) MysqlInit() - return + return err } sqlDB.SetMaxIdleConns(viper.GetInt("mysql_max_idle_conns")) sqlDB.SetMaxOpenConns(viper.GetInt("mysql_max_open_conns")) @@ -51,7 +49,8 @@ func MysqlInit() { // panic(err) time.Sleep(10 * time.Second) MysqlInit() - return + return err } log.Sugar.Debug("[store_db] mysql connDB success") + return nil } diff --git a/src/model/dao/mdb_postgres.go b/src/model/dao/mdb_postgres.go index fbac9ee..d14c65a 100644 --- a/src/model/dao/mdb_postgres.go +++ b/src/model/dao/mdb_postgres.go @@ -15,7 +15,7 @@ import ( ) // PostgreSQLInit 数据库初始化 -func PostgreSQLInit() { +func PostgreSQLInit() error { var err error user := viper.GetString("postgres_user") pass := viper.GetString("postgres_passwd") @@ -37,9 +37,7 @@ func PostgreSQLInit() { if err != nil { color.Red.Printf("[store_db] postgres open DB,err=%s\n", err) // panic(err) - time.Sleep(10 * time.Second) - PostgreSQLInit() - return + return err } if config.AppDebug { @@ -49,9 +47,7 @@ func PostgreSQLInit() { if err != nil { color.Red.Printf("[store_db] postgres get DB,err=%s\n", err) // panic(err) - time.Sleep(10 * time.Second) - PostgreSQLInit() - return + return err } sqlDB.SetMaxIdleConns(viper.GetInt("postgres_max_idle_conns")) sqlDB.SetMaxOpenConns(viper.GetInt("postgres_max_open_conns")) @@ -60,9 +56,8 @@ func PostgreSQLInit() { if err != nil { color.Red.Printf("[store_db] postgres connDB err:%s", err.Error()) // panic(err) - time.Sleep(10 * time.Second) - PostgreSQLInit() - return + return err } log.Sugar.Debug("[store_db] postgres connDB success") + return nil } diff --git a/src/model/dao/mdb_sqlite.go b/src/model/dao/mdb_sqlite.go index 09767dd..7806187 100644 --- a/src/model/dao/mdb_sqlite.go +++ b/src/model/dao/mdb_sqlite.go @@ -7,6 +7,8 @@ import ( "github.com/assimon/luuu/util/log" "github.com/gookit/color" "github.com/spf13/viper" + + // "github.com/glebarez/sqlite" "gorm.io/driver/sqlite" "gorm.io/gorm" "gorm.io/gorm/logger" @@ -14,7 +16,7 @@ import ( ) // SqliteInit 数据库初始化 -func SqliteInit() { +func SqliteInit() error { var err error dbFilename := ".db" if dbfile := viper.GetString("sqlite_database_filename"); len(dbfile) > 0 { @@ -29,7 +31,9 @@ func SqliteInit() { Logger: logger.Default.LogMode(logger.Error), }) if err != nil { - panic(err) + color.Red.Printf("[store_db] sqlite open DB,err=%s\n", err) + // panic(err) + return err } if config.AppDebug { Mdb = Mdb.Debug() @@ -37,7 +41,8 @@ func SqliteInit() { sqlDB, err := Mdb.DB() if err != nil { 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.SetMaxOpenConns(viper.GetInt("sqlite_max_open_conns")) @@ -45,7 +50,9 @@ func SqliteInit() { err = sqlDB.Ping() if err != nil { 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") + return nil } diff --git a/src/model/dao/rdb.go b/src/model/dao/rdb.go index a1e990f..0bda4c5 100644 --- a/src/model/dao/rdb.go +++ b/src/model/dao/rdb.go @@ -13,7 +13,7 @@ import ( var Rdb *redis.Client -func RedisInit() { +func RedisInit() error { options := redis.Options{ Addr: fmt.Sprintf( "%s:%s", @@ -36,10 +36,11 @@ func RedisInit() { } else if err != nil { color.Red.Printf("[store_redis] redis connRdb err,err=%s", err) // panic(err) - time.Sleep(10 * time.Second) - RedisInit() - return + // time.Sleep(10 * time.Second) + // RedisInit() + return err } else { log.Sugar.Debug("[store_redis] redis connRdb success,suc=%s", pong) } + return nil }