refactor: Refactor import statements and error handling in multiple files

- Fix import statements in main.go
- Improve error handling and logging in model/main.go
This commit is contained in:
Laisky.Cai 2023-12-12 06:34:08 +00:00
parent c503a87c74
commit 8d8fdaa2af
2 changed files with 26 additions and 23 deletions

13
main.go
View File

@ -3,9 +3,6 @@ package main
import (
"embed"
"fmt"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"one-api/common"
"one-api/controller"
"one-api/middleware"
@ -13,6 +10,10 @@ import (
"one-api/router"
"os"
"strconv"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
)
//go:embed web/build
@ -33,19 +34,19 @@ func main() {
// Initialize SQL Database
err := model.InitDB()
if err != nil {
common.FatalLog("failed to initialize database: " + err.Error())
common.FatalLog(fmt.Sprintf("failed to initialize database: %+v", err))
}
defer func() {
err := model.CloseDB()
if err != nil {
common.FatalLog("failed to close database: " + err.Error())
common.FatalLog(fmt.Sprintf("failed to close database: %+v", err))
}
}()
// Initialize Redis
err = common.InitRedisClient()
if err != nil {
common.FatalLog("failed to initialize Redis: " + err.Error())
common.FatalLog(fmt.Sprintf("failed to initialize Redis: %+v", err))
}
// Initialize options

View File

@ -1,14 +1,16 @@
package model
import (
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"one-api/common"
"os"
"strings"
"time"
"github.com/Laisky/errors/v2"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
var DB *gorm.DB
@ -20,7 +22,7 @@ func createRootAccountIfNeed() error {
common.SysLog("no user exists, create a root user for you: username is root, password is 123456")
hashedPassword, err := common.Password2Hash("123456")
if err != nil {
return err
return errors.WithStack(err)
}
rootUser := User{
Username: "root",
@ -73,7 +75,7 @@ func InitDB() (err error) {
DB = db
sqlDB, err := DB.DB()
if err != nil {
return err
return errors.WithStack(err)
}
sqlDB.SetMaxIdleConns(common.GetOrDefault("SQL_MAX_IDLE_CONNS", 100))
sqlDB.SetMaxOpenConns(common.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
@ -85,46 +87,46 @@ func InitDB() (err error) {
common.SysLog("database migration started")
err = db.AutoMigrate(&Channel{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&Token{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&User{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&Option{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&Redemption{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&Ability{})
if err != nil {
return err
return errors.WithStack(err)
}
err = db.AutoMigrate(&Log{})
if err != nil {
return err
return errors.WithStack(err)
}
common.SysLog("database migrated")
err = createRootAccountIfNeed()
return err
return errors.WithStack(err)
} else {
common.FatalLog(err)
}
return err
return errors.WithStack(err)
}
func CloseDB() error {
sqlDB, err := DB.DB()
if err != nil {
return err
return errors.WithStack(err)
}
err = sqlDB.Close()
return err
return errors.WithStack(err)
}