• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Go web框架构建三层架构

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

go-api-framework

go基于gin三层架构web框架

三层架构模式

func RegisterHandler(业务最终函数,怎么取参数,怎么处理业务结果) func(context *gin.Context) {

       xxxxxoooo


}

 



这个就是最终的结果

unc RegisterHandler(业务最终函数,怎么取参数,怎么处理业务结果) func(context *gin.Context) {

        参数:=怎么取参数() 
        业务结果:=业务最终函数(参数)
   
        
        怎么处理业务结果(业务结果)

}

 

首先要定义原型

业务最终函数

       type Endpoint func(ctx context.Context,request interface{}) (response interface{}, err error)


   一律使用interface{}  。这样可以处理不同的类型

怎么取参数 :

      type EncodeRequestFunc func(*gin.Context, interface{}) (interface{}, error)

 
 怎么处理响应:
    
        type DecodeResponseFunc func(*gin.Context,  interface{}) error

然后写成这样

func RegisterHandler(endpoint Endpoint,encodeFunc EncodeRequestFunc,decodeFunc DecodeResponseFunc) func(context *gin.Context){
    return func(context *gin.Context) {
        req,err:=encodeFunc(context,nil)
        if err!=nil{
            context.JSON(500,gin.H{"error":"param err"+err.Error()})
            return
        }
        res,err:=endpoint(context,req)
        if err!=nil{
            context.JSON(500,gin.H{"error":err})
        }else{
            err:=decodeFunc(context,res)
            if err!=nil{
                context.JSON(500,gin.H{"error":err})
            }
        }

    }
}

 

最终核心结构:

package App

import (
    "context"
    "fmt"
    "github.com/gin-gonic/gin"
)

type Middleware func(Endpoint) Endpoint
//业务最终函数原型
type Endpoint func(ctx context.Context,request interface{}) (response interface{}, err error)

//怎么取参数
type EncodeRequestFunc func(*gin.Context) (interface{}, error)

//怎么处理业务结果
type DecodeResponseFunc func(*gin.Context, interface{}) error

func RegisterHandler(endpoint Endpoint,encodeFunc EncodeRequestFunc, decodeFunc DecodeResponseFunc) func(context *gin.Context){
    return func(context *gin.Context) {

        defer func() {
            if r:=recover();r!=nil{
                fmt.Fprintln(gin.DefaultWriter,fmt.Sprintf("fatal error:%s",r))
                context.JSON(500,gin.H{"error":fmt.Sprintf("fatal error:%s",r)})
                return
            }
        }()
        //参数:=怎么取参数(context)
        //业务结果,error:=业务最终函数(context,参数)
        //
        //
        //怎么处理业务结果(业务结果)
        req,err:=encodeFunc(context) //获取参数
        if err!=nil{
            context.JSON(400,gin.H{"error":"param error:"+err.Error()})
            return
        }
        rsp,err:=endpoint(context,req) //执行业务过程
        if err!=nil{
            fmt.Fprintln(gin.DefaultWriter,"response error:",err)
            context.JSON(400,gin.H{"error":"response error:"+err.Error()})
            return
        }
        err=decodeFunc(context,rsp) //处理 业务执行 结果
        if err!=nil{
            context.JSON(500,gin.H{"error":"server error:"+err.Error()})
            return
        }

    }
}

 

 

github地址:https://github.com/sunlongv520/go-api-framework





 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Go语言的Web框架比较发布时间:2022-07-10
下一篇:
谁是最快的Go Web框架发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap