GinAdmin management basic platform

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

rely on

Function list

Permission control

Log management

Template page

Automatic paging

Docker deployment

Static resource packaging

Performance monitoring

Working with documents

Start using

  1. git clone address

    git clone https://github.com/gphper/ginadmin.git
  2. Download dependent packages

    go mod download
  3. 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
  4. Run go run main.go to access the address http://localhost: Port address / admin/login. Default account: admin Password: 111111

Docker compose build environment

  1. 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
  2. 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

  1. 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)
  2. Use in html
    {{ .adminUserData.PageHtml }}

journal

  1. system log

    Set the routing middleware to collect system logs and error logs, and set the internal/router/default.go file

  2. 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",
    })
  3. 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

  1. 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{},
        }
    }
  2. 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

  3. 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
  4. For data filling, you need to implement FillData() method in the corresponding directory and execute the following command

    ginadmin-cli db seed
  5. 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

  1. 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"`
    }
  2. Add configuration information in configs/config.ini

    [base]
    port=:8091
  3. 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

Keywords: Go gin

Added by metin on Sat, 11 Sep 2021 21:11:51 +0300