The project is a background management platform based on gin framework. Although many people think that go is used to develop high-performance server-side projects, it is inevitable that there is a need to do web management side. You can't use other languages to develop it. Therefore, the ginadmin project has been integrated. Please give more comments and corrections!
- golang > 1.8
- Gin
- BootStrap
- LayUi
- WebUpload
- Light Year Admin Using Iframe
✅Rbac
✅Logging
✅Template
✅Paginate
✅Docker deployment
✅Static resource packaging
✅Performance monitoring
- http://122.152.196.83/admin/login Account:admin Password: 111111
-
Clone the project
git clone https://github.com/gphper/ginadmin.git
-
Download mod
go mod download
-
Config
configs/config.yaml
[mysql] username=root password=123456 database=db_beego host=127.0.0.1 port=3306 max_open_conn=50 max_idle_conn=20 [redis] addr=localhost:6379 db=0 password="" [session] session_name=gosession_id [base] port=:8091
-
Run
go run main.go
Visit http://localhost:port/admin/login。Account:admin Password:111111
-
Replace
configs/config.yaml
[mysql] username=docker password=123456 database=docker_mysql host=localmysql port=3306 max_open_conn=50 max_idle_conn=20 [session] session_name=gosession_id [base] host=0.0.0.0 port=20010 fill_data=true
-
Run
docker-compose up
|--api // Api Controllers
|--build
|--cmd
|--configs // Configration
|--deployments // docker-compose deployment
|--internal
|--logs // Logging
|--pkg // Common method
|--web //Static resources
- Use
pkg/comment/util.go
PageOperation
adminDb := models.Db.Table("admin_users").Select("nickname","username").Where("uid != ?", 1) adminUserData := comment.PageOperation(c, adminDb, 1, &adminUserList)
- Use in template
{{ .adminUserData.PageHtml }}
-
System Log
Set the routing middleware to collect system logs and error logs.
internal/router/default.go
-
Custom Log
Use
loggers.LogInfo()
ingithub.com/gphper/ginadmin/pkg/loggers
loggers.LogInfo("admin", "this is a info message", map[string]string{ "user_info": "this is a user info", })
-
Switching storage media
-
System Log in
internal/router/default.go
switch storage media -
Custom Log in
loggers.LogInfo
method usefacade.NewZaplog
andfacade.NewRedislog
switch storage media
-
🔹ORM
-
Models must define
TableName() string
method, write the pointer implementing the structure into theGetModels
method.func GetModels() []interface{} { return []interface{}{ &AdminUsers{}, &AdminGroup{}, } }
-
Model needs to inherit
BaseModle
and implementTableName
method. If it needs to initialize and fill data, it needs to implementFillData()
method and write the code to be executed for data filling into the function body. Please refer toAdminUsers
for details. -
You can set
full_ Data
andmigrate_ table
in the ini configuration file to control whether the data table and data are automatically migrated and seeded when the program is restarted.
- In
pkg/cron/cron.go
add timed task.
-
First, in
configs/config.go
add struct type of configuration item,eg:type AppConf struct { BaseConf `ini:"base"` } type BaseConf struct { Port string `ini:"port"` }
-
Second, in
configs/config.yaml
add configuration information[base] port=:8091
-
Using configuration information in code.
configs.App.BaseConf.Port
- 所有的后台模板都写到
web/views/template
目录下面,并且分目录存储,调用时按照目录/模板名称
的方式调用
🔹Rbac
-
In
internal/menu/menu.go
define permission name,then edit permissions in user group management. -
casbin版集成了casbin权限管理框架,官方地址:casbin
-
框架中的常用方法定义在
comment/auth/casbinauth/asbin.go
文件中 -
在控制器中可用从
gin.context
获取登录用户信息info,_ := c.Get("userInfo")
-
template 中判断权限的函数
judgeContainPriv
定义在pkg/template/default.go
文件下"judgeContainPriv": func(username string, obj string, act string) bool { if username == "admin" { return true } ok, err := casbinauth.Check(username, obj, act) if !ok || err != nil { return false } return true },
-
Use swagg generate api doc.
swag init -g cmd/ginadmin/main.go
-
On root path run
go build .\cmd\ginadmin\
,then visit http://localhost:20010/swagger/index.html
- Generate online version
go build -tags=release .\cmd\ginadmin
- Packaged static resource deployment
go build -tags=embed .\cmd\ginadmin
- Prometheus and grafana is recommended for performance monitoring,refer to https://github.com/gphper/ginmonitor