RBAC permission API management service based on golang (including automatic generation of curd code)

Time:2021-10-2

Source codegithub.com/wuyan94zl/GoApiServer
experiencegorbacui.wuyan94zl.cn

use

Directly pull code from workspace
git clone https://github.com/wuyan94zl/GoApiServer

start

First put.env.exampleChange to.envAnd configure the database connection, and other information will be modified according to the situation

Start servicego run main.goYou will see the following route

RBAC permission API management service based on golang (including automatic generation of curd code)

Can see/api/adminThe user interface at the beginning is not added, deleted, modified or queried. Use the following command to build

1. Create curd controller for admin

go run wuyan.go admin
#By default, the curd.go file is created in the app / Controllers / Admin folder
#Change to go run wuyan.go admin. The web will be in the app / Controllers / Web / Admin folder
#The web parameter can accept Web / console app / Controllers / Web / console / Admin

2. Create curd controller routes for admin

go run wuyan.go admin route permission
#Go run wuyan.go admin route followed by the route file name in the folder routes is added to api.go by default
#Api.go does not require authenticated route access
#Auth.go requires authenticated route access
#Permission.go requires authenticated and authorized route access

3. Run the service again at this timego run main.go

RBAC permission API management service based on golang (including automatic generation of curd code)
API functions such as adding, deleting, modifying and querying have been added above

Demonstrate the steps to implement all functions of the API

  • Step 1:git clone https://github.com/wuyan94zl/GoApiServer
  • Step 2:go run wuyan.go adminandgo run wuyan.go admin route permission
  • Step 3:go run main.go
  • The above operation is to demonstrate all the functions of the API in the address

Using the build curd tool

Requirements: add article management function
1. Create data table model

type Article struct {
    Id          uint64 `gorm:"column:id;primaryKey;autoIncrement;not null"json:"id"`
    Title       string `validate:"required,min:10,max:50"search:"like"json:"title"`
    Description string `validate:"required,min:10,max:200"json:"description"`
    Content     string `validate:"required"json:"content"`
    View        uint64
    AdminId     uint64      `validate:"required,numeric"json:"admin_id"`
    Admin       admin.Admin `gorm:"-"relationship:"belongTo"json:"admin"`
    CreatedAt   time.Time   `gorm:"column:created_at;index"json:"created_at"`
    UpdatedAt   time.Time   `gorm:"column:updated_at"json:"updated_at"`
}
//The validate date tag controls the parameter validation of create and update. See github.com/thedevsaddam/govalidator for details of the validate date parameter
//When created above, title and description are required and the length is 10-50 and 10-200
//admin_ ID is required and numeric

//The search tag controls the query method of the paging list (currently supported: like, =, >, <,! =. Like is STR%)
//As above, the paging list interface receives the title keyword like query

//Relationship is association. When querying above, admin user information will be associated

2、bootstrap/auto_migrate.go initIn functionMigrateStructMap data addition model

MigrateStruct["article"] = Article{}

3. Executego run wuyan.go articleandgo run wuyan.go article route permission

After the above operations, five interfaces for adding, deleting, modifying, detailed and paging data of articles will be added
The fields in the added and modified interfaces are verified as:
Title: required, length between 10 and 50
Description: required, length between 10 and 200
Content: required
admin_ ID: required; must be a number
In detailed and paging data:
The data will be associated with the query of admin information

This work adoptsCC agreement, reprint must indicate the author and the link to this article