Golang学习日志 ━━ Gin-Vue-Admin按步骤手动创建api及router、service

gin-vue-admin是一套国人用golang开发的后台管理系统,最新版本的系统工具中已经自带自动化package功能,本文记录的是手动创建过程。
官网:https://www.gin-vue-admin.com/
学习视频:https://www.bilibili.com/video/BV1kv4y1g7nT/?p=6

准备工作

server/api/v1server/routerserver/service目录下创建自己的文件夹及文件。
本例目录及文件命名都为myTest/enter.gomyTest/my_test1.go,包名都为myTestPkg
Golang学习日志 ━━ Gin-Vue-Admin按步骤手动创建api及router、service_第1张图片

功能服务

server/service/myTest/enter.go

package myTestPkg

type ServiceGroup struct {
	MyTest1Service
}

server/service/myTest/my_test1.go

package myTestPkg

import (
	"fmt"
)

type MyTest1Service struct {
}

func (s *MyTest1Service) CreateService() {
	fmt.Print("这里仅执行打印")
}

server/service/enter.go

package service

import (
	"github.com/flipped-aurora/gin-vue-admin/server/service/example"
	"github.com/flipped-aurora/gin-vue-admin/server/service/myTest"
	"github.com/flipped-aurora/gin-vue-admin/server/service/system"
)

type ServiceGroup struct {
	SystemServiceGroup  system.ServiceGroup
	ExampleServiceGroup example.ServiceGroup
	MyTestServiceGroup  myTestPkg.ServiceGroup
}

var ServiceGroupApp = new(ServiceGroup)

API

server/api/v1/myTest/enter.go

package myTestPkg

import (
	"github.com/flipped-aurora/gin-vue-admin/server/service"
)

type ApiGroup struct {
	MyTest1Api
}

var (
	myTestService = service.ServiceGroupApp.MyTestServiceGroup.MyTest1Service
)

server/api/v1/myTest/my_test1.go

package myTestPkg

import (
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/response"
	"github.com/gin-gonic/gin"
)

type MyTest1Api struct {
}

func (m *MyTest1Api) CreateApi(c *gin.Context) {
	myTestService.CreateService()

	response.Ok(c)
}

server/api/v1/enter.go

package v1

import (
	"github.com/flipped-aurora/gin-vue-admin/server/api/v1/example"
	"github.com/flipped-aurora/gin-vue-admin/server/api/v1/myTest"
	"github.com/flipped-aurora/gin-vue-admin/server/api/v1/system"
)

type ApiGroup struct {
	SystemApiGroup  system.ApiGroup
	ExampleApiGroup example.ApiGroup
	MyTestApiGroup  myTestPkg.ApiGroup
}

var ApiGroupApp = new(ApiGroup)

路由

server/router/myTest/enter.go

package myTestPkg

type RouterGroup struct {
	MyTest1Router
}

server/router/myTest/my_test1.go

package myTestPkg

import (
	"github.com/flipped-aurora/gin-vue-admin/server/api/v1"
	"github.com/flipped-aurora/gin-vue-admin/server/middleware"
	"github.com/gin-gonic/gin"
)

type MyTest1Router struct {
}

// InitMyTest1Router 初始化 MyTest1 路由信息
func (s *MyTest1Router) InitMyTest1Router(Router *gin.RouterGroup) {
	myTest1Router := Router.Group("myTest1").Use(middleware.OperationRecord())

	var myTest1Api = v1.ApiGroupApp.MyTestApiGroup.MyTest1Api
	{
		myTest1Router.POST("createMyTest1Api", myTest1Api.CreateApi)
	}
}

server/router/enter.go

package router

import (
	"github.com/flipped-aurora/gin-vue-admin/server/router/example"
	"github.com/flipped-aurora/gin-vue-admin/server/router/myTest"
	"github.com/flipped-aurora/gin-vue-admin/server/router/system"
)

type RouterGroup struct {
	System            system.RouterGroup
	Example           example.RouterGroup
	MyTestRouterGroup myTestPkg.RouterGroup
}

var RouterGroupApp = new(RouterGroup)

初始化

server/initialize/router.go

package initialize

import (
	"net/http"

	_ "github.com/flipped-aurora/gin-vue-admin/server/docs"
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/middleware"
	"github.com/flipped-aurora/gin-vue-admin/server/router"
	"github.com/gin-gonic/gin"
	"github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

// 初始化总路由

func Routers() *gin.Engine {
	Router := gin.Default()
	myTestRouter := router.RouterGroupApp.MyTestRouterGroup
	systemRouter := router.RouterGroupApp.System
	exampleRouter := router.RouterGroupApp.Example

	Router.StaticFS(global.GVA_CONFIG.Local.Path, http.Dir(global.GVA_CONFIG.Local.StorePath)) // 为用户头像和文件提供静态地址
	Router.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	global.GVA_LOG.Info("register swagger handler")
	// 方便统一添加路由组前缀 多服务器上线使用

	PublicGroup := Router.Group("")
	{
		// 健康监测
		PublicGroup.GET("/health", func(c *gin.Context) {
			c.JSON(200, "ok")
		})
	}
	{
		systemRouter.InitBaseRouter(PublicGroup) // 注册基础功能路由 不做鉴权
		systemRouter.InitInitRouter(PublicGroup) // 自动初始化相关
	}
	PrivateGroup := Router.Group("")
	PrivateGroup.Use(middleware.JWTAuth()).Use(middleware.CasbinHandler())
	{
		systemRouter.InitApiRouter(PrivateGroup)                 // 注册功能api路由
		systemRouter.InitJwtRouter(PrivateGroup)                 // jwt相关路由
		systemRouter.InitUserRouter(PrivateGroup)                // 注册用户路由
		systemRouter.InitMenuRouter(PrivateGroup)                // 注册menu路由
		systemRouter.InitSystemRouter(PrivateGroup)              // system相关路由
		systemRouter.InitCasbinRouter(PrivateGroup)              // 权限相关路由
		systemRouter.InitAutoCodeRouter(PrivateGroup)            // 创建自动化代码
		systemRouter.InitAuthorityRouter(PrivateGroup)           // 注册角色路由
		systemRouter.InitSysDictionaryRouter(PrivateGroup)       // 字典管理
		systemRouter.InitAutoCodeHistoryRouter(PrivateGroup)     // 自动化代码历史
		systemRouter.InitSysOperationRecordRouter(PrivateGroup)  // 操作记录
		systemRouter.InitSysDictionaryDetailRouter(PrivateGroup) // 字典详情管理
		systemRouter.InitAuthorityBtnRouterRouter(PrivateGroup)  // 字典详情管理

		exampleRouter.InitExcelRouter(PrivateGroup)                 // 表格导入导出
		exampleRouter.InitCustomerRouter(PrivateGroup)              // 客户路由
		exampleRouter.InitFileUploadAndDownloadRouter(PrivateGroup) // 文件上传下载功能路由

		myTestRouter.InitMyTest1Router(PrivateGroup)
	}

	InstallPlugin(Router) // 安装插件

	global.GVA_LOG.Info("router register success")
	return Router
}

总结

代码要求还是很统一的,创建同样的目录文件,放在同一个包下,告诉enter.go有几个组(本文就一个,都是开头用MyTest1命名,比如MyTest1Api),先创建service,再给api调用,然后给router调用,最后initialize初始化路由。

go build main.go

生成执行文件打开后,如图,表示api接口已经对外发布。
Golang学习日志 ━━ Gin-Vue-Admin按步骤手动创建api及router、service_第2张图片

你可能感兴趣的:(Golang,vue.js,golang,学习,gin-vue-admin,admin)