From dcd21a0cd9416b27e3ba131860403c766f3b4651 Mon Sep 17 00:00:00 2001 From: timerzz Date: Sun, 12 May 2024 16:35:46 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=95=B0=E6=8D=AE=E5=BA=93=E6=94=B9?= =?UTF-8?q?=E4=B8=BApgsql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pkg/database/init.go | 10 +++------- pkg/options/db.go | 7 +++++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/pkg/database/init.go b/pkg/database/init.go index 7f55760..a20e9d9 100644 --- a/pkg/database/init.go +++ b/pkg/database/init.go @@ -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{}) } diff --git a/pkg/options/db.go b/pkg/options/db.go index ee4e915..55df2f9 100644 --- a/pkg/options/db.go +++ b/pkg/options/db.go @@ -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"` }