config.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/qor/oss/filesystem"
  5. "github.com/qor5/admin/activity"
  6. "github.com/qor5/admin/l10n"
  7. l10n_view "github.com/qor5/admin/l10n/views"
  8. media_view "github.com/qor5/admin/media/views"
  9. "github.com/qor5/admin/pagebuilder"
  10. "github.com/qor5/admin/pagebuilder/example"
  11. "github.com/qor5/admin/presets"
  12. "github.com/qor5/admin/presets/gorm2op"
  13. "github.com/qor5/admin/publish"
  14. publish_view "github.com/qor5/admin/publish/views"
  15. "github.com/qor5/admin/utils"
  16. "github.com/qor5/ui/vuetify"
  17. "github.com/qor5/web"
  18. "github.com/qor5/x/login"
  19. "github.com/qor5/x/perm"
  20. h "github.com/theplant/htmlgo"
  21. "golang.org/x/text/language"
  22. )
  23. const (
  24. PublishDir = "./publish"
  25. )
  26. type Config struct {
  27. pb *presets.Builder
  28. pageBuilder *pagebuilder.Builder
  29. }
  30. func InitApp() *http.ServeMux {
  31. c := newPB()
  32. mux := SetupRouter(c)
  33. return mux
  34. }
  35. func newPB() Config {
  36. db := ConnectDB()
  37. b := presets.New().VuetifyOptions(`
  38. {
  39. icons: {
  40. iconfont: 'md', // 'mdi' || 'mdiSvg' || 'md' || 'fa' || 'fa4'
  41. },
  42. theme: {
  43. themes: {
  44. light: {
  45. primary: "#ed6f2d",
  46. secondary: "#009688",
  47. accent: "#ff5722",
  48. error: "#f44336",
  49. warning: "#ff9800",
  50. info: "#8bc34a",
  51. success: "#4caf50"
  52. },
  53. },
  54. },
  55. }
  56. `)
  57. b.URIPrefix("/admin").DataOperator(gorm2op.DataOperator(db)).
  58. BrandFunc(func(ctx *web.EventContext) h.HTMLComponent {
  59. logo := `data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAOAAAAAwCAYAAAAIE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zl6L+jVM8pfrcIV/rZChTYoBhQ6QGT3MMIfiAil17h4HeZ24/1ynB9CtcR+WPZvhZihz/MFtAeYZxnfJ6B5jIYSWdymxtI/fXxO9f/ab4c/sU3c7Pa05bQFNTCFIgvgobbgrZwkWHCqZzkrpHfJJTRRGaGQ1vrkt6EyqwD0QRwXVwVPof/xtvMHOdqqApPmjg2BeIzYOwsk0kIE14JE54G86yMhVGv16+VUzlLdyGdeYPsgJT9ZmhwLDTIiJmVEeIOou9XyTUWlaFPY0ehQEEE3ZRVxRpZHEjkWpUeqv7/goZ/NRBf8VTqXJQKTsCIuyxjwYR3o82bQ46Xk+UZlKlvhKzfI6+Z3EZ5c6RZz1bHzP/8Rciqoiiq5fXTX+81LeOgwUPQ4ClocAVR9f8YdiiTZCEDz40cOi5cyaHX+WVu1/YnDRpaLwu0uRvjADb/JxHAk8k5uM3UZE5p+OmicRtROZVyeoQ7c21+qVhtG8bZWZsmsG0LVUXurIv2IvkRNO4OEF+3FbrzrZcvfWQ6WK38jTtDF3nvyqwiu3aX1FgFjeJSYL1ewmV7wUUB5WBkrX9Mo9cG1snjJS+Ganc1SguUUiClQEqBFqTA/wG6drh4/QLmIwAAAABJRU5ErkJggg==`
  60. return vuetify.VContainer(
  61. h.Img(logo).Attr("width", "150"),
  62. ).Class("ma-n4")
  63. }).
  64. HomePageFunc(func(ctx *web.EventContext) (r web.PageResponse, err error) {
  65. r.Body = vuetify.VContainer(
  66. h.H1("Home"),
  67. h.P().Text("Change your home page here"))
  68. return
  69. })
  70. b.Permission(
  71. perm.New().Policies(
  72. perm.PolicyFor(perm.Anybody).WhoAre(perm.Allowed).ToDo(presets.PermCreate, presets.PermUpdate, presets.PermDelete, presets.PermGet, presets.PermList).On("*"),
  73. perm.PolicyFor(perm.Anybody).WhoAre(perm.Denied).ToDo(presets.PermCreate, presets.PermUpdate, presets.PermDelete).On("*:activity_logs:*"),
  74. ),
  75. )
  76. utils.Configure(b)
  77. media_view.Configure(b, db)
  78. ab := activity.New(b, db).SetCreatorContextKey(login.UserKey)
  79. l10nBuilder := l10n.New()
  80. pageBuilder := example.ConfigPageBuilder(db, "/admin/page_builder", ``, b.I18n())
  81. pm := pageBuilder.Configure(b, db, l10nBuilder, ab)
  82. tm := pageBuilder.ConfigTemplate(b, db)
  83. cm := pageBuilder.ConfigCategory(b, db)
  84. ab.RegisterModels(pm, tm, cm)
  85. storage := filesystem.New(PublishDir)
  86. publisher := publish.New(db, storage).WithPageBuilder(pageBuilder)
  87. publish_view.Configure(b, db, ab, publisher, pm)
  88. l10nBuilder.
  89. RegisterLocales("International", "International", "International").
  90. RegisterLocales("China", "China", "China").
  91. GetSupportLocaleCodesFromRequestFunc(func(R *http.Request) []string {
  92. return l10nBuilder.GetSupportLocaleCodes()[:]
  93. })
  94. l10n_view.Configure(b, db, l10nBuilder, ab, pm)
  95. b.I18n().
  96. SupportLanguages(language.English, language.SimplifiedChinese).
  97. RegisterForModule(language.English, I18nExampleKey, Messages_en_US).
  98. RegisterForModule(language.SimplifiedChinese, I18nExampleKey, Messages_zh_CN).
  99. RegisterForModule(language.SimplifiedChinese, presets.ModelsI18nModuleKey, Messages_zh_CN_ModelsI18nModuleKey).
  100. GetSupportLanguagesFromRequestFunc(func(r *http.Request) []language.Tag {
  101. return b.I18n().GetSupportLanguages()
  102. })
  103. b.MenuOrder(
  104. b.MenuGroup("Page Builder").SubItems("pages", "page_templates", "page_categories").Icon("web"),
  105. "shared_containers",
  106. "demo_containers",
  107. "media-library",
  108. )
  109. initMediaLibraryData(db)
  110. initWebsiteData(db)
  111. return Config{
  112. pb: b,
  113. pageBuilder: pageBuilder,
  114. }
  115. }