Skip to content

Commit

Permalink
fix: 如果数据库无法链接直接终止程序运行
Browse files Browse the repository at this point in the history
  • Loading branch information
pojol committed Jun 30, 2023
1 parent 1065961 commit 796fabe
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 56 deletions.
5 changes: 3 additions & 2 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ end
|`base64`|`http`|`protobuf`|`mongoDB`|`json`|
|`md5`|`uuid`|`random`|...|

## [在线试用](http://123.60.17.61:7777) <--
## [文档](https://pojol.gitee.io/gobot/#/) <--
## [在线试用](http://123.60.17.61:7777)
## [文档](https://pojol.gitee.io/gobot/#/)

## [视频演示](https://www.bilibili.com/video/BV1sS4y1z7Dg/?vd_source=7c2dfd750914fd5f8a9811b19f0bf447)

## 编辑器预览
[![image.png](https://i.postimg.cc/t4jMVjp1/image.png)](https://postimg.cc/PPS4B0Lh)
102 changes: 49 additions & 53 deletions database/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"os"
"sync"

"github.com/glebarez/sqlite"
"gorm.io/driver/mysql"
Expand All @@ -30,7 +29,6 @@ type Cache struct {
}

var db *Cache
var once sync.Once

func GetConfig() *Conf {
return db.conf
Expand All @@ -52,63 +50,61 @@ func GetTask() *Task {
return db.task
}

func Init(NoDBMode bool) *Cache {
once.Do(func() {

var sqlptr *gorm.DB
var err error

if NoDBMode {
sqlptr, err = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
} else {
pwd := os.Getenv("MYSQL_PASSWORD")
if pwd == "" {
panic(errors.New("mysql password is not defined"))
}

name := os.Getenv("MYSQL_DATABASE")
if name == "" {
panic(errors.New("mysql database is not defined"))
}

host := os.Getenv("MYSQL_HOST")
if host == "" {
panic(errors.New("mysql host is not defined"))
}

user := os.Getenv("MYSQL_USER")
if user == "" {
panic(errors.New("mysql user is not defined"))
}

dsn := user + ":" + pwd + "@tcp(" + host + ")/" + name + "?charset=utf8&parseTime=True&loc=Local"

sqlptr, err = gorm.Open(mysql.New(mysql.Config{
DSN: dsn, // data source name
DefaultStringSize: 256, // default size for string fields
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
}), &gorm.Config{})
func Init(NoDBMode bool) (*Cache, error) {

var sqlptr *gorm.DB
var err error

if NoDBMode {
sqlptr, err = gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})
} else {
pwd := os.Getenv("MYSQL_PASSWORD")
if pwd == "" {
panic(errors.New("mysql password is not defined"))
}

name := os.Getenv("MYSQL_DATABASE")
if name == "" {
panic(errors.New("mysql database is not defined"))
}

if err != nil {
fmt.Println("open mysql err", err.Error())
return
host := os.Getenv("MYSQL_HOST")
if host == "" {
panic(errors.New("mysql host is not defined"))
}

db = &Cache{
mysqlptr: sqlptr,
conf: CreateConfig(sqlptr),
prefab: CreatePrefab(sqlptr),
behavior: CreateBehavior(sqlptr),
report: CreateReport(sqlptr),
task: CreateTask(sqlptr),
user := os.Getenv("MYSQL_USER")
if user == "" {
panic(errors.New("mysql user is not defined"))
}
})

return db
dsn := user + ":" + pwd + "@tcp(" + host + ")/" + name + "?charset=utf8&parseTime=True&loc=Local"

sqlptr, err = gorm.Open(mysql.New(mysql.Config{
DSN: dsn, // data source name
DefaultStringSize: 256, // default size for string fields
DisableDatetimePrecision: true, // disable datetime precision, which not supported before MySQL 5.6
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
}), &gorm.Config{})
}

if err != nil {
fmt.Println("gorm open err", err.Error())
return nil, err
}

db = &Cache{
mysqlptr: sqlptr,
conf: CreateConfig(sqlptr),
prefab: CreatePrefab(sqlptr),
behavior: CreateBehavior(sqlptr),
report: CreateReport(sqlptr),
task: CreateTask(sqlptr),
}

return db, nil
}

func init() {
Expand Down
5 changes: 4 additions & 1 deletion factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ func Create(opts ...Option) (*Factory, error) {
opt(&p)
}

db := database.Init(p.NoDBMode)
db, err := database.Init(p.NoDBMode)
if err != nil {
panic(err)
}
f := &Factory{
parm: p,
db: db,
Expand Down

0 comments on commit 796fabe

Please sign in to comment.