main.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package main
  2. import (
  3. _ "github.com/GoAdminGroup/go-admin/adapter/gin" // adapter
  4. _ "github.com/GoAdminGroup/go-admin/modules/db/drivers/sqlite" // sql driver
  5. _ "github.com/GoAdminGroup/themes/adminlte" // theme
  6. "io/ioutil"
  7. "github.com/GoAdminGroup/example/tables"
  8. "github.com/GoAdminGroup/go-admin/engine"
  9. "github.com/GoAdminGroup/go-admin/plugins/admin"
  10. "github.com/GoAdminGroup/go-admin/plugins/example"
  11. "github.com/GoAdminGroup/go-admin/template"
  12. "github.com/GoAdminGroup/go-admin/template/chartjs"
  13. "github.com/GoAdminGroup/go-admin/template/types"
  14. "github.com/gin-gonic/gin"
  15. )
  16. func main() {
  17. r := gin.Default()
  18. gin.SetMode(gin.ReleaseMode)
  19. gin.DefaultWriter = ioutil.Discard
  20. eng := engine.Default()
  21. adminPlugin := admin.NewAdmin(tables.Generators)
  22. // add generator, first parameter is the url prefix of table when visit.
  23. // example:
  24. //
  25. // "user" => http://localhost:9033/admin/info/user
  26. //
  27. adminPlugin.AddGenerator("user", tables.GetUserTable)
  28. adminPlugin.AddGenerator("external", tables.GetExternalTable)
  29. // customize a plugin
  30. // 自己定制一个插件👇
  31. examplePlugin := example.NewExample()
  32. template.AddComp(chartjs.NewChart())
  33. // you can also add config like:
  34. // 您也可以像下面这样的方式去引入数据库👇
  35. //
  36. // import "github.com/GoAdminGroup/go-admin/modules/config"
  37. //
  38. // cfg := config.Config{
  39. // Databases: config.DatabaseList{
  40. // "default": {
  41. // Host: "127.0.0.1",
  42. // Port: "3306",
  43. // User: "root",
  44. // Pwd: "root",
  45. // Name: "godmin",
  46. // MaxIdleCon: 50,
  47. // MaxOpenCon: 150,
  48. // Driver: db.DriverMysql,
  49. // },
  50. // },
  51. // UrlPrefix: "admin",
  52. // IndexUrl: "/",
  53. // Debug: true,
  54. // Language: language.CN,
  55. // }
  56. //
  57. // eng.AddConfig(cfg)
  58. if err := eng.AddConfigFromJSON("./config.json").
  59. AddPlugins(adminPlugin, examplePlugin).
  60. Use(r); err != nil {
  61. panic(err)
  62. }
  63. r.Static("/uploads", "./uploads")
  64. // customize your index pages
  65. // 下面这样定制您的首页👇
  66. r.GET("/admin", func(ctx *gin.Context) {
  67. engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
  68. return DashboardPage()
  69. })
  70. })
  71. _ = r.Run(":9033")
  72. }