From 0817fa0ab8acb052e6267af45132a972afd246c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=BE=B7=E6=BB=A1?= Date: Fri, 26 Mar 2021 14:52:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0api=E6=B3=A8=E5=86=8C(?= =?UTF-8?q?=E6=8C=89=E7=85=A7=E6=8E=A5=E5=8F=A3=E7=BA=A6=E6=9D=9F=E7=9A=84?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/api/api.go | 26 +++++++++++++++++++++ gin/api/register.go | 56 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 gin/api/api.go create mode 100644 gin/api/register.go diff --git a/gin/api/api.go b/gin/api/api.go new file mode 100644 index 0000000..7a50bea --- /dev/null +++ b/gin/api/api.go @@ -0,0 +1,26 @@ +// Package gin ... +// +// Description : 便捷的相关API处理 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-26 2:06 下午 +package api + +import "github.com/gin-gonic/gin" + +// IApi 每一个接口的实现约束 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2:08 下午 2021/3/26 +type IApi interface { + // GetMethod 接口请求方法 + GetMethod() string + // GetURI 接口URI + GetURI() string + // GetMiddleWareList 使用的中间件列表 + GetMiddleWareList() []gin.HandlerFunc + // 处理的handler + GetHandler() gin.HandlerFunc +} diff --git a/gin/api/register.go b/gin/api/register.go new file mode 100644 index 0000000..766665a --- /dev/null +++ b/gin/api/register.go @@ -0,0 +1,56 @@ +// Package api ... +// +// Description : 注册路由 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2021-03-26 2:13 下午 +package api + +import ( + "log" + + "github.com/gin-gonic/gin" + "github.com/go-developer/gopkg/gin/util" +) + +var ( + // DebugLogEnable 默认打开debug日志 + DebugLogEnable = true +) + +// DisableDebugLog 禁用debug日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2:17 下午 2021/3/26 +func DisableDebugLog() { + DebugLogEnable = false +} + +// RegisterRouter 注册一个路由 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2:14 下午 2021/3/26 +func RegisterRouter(router *gin.Engine, apiInstanceList []IApi) error { + for _, apiInstance := range apiInstanceList { + if err := util.RegisterRouter(router, apiInstance.GetMethod(), apiInstance.GetURI(), apiInstance.GetHandler(), apiInstance.GetMiddleWareList()); nil != err { + routerLog(err) + panic(err.Error()) + } + } + return nil +} + +// routerLog 记录日志 +// +// Author : go_developer@163.com<张德满> +// +// Date : 2:28 下午 2021/3/26 +func routerLog(err error) { + if !DebugLogEnable || nil == err { + return + } + log.Fatal(err.Error()) +}