- Add function RenderNamedTemplate executes a named template from the registered set with the given data and returns the rendered HTML.
- gormlogger.Silent is the log level that suppresses all Gorm log messages, including the "record not found" warnings.
This commit is contained in:
parent
59eadf29d0
commit
19dba8f504
2 changed files with 32 additions and 4 deletions
16
core.go
16
core.go
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
gormlogger "gorm.io/gorm/logger"
|
||||||
)
|
)
|
||||||
|
|
||||||
var loggr *logger.Logger
|
var loggr *logger.Logger
|
||||||
|
|
@ -517,13 +518,18 @@ func NewGorm() *gorm.DB {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("error locating sqlite file: %v", err.Error()))
|
panic(fmt.Sprintf("error locating sqlite file: %v", err.Error()))
|
||||||
}
|
}
|
||||||
db, err = gorm.Open(sqlite.Open(fullSqlitePath), &gorm.Config{})
|
db, err = gorm.Open(sqlite.Open(fullSqlitePath), &gorm.Config{
|
||||||
|
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
|
||||||
|
})
|
||||||
default:
|
default:
|
||||||
panic("database driver not selected")
|
panic("database driver not selected")
|
||||||
}
|
}
|
||||||
if gormC.EnableGorm && err != nil {
|
if gormC.EnableGorm && err != nil {
|
||||||
panic(fmt.Sprintf("gorm has problem connecting to %v, (if it's not needed you can disable it in config/gorm.go): %v", os.Getenv("DB_DRIVER"), err))
|
panic(fmt.Sprintf("gorm has problem connecting to %v, (if it's not needed you can disable it in config/gorm.go): %v", os.Getenv("DB_DRIVER"), err))
|
||||||
}
|
}
|
||||||
|
if db != nil {
|
||||||
|
db.Logger = db.Logger.LogMode(gormlogger.Silent)
|
||||||
|
}
|
||||||
return db
|
return db
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -560,7 +566,9 @@ func postgresConnect() (*gorm.DB, error) {
|
||||||
os.Getenv("POSTGRES_SSL_MODE"),
|
os.Getenv("POSTGRES_SSL_MODE"),
|
||||||
os.Getenv("POSTGRES_TIMEZONE"),
|
os.Getenv("POSTGRES_TIMEZONE"),
|
||||||
)
|
)
|
||||||
return gorm.Open(postgres.Open(dsn), &gorm.Config{})
|
return gorm.Open(postgres.Open(dsn), &gorm.Config{
|
||||||
|
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// mysqlConnect establishes a connection to a MySQL database using credentials and configurations from environment variables.
|
// mysqlConnect establishes a connection to a MySQL database using credentials and configurations from environment variables.
|
||||||
|
|
@ -581,7 +589,9 @@ func mysqlConnect() (*gorm.DB, error) {
|
||||||
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
|
DontSupportRenameIndex: true, // drop & create when rename index, rename index not supported before MySQL 5.7, MariaDB
|
||||||
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
|
DontSupportRenameColumn: true, // `change` when rename column, rename column not supported before MySQL 8, MariaDB
|
||||||
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
|
SkipInitializeWithVersion: false, // auto configure based on currently MySQL version
|
||||||
}), &gorm.Config{})
|
}), &gorm.Config{
|
||||||
|
Logger: gormlogger.Default.LogMode(gormlogger.Silent),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// getJWT returns a function that initializes and provides a *JWT instance configured with environment variables.
|
// getJWT returns a function that initializes and provides a *JWT instance configured with environment variables.
|
||||||
|
|
|
||||||
18
templates.go
18
templates.go
|
|
@ -360,3 +360,21 @@ func NewTemplates(components embed.FS, templates embed.FS) {
|
||||||
)
|
)
|
||||||
tmpl = template.Must(tmpl.ParseFS(templates, pathst...))
|
tmpl = template.Must(tmpl.ParseFS(templates, pathst...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// RenderNamedTemplate executes a named template from the registered set with
|
||||||
|
// the given data and returns the rendered HTML.
|
||||||
|
// Usage:
|
||||||
|
//
|
||||||
|
// html, err := core.RenderNamedTemplate("tabler_table", data)
|
||||||
|
// if err != nil {
|
||||||
|
// // handle error
|
||||||
|
// }
|
||||||
|
// return c.Response.HTML(string(html))
|
||||||
|
func RenderNamedTemplate(name string, data interface{}) (template.HTML, error) {
|
||||||
|
var buf strings.Builder
|
||||||
|
if err := tmpl.ExecuteTemplate(&buf, name, data); err != nil {
|
||||||
|
return "", fmt.Errorf("failed to execute template %q: %w", name, err)
|
||||||
|
}
|
||||||
|
return template.HTML(buf.String()), nil
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue