main.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package main
  2. import (
  3. _ "github.com/chenhg5/go-admin/adapter/gin"
  4. "github.com/chenhg5/go-admin/engine"
  5. "github.com/chenhg5/go-admin/examples/datamodel"
  6. "github.com/chenhg5/go-admin/plugins/admin"
  7. "github.com/chenhg5/go-admin/plugins/example"
  8. "github.com/chenhg5/go-admin/template/types"
  9. "github.com/gin-gonic/gin"
  10. "io/ioutil"
  11. )
  12. func main() {
  13. r := gin.Default()
  14. gin.SetMode(gin.ReleaseMode)
  15. gin.DefaultWriter = ioutil.Discard
  16. eng := engine.Default()
  17. adminPlugin := admin.NewAdmin(datamodel.Generators)
  18. // add generator, first parameter is the url prefix of table when visit.
  19. // example:
  20. //
  21. // "user" => http://localhost:9033/admin/info/user
  22. //
  23. adminPlugin.AddGenerator("user", datamodel.GetUserTable)
  24. // customize a plugin
  25. examplePlugin := example.NewExample()
  26. // you can also add config like:
  27. //
  28. // import "github.com/chenhg5/go-admin/modules/config""
  29. //
  30. // cfg := config.Config{
  31. // Databases: config.DatabaseList{
  32. // "default": {
  33. // Host: "127.0.0.1",
  34. // Port: "3306",
  35. // User: "root",
  36. // Pwd: "root",
  37. // Name: "godmin",
  38. // MaxIdleCon: 50,
  39. // MaxOpenCon: 150,
  40. // Driver: db.DriverMysql,
  41. // },
  42. // },
  43. // UrlPrefix: "admin",
  44. // IndexUrl: "/",
  45. // Debug: true,
  46. // Language: language.CN,
  47. // }
  48. //
  49. // eng.AddConfig(cfg)
  50. if err := eng.AddConfigFromJson("./config.json").
  51. AddPlugins(adminPlugin, examplePlugin).
  52. Use(r); err != nil {
  53. panic(err)
  54. }
  55. r.Static("/uploads", "./uploads")
  56. // customize your index pages
  57. r.GET("/admin", func(ctx *gin.Context) {
  58. engine.Content(ctx, func(ctx interface{}) (types.Panel, error) {
  59. return datamodel.GetContent()
  60. })
  61. })
  62. _ = r.Run(":9033")
  63. }