This commit is contained in:
parent
dcebdb399e
commit
dcd21a0cd9
@ -2,16 +2,12 @@ package database
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/glebarez/sqlite"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"haitao_watcher/pkg/options"
|
"haitao_watcher/pkg/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
func InitDatabase(opt *options.DBOption) (*gorm.DB, error) {
|
func InitDatabase(opt *options.DBOption) (*gorm.DB, error) {
|
||||||
if opt.Timeout == 0 {
|
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", opt.Host, opt.User, opt.Password, opt.DBName, opt.Port)
|
||||||
opt.Timeout = 3000
|
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
||||||
}
|
|
||||||
dialector := sqlite.Open(fmt.Sprintf("%s?_pragma=busy_timeout(%d)", opt.Path, opt.Timeout))
|
|
||||||
|
|
||||||
return gorm.Open(dialector, &gorm.Config{})
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package options
|
package options
|
||||||
|
|
||||||
type DBOption struct {
|
type DBOption struct {
|
||||||
Path string `json:"path"`
|
Host string `yaml:"host"`
|
||||||
Timeout int `json:"timeout"`
|
User string `yaml:"user"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
|
Port string `yaml:"port"`
|
||||||
|
DBName string `yaml:"dbname"`
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user