config.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package example
  2. import (
  3. "embed"
  4. "io/fs"
  5. "net/http"
  6. "os"
  7. "path"
  8. "github.com/qor5/x/i18n"
  9. h "github.com/theplant/htmlgo"
  10. media_view "github.com/qor5/admin/media/views"
  11. "github.com/qor5/admin/pagebuilder"
  12. "github.com/qor5/admin/pagebuilder/example/containers"
  13. "github.com/qor5/admin/pagebuilder/example/layouts"
  14. "github.com/qor5/admin/richeditor"
  15. "gorm.io/driver/postgres"
  16. "gorm.io/gorm"
  17. "gorm.io/gorm/logger"
  18. )
  19. func ConnectDB() (db *gorm.DB) {
  20. var err error
  21. db, err = gorm.Open(postgres.Open(os.Getenv("DB_PARAMS")), &gorm.Config{})
  22. if err != nil {
  23. panic(err)
  24. }
  25. db.Logger = db.Logger.LogMode(logger.Info)
  26. return
  27. }
  28. //go:embed assets/images
  29. var containerImages embed.FS
  30. func ConfigPageBuilder(db *gorm.DB, prefix, style string, i18nB *i18n.Builder) *pagebuilder.Builder {
  31. err := db.AutoMigrate(
  32. &containers.WebHeader{},
  33. &containers.WebFooter{},
  34. &containers.VideoBanner{},
  35. &containers.Heading{},
  36. &containers.BrandGrid{},
  37. &containers.ListContent{},
  38. &containers.ImageContainer{},
  39. &containers.InNumbers{},
  40. &containers.ContactForm{},
  41. &containers.PageTitle{},
  42. &containers.ListContentLite{},
  43. &containers.ListContentWithImage{},
  44. )
  45. if err != nil {
  46. panic(err)
  47. }
  48. pb := pagebuilder.New(prefix, db, i18nB)
  49. if style != "" {
  50. pb.PageStyle(h.RawHTML(style))
  51. }
  52. media_view.Configure(pb.GetPresetsBuilder(), db)
  53. richeditor.Plugins = []string{"alignment", "table", "video", "imageinsert"}
  54. pb.GetPresetsBuilder().ExtraAsset("/redactor.js", "text/javascript", richeditor.JSComponentsPack())
  55. pb.GetPresetsBuilder().ExtraAsset("/redactor.css", "text/css", richeditor.CSSComponentsPack())
  56. pb.PageLayout(layouts.DefaultPageLayoutFunc)
  57. fSys, _ := fs.Sub(containerImages, "assets/images")
  58. imagePrefix := "/assets/images"
  59. pb.Images(http.StripPrefix(path.Join(prefix, imagePrefix), http.FileServer(http.FS(fSys))), imagePrefix)
  60. containers.RegisterHeader(pb)
  61. containers.RegisterFooter(pb)
  62. containers.RegisterVideoBannerContainer(pb)
  63. containers.RegisterHeadingContainer(pb, db)
  64. containers.RegisterBrandGridContainer(pb, db)
  65. containers.RegisterListContentContainer(pb, db)
  66. containers.RegisterImageContainer(pb, db)
  67. containers.RegisterInNumbersContainer(pb, db)
  68. containers.RegisterContactFormContainer(pb, db)
  69. containers.RegisterPageTitleContainer(pb, db)
  70. containers.RegisterListContentLiteContainer(pb, db)
  71. containers.RegisterListContentWithImageContainer(pb, db)
  72. return pb
  73. }