main.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package main
  2. import (
  3. "io/ioutil"
  4. "net/http"
  5. _ "github.com/GoAdminGroup/go-admin/adapter/gin" // adapter
  6. _ "github.com/GoAdminGroup/go-admin/modules/db/drivers/sqlite" // sql driver
  7. _ "github.com/GoAdminGroup/themes/adminlte" // theme
  8. "github.com/GoAdminGroup/go-admin/engine"
  9. "github.com/GoAdminGroup/go-admin/examples/datamodel"
  10. "github.com/GoAdminGroup/go-admin/plugins/admin"
  11. "github.com/GoAdminGroup/go-admin/plugins/example"
  12. "github.com/GoAdminGroup/go-admin/template"
  13. "github.com/GoAdminGroup/go-admin/template/chartjs"
  14. "github.com/GoAdminGroup/go-admin/template/types"
  15. "github.com/gin-gonic/gin"
  16. )
  17. func main() {
  18. r := gin.Default()
  19. gin.SetMode(gin.ReleaseMode)
  20. gin.DefaultWriter = ioutil.Discard
  21. eng := engine.Default()
  22. adminPlugin := admin.NewAdmin(datamodel.Generators)
  23. // add generator, first parameter is the url prefix of table when visit.
  24. // example:
  25. //
  26. // "user" => http://localhost:9033/admin/info/user
  27. //
  28. adminPlugin.AddGenerator("user", datamodel.GetUserTable)
  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.POST("/admin/popup", func(ctx *gin.Context) {
  72. user, ok := eng.User(ctx)
  73. if !ok {
  74. ctx.JSON(http.StatusOK, gin.H{
  75. "code": 401,
  76. "msg": "auth fail",
  77. })
  78. return
  79. }
  80. if !user.CheckPermission("*") {
  81. //ctx.JSON(http.StatusOK, gin.H{
  82. // "code": 401,
  83. // "msg": "没有权限",
  84. //})
  85. //return
  86. }
  87. ctx.JSON(http.StatusOK, gin.H{
  88. "code": 0,
  89. "data": "<h2>hello world</h2>",
  90. })
  91. })
  92. _ = r.Run(":9033")
  93. }