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 ( import (
_ "embed" _ "embed"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"gorm.io/driver/sqlite" "gorm.io/driver/sqlite"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger"
) )
type Manager struct { type Manager struct {
ConnectionString string ConnectionString string
Db *gorm.DB Db *gorm.DB
CreateIfNotExists bool CreateIfNotExists bool
ActivateGormLogger bool
} }
func NewDatabase(connectionString string, createIfNotExists bool) Manager { func NewDatabase(connectionString string, createIfNotExists bool, activateGormLogger bool) Manager {
return Manager{ return Manager{
ConnectionString: connectionString, ConnectionString: connectionString,
Db: nil, Db: nil,
CreateIfNotExists: createIfNotExists, CreateIfNotExists: createIfNotExists,
ActivateGormLogger: activateGormLogger,
} }
} }
func (dbMgr *Manager) Open() error { 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 { if err != nil {
return err 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 dbMgr.Db = db
if dbMgr.CreateIfNotExists { if dbMgr.CreateIfNotExists {
err = dbMgr.createDatabaseIfNotExists() err = dbMgr.createDatabaseIfNotExists()

View File

@@ -7,12 +7,19 @@ import (
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strconv"
"strings"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
type Bato struct{} 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) { func (b *Bato) GetImageList(html string) ([]string, error) {
reg, err := regexp.Compile(`<astro-island.*props=".*;imageFiles&quot;:\[1,&quot;\[(.*)]&quot;]`) reg, err := regexp.Compile(`<astro-island.*props=".*;imageFiles&quot;:\[1,&quot;\[(.*)]&quot;]`)
if err != nil { if err != nil {

View File

@@ -1,6 +1,7 @@
package provider package provider
type Provider interface { type Provider interface {
CleanUrlToSub(url string) string
GetImageList(html string) (imageUrls []string, err error) GetImageList(html string) (imageUrls []string, err error)
GetHtml(url string) (html string, err error) GetHtml(url string) (html string, err error)
GetNext(html string) (url 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) { func (s *Server) HandleNewQuery(w http.ResponseWriter, r *http.Request) {
sub := r.PostFormValue("subUrl") sub := r.PostFormValue("subUrl")
sub = s.Provider.CleanUrlToSub(sub)
url := fmt.Sprintf("/title/%s", sub) url := fmt.Sprintf("/title/%s", sub)
s.CurrSubUrl = url s.CurrSubUrl = url

View File

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