Gorm silent, except in debug mode

This commit is contained in:
Pablu23
2024-05-30 23:58:18 +02:00
parent 766da5aeb2
commit e5e7c4eb54
5 changed files with 36 additions and 11 deletions

View File

@@ -2,30 +2,46 @@ package database
import (
_ "embed"
_ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Manager struct {
ConnectionString string
Db *gorm.DB
CreateIfNotExists bool
ActivateGormLogger bool
}
func NewDatabase(connectionString string, createIfNotExists bool) Manager {
func NewDatabase(connectionString string, createIfNotExists bool, activateGormLogger bool) Manager {
return Manager{
ConnectionString: connectionString,
Db: nil,
CreateIfNotExists: createIfNotExists,
ActivateGormLogger: activateGormLogger,
}
}
func (dbMgr *Manager) Open() error {
db, err := gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{})
var db *gorm.DB
var err error
if dbMgr.ActivateGormLogger {
db, err = gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{})
if err != nil {
return err
}
} else {
db, err = gorm.Open(sqlite.Open(dbMgr.ConnectionString), &gorm.Config{
Logger: logger.Default.LogMode(logger.Silent),
})
if err != nil {
return err
}
}
dbMgr.Db = db
if dbMgr.CreateIfNotExists {
err = dbMgr.createDatabaseIfNotExists()

View File

@@ -7,12 +7,19 @@ import (
"net/http"
"regexp"
"strconv"
"strings"
"github.com/rs/zerolog/log"
)
type Bato struct{}
func (b *Bato) CleanUrlToSub(url string) string {
trimmed := strings.TrimPrefix(url, "https://bato.to/title")
trimmed = strings.Trim(trimmed, "/")
return trimmed
}
func (b *Bato) GetImageList(html string) ([]string, error) {
reg, err := regexp.Compile(`<astro-island.*props=".*;imageFiles&quot;:\[1,&quot;\[(.*)]&quot;]`)
if err != nil {

View File

@@ -1,6 +1,7 @@
package provider
type Provider interface {
CleanUrlToSub(url string) string
GetImageList(html string) (imageUrls []string, err error)
GetHtml(url string) (html string, err error)
GetNext(html string) (url string, err error)

View File

@@ -361,6 +361,7 @@ func (s *Server) HandleSetting(w http.ResponseWriter, r *http.Request) {
func (s *Server) HandleNewQuery(w http.ResponseWriter, r *http.Request) {
sub := r.PostFormValue("subUrl")
sub = s.Provider.CleanUrlToSub(sub)
url := fmt.Sprintf("/title/%s", sub)
s.CurrSubUrl = url

View File

@@ -68,7 +68,7 @@ func main() {
filePath = getDbPath()
}
db := database.NewDatabase(filePath, true)
db := database.NewDatabase(filePath, true, *debugFlag)
err := db.Open()
if err != nil {
log.Fatal().Err(err).Str("Path", filePath).Msg("Could not open Database")