Skip to content

Commit

Permalink
feat: optimize database connect
Browse files Browse the repository at this point in the history
  • Loading branch information
pupilcc committed Sep 27, 2024
1 parent 3e1eeee commit d4fa059
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 14 deletions.
17 changes: 4 additions & 13 deletions domain/service/cert_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion infrastructure/database/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions infrastructure/repository/cert_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package repository

import (
"autossl/domain/model"
"autossl/infrastructure/database"
"autossl/infrastructure/ent"
"autossl/infrastructure/ent/cert"
"context"
Expand All @@ -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().
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit d4fa059

Please sign in to comment.