config.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/qor5/admin/pagebuilder"
  5. "github.com/qor5/admin/pagebuilder/example"
  6. "github.com/qor5/admin/presets"
  7. "github.com/qor5/admin/presets/gorm2op"
  8. "github.com/qor5/admin/utils"
  9. "github.com/qor5/ui/vuetify"
  10. "github.com/qor5/web"
  11. "github.com/qor5/x/perm"
  12. h "github.com/theplant/htmlgo"
  13. "golang.org/x/text/language"
  14. )
  15. const (
  16. PublishDir = "./publish"
  17. )
  18. type Config struct {
  19. pb *presets.Builder
  20. pageBuilder *pagebuilder.Builder
  21. }
  22. func InitApp() *http.ServeMux {
  23. c := newPB()
  24. mux := SetupRouter(c)
  25. return mux
  26. }
  27. func newPB() Config {
  28. db := ConnectDB()
  29. b := presets.New().VuetifyOptions(`
  30. {
  31. icons: {
  32. iconfont: 'md', // 'mdi' || 'mdiSvg' || 'md' || 'fa' || 'fa4'
  33. },
  34. theme: {
  35. themes: {
  36. light: {
  37. primary: "#ed6f2d",
  38. secondary: "#009688",
  39. accent: "#ff5722",
  40. error: "#f44336",
  41. warning: "#ff9800",
  42. info: "#8bc34a",
  43. success: "#4caf50"
  44. },
  45. },
  46. },
  47. }
  48. `)
  49. b.URIPrefix("/admin").DataOperator(gorm2op.DataOperator(db)).
  50. BrandFunc(func(ctx *web.EventContext) h.HTMLComponent {
  51. logo := `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAESElEQVR4Xu1aS1LbQBDVSLYhq8AJsE8A3MAs+KyCfQNYJlSq7BMEToCphGQZOEHEjs8C5wTxDeKcIMoqhXGkdLsk12ikkWakMR7wTBULrPl0v3790aiJteCDLLj+lgHAMGDBEXgSF2g2myvVarUug7XjON719fVQZk2RuTMDAJWu1WodQsg+CIbKrxQRENa4QRBc3t7eugXXZy6b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0VnZ+zBojTqc7tLMvrE0xrfEYdhvCn7CuOQlDq9CVLuK8HLTYNXotNJgApfboKZX2yrTI7y3Ibn0IQ8MWFbYF/Mg0KHiTUWZYLQHR42DiJ3V54l5/Wl1tQTqXL0C0HYcd4T6SzTBgApWJqtJkBQCNjzEUUw4C5wK7RoQvPgP92SnFu7MAGXwAAAABJRU5ErkJggg==`
  52. return vuetify.VContainer(
  53. h.Img(logo).Attr("width", "120"),
  54. ).Class("ma-n4")
  55. }).
  56. HomePageFunc(func(ctx *web.EventContext) (r web.PageResponse, err error) {
  57. r.Body = vuetify.VContainer(
  58. h.H1("Home"),
  59. h.P().Text("Change your home page here"))
  60. return
  61. })
  62. b.Permission(
  63. perm.New().Policies(
  64. perm.PolicyFor(perm.Anybody).WhoAre(perm.Allowed).ToDo(presets.PermCreate, presets.PermUpdate, presets.PermDelete, presets.PermGet, presets.PermList).On("*"),
  65. perm.PolicyFor(perm.Anybody).WhoAre(perm.Denied).ToDo(presets.PermCreate, presets.PermUpdate, presets.PermDelete).On("*:activity_logs:*"),
  66. ),
  67. )
  68. utils.Configure(b)
  69. //ab := activity.New(b, db).SetCreatorContextKey(login.UserKey)
  70. //l10nBuilder := l10n.New()
  71. pageBuilder := example.ConfigPageBuilder(db, "/admin/page_builder", ``, b.I18n())
  72. //pm := pageBuilder.Configure(b, db, l10nBuilder, ab, nil, nil)
  73. //tm := pageBuilder.ConfigTemplate(b, db)
  74. //cm := pageBuilder.ConfigCategory(b, db, nil)
  75. //ab.RegisterModels(pm, tm, cm)
  76. b.I18n().
  77. SupportLanguages(language.English, language.SimplifiedChinese).
  78. RegisterForModule(language.English, I18nExampleKey, Messages_en_US).
  79. RegisterForModule(language.SimplifiedChinese, I18nExampleKey, Messages_zh_CN).
  80. RegisterForModule(language.SimplifiedChinese, presets.ModelsI18nModuleKey, Messages_zh_CN_ModelsI18nModuleKey).
  81. GetSupportLanguagesFromRequestFunc(func(r *http.Request) []language.Tag {
  82. return b.I18n().GetSupportLanguages()
  83. })
  84. b.MenuOrder(
  85. b.MenuGroup("Page Builder").SubItems("pages", "page_templates", "page_categories").Icon("web"),
  86. "shared_containers",
  87. )
  88. initWebsiteData(db)
  89. return Config{
  90. pb: b,
  91. pageBuilder: pageBuilder,
  92. }
  93. }