diff --git a/domain/service/cert_service.go b/domain/service/cert_service.go index a7ce886..a651286 100644 --- a/domain/service/cert_service.go +++ b/domain/service/cert_service.go @@ -3,7 +3,6 @@ package service import ( "autossl/domain/model" "autossl/infrastructure/acme" - "autossl/infrastructure/database" "autossl/infrastructure/exception" "autossl/infrastructure/repository" "autossl/infrastructure/util" @@ -16,9 +15,7 @@ import ( ) func ExistCert(domain string) bool { - repo := &repository.CertRepo{ - Db: database.Init(), - } + repo := repository.GetCertRepo() byDomain, err := repo.FindByDomain(domain) if err != nil { @@ -43,9 +40,7 @@ func CreateCert(domain string, code string) error { return err } - repo := &repository.CertRepo{ - Db: database.Init(), - } + repo := repository.GetCertRepo() cert := &model.Cert{ Code: code, Domain: domain, @@ -79,9 +74,7 @@ func ImportCert(domainName string, certFile *multipart.FileHeader, keyFile *mult } func ListCert() ([]*model.Cert, error) { - repo := &repository.CertRepo{ - Db: database.Init(), - } + repo := repository.GetCertRepo() list, err := repo.List() if err != nil { return nil, err @@ -101,9 +94,7 @@ func ListCert() ([]*model.Cert, error) { func DeleteCert(code string) error { var err error - repo := &repository.CertRepo{ - Db: database.Init(), - } + repo := repository.GetCertRepo() byCode, err := repo.FindByCode(code) if err != nil { diff --git a/infrastructure/database/connect.go b/infrastructure/database/connect.go index 5f3acf9..d865e4a 100644 --- a/infrastructure/database/connect.go +++ b/infrastructure/database/connect.go @@ -6,10 +6,23 @@ import ( "entgo.io/ent/dialect/sql" _ "github.com/mattn/go-sqlite3" "log" + "sync" "time" ) -func Init() *ent.Client { +var ( + dbClient *ent.Client + once sync.Once +) + +func GetDBClient() *ent.Client { + once.Do(func() { + dbClient = initDB() + }) + return dbClient +} + +func initDB() *ent.Client { drv, err := sql.Open(dialect.SQLite, "/root/data/autossl.db?mode=memory&cache=shared&_fk=1") if err != nil { log.Fatalf("failed opening connection to sqlite: %v", err) diff --git a/infrastructure/repository/cert_repository.go b/infrastructure/repository/cert_repository.go index 2a6c057..8b08984 100644 --- a/infrastructure/repository/cert_repository.go +++ b/infrastructure/repository/cert_repository.go @@ -2,6 +2,7 @@ package repository import ( "autossl/domain/model" + "autossl/infrastructure/database" "autossl/infrastructure/ent" "autossl/infrastructure/ent/cert" "context" @@ -11,6 +12,17 @@ type CertRepo struct { Db *ent.Client } +var certRepo *CertRepo + +func GetCertRepo() *CertRepo { + if certRepo == nil { + certRepo = &CertRepo{ + Db: database.GetDBClient(), + } + } + return certRepo +} + func (repo *CertRepo) Create(cert *model.Cert) error { _, err := repo.Db.Cert. Create(). diff --git a/main.go b/main.go index ba3275d..266acf9 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,11 @@ func main() { // database database.AutoMigrate() + defer func() { + if err := database.GetDBClient().Close(); err != nil { + log.Printf("Failed to close database connection: %v", err) + } + }() // Logger e.Use(config.RequestLogger())