GinAdmin
This 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! Welcome, star ⭐⭐
Demo address
- 122.152.196.83/admin/login Account No.: admin Password: 111111
rely on
- golang > 1.8
- Gin
- BootStrap
- LayUi
- WebUpload
- Light Year Admin Using Iframe
Function list
Permission control
Log management
Template page
Automatic paging
Docker deployment
Static resource packaging
Performance monitoring
Working with documents
- Demo address
- Start using
- Docker compose build environment
- Project directory
- paging
- journal
- database
- Timed task
- configuration file
- Template page
- User rights
- API documentation
- Online deployment
- Performance monitoring
Start using
git clone address
git clone https://github.com/gphper/ginadmin.git
Download dependent packages
go mod download
Configure the configs/config.ini file
[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 to access the address http://localhost: Port address / admin/login. Default account: admin Password: 111111
Docker compose build environment
Replace the configuration item in the conf directory
[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
Execute the command docker compose up
Project directory
|--api // Api interface controller |--build // Encapsulated public method |--cmd // Command line tools |--configs // configuration file |--deployments // Docker compose deployment file |--internal //Core code |--logs // Log storage directory |--pkg // Public call part |--web //View static file
paging
- Use PageOperation IN pkg/comment/util.go for paging
adminDb := models.Db.Table("admin_users").Select("nickname","username").Where("uid != ?", 1) adminUserData := comment.PageOperation(c, adminDb, 1, &adminUserList)
- Use in html
{{ .adminUserData.PageHtml }}
journal
system log
Set the routing middleware to collect system logs and error logs, and set the internal/router/default.go file
Custom log
Use the loggers.LogInfo() method to log github/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
The system log is in the internal/router/default.go file. Change the middleware and switch the log storage medium
In the loggers.LogInfo method, switch between facade.NewZaplog and facade.NewRedislog
database
All files defined under models need to implement the TableName() string method, and write the pointer implementing the structure into the GetModels method
func GetModels() []interface{} { return []interface{}{ &AdminUsers{}, &AdminGroup{}, } }
model needs to inherit basemode and implement TableName method. If it needs to initialize and fill data, it needs to implement FillData() method and write the code to be executed for data filling into the function body. Please refer to AdminUsers for details
For database migration, first use go install CMD \ ginadmin cli to install the ginadmin cli command and execute the command line tool
ginadmin-cli db migrate
For data filling, you need to implement FillData() method in the corresponding directory and execute the following command
ginadmin-cli db seed
You can set fill in the ini configuration file_ Data and migrate_table controls the automatic migration of data tables and filling of data when the program is restarted
Timed task
- Add scheduled execution tasks in pkg/cron/cron.go
configuration file
Now configure / config.go adds the struct type of the configuration item, for example
type AppConf struct { BaseConf `ini:"base"` } type BaseConf struct { Port string `ini:"port"` }
Add configuration information in configs/config.ini
[base] port=:8091
Call the configuration file information in code.
configs.App.BaseConf.Port
Template page
- All background templates are written to the web/views/template directory and stored in different directories. When calling, they are called according to the directory / template name
User rights
Menu permissions are defined in the internal/menu/menu.go file. After definition, edit permissions in user group management
Casbin version integrates casbin permission management framework. Official address: casbin
Common methods in the framework are defined in the comment/auth/casbinauth/asbin.go file
In the controller, the login user information can be obtained from gin.context
info,_ := c.Get("userInfo")
The judgeContainPriv function for judging permissions in the template is defined in the pkg/template/default.go file
"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 },
API documentation
Use swagg to generate api documents and generate files in the docs directory
swag init -g cmd/ginadmin/main.go
Execute go build. \ CMD \ ginadmin \ in the root directory, and then access localhost:20010/swagger/index.html
Online deployment
- Use go build - tags = release. \ CMD \ ginadmin to generate binaries
- Package static resource deployment go build - tags = embedded. \ CMD \ ginadmin
Performance monitoring
- It is recommended to use prometheus + grafana for performance monitoring. Refer to the example github.com/gphper/ginmonitor