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