1 Star 0 Fork 0

xiaojun207 / gin-boot

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README

GIN-BOOT

功能描述

  • http请求入参可以自动填充到结构体,如果是POST,则将http body数据填充到结构体; 如果是GET,则将URL query参数自动填充到结构体;
  • 返回数据,可以是任意数据类型。如果数据不是boot.ApiResp,则返回数据会被包装为boot.ApiResp的json数据;
  • 统一全局异常,请求返回会被包装为系统异常

返回json格式

  {
    "code":"100200",
    "msg":"成功",
    "data":null
  }

使用demo

package main

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/xiaojun207/gin-boot/boot"
	"log"
	"net/http"
)

type Foo struct {
	Username string `json:"username" form:"username" binding:"required"`
	Password string `json:"password" form:"password"`
}

type Page struct {
	boot.BindQuery     // 继承boot.BindQuery的结构体,指定绑定到url参数
	PageNum        int `json:"page_num" form:"page_num"`   // url 中的参数,需要用tag 'form' 标识,才能自动绑定
	PageSize       int `json:"page_size" form:"page_size"` // url 中的参数,需要用tag 'form' 标识,才能自动绑定
}

type QueryHeader struct {
	boot.BindHeader
	Authorization string `header:"authorization"`
}

// /testPost
// 入参可以自动填充到结构体,如果是POST,则将http body数据填充到结构体;
// 返回数据,可以是任意数据类型。如果数据不是boot.ApiResp,则返回数据会被包装为boot.ApiResp的json数据;<br>
// 如果handler执行异常,请求返回会被包装为系统异常
// 参数page继承boot.BindQuery的结构体,绑定url参数
func TestPost1Handler(c *gin.Context, req *Foo, page Page, header QueryHeader) boot.ApiResp {
	log.Println("TestPost1Handler.req:", req.Username, ",Password:", req.Password)
	log.Println("TestPost1Handler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
	log.Println("TestPost1Handler.Authorization:", header.Authorization)
	return boot.ApiResp{Code: "100200", Msg: "Success", Data: "TestData: " + req.Username}
}

// /testGetEmpty
// 空返回值包装测试,返回:{"code":"100200","data":null,"msg":"成功"}
func TestGetEmptyHandler(c *gin.Context, req *Foo) {
	log.Println("TestGetEmptyHandler.req.username:", req.Username, ",password:", req.Password)
}

// 空返回值包装测试,返回:{"code":"100200","data":null,"msg":"成功"}
func TestGetHandler(c *gin.Context, page Page, header QueryHeader) {
	log.Println("TestGetHandler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
	log.Println("TestGetHandler.Authorization:", header.Authorization)
	log.Println("TestGetHandler")
}

// 异常全局处理测试,返回:{"code":"100101","data":null,"msg":"TestPost2Handler.TestError"}
func TestPost2Handler(c *gin.Context, req *Foo) {
	log.Println("TestPost2Handler.req.username:", req.Username, ",password:", req.Password)
	panic(errors.New("TestPost2Handler.TestError"))
}

// /testGet?username=admin12&password=1234&page_num=1&page_size=10
// url 中的参数,需要用tag 'form' 标识
// 也可以使用gin方法, GET类型的请求,query参数也可以自动装填到结构体
func TestGet1Handler(c *gin.Context, req *Foo, page Page) interface{} {
	log.Println("TestGet1Handler.req.username:", req.Username, ",password:", req.Password)
	log.Println("TestGet1Handler.page.PageNum:", page.PageNum, ",PageSize:", page.PageSize)
	data := map[string]interface{}{
		"list": []*Foo{req},
		"page": page,
	}
	log.Println("TestGet1Handler.data:", data)
	return data
}

func AuthInterceptor(c *gin.Context, header http.Header) {
	authorization := c.GetHeader("authorization")
	log.Println("AuthInterceptor.authorization:", authorization)
	if authorization == "" {
		log.Println("AuthInterceptor authorization is null")
		boot.Resp(c, "105101", "账户未登录", "")
		c.Abort()
		return
	}

	//TODO getUid By authorization
	id := 100
	c.Set("uid", id)
}

var webRouter = func(router *boot.WebRouter) {
	//router.Use(AuthInterceptor)

	// 静态资源
	router.StaticFile("/", "./views/index.html")
	router.StaticFS("/static/", http.Dir("./views/static/"))

	// 动态API
	router.POST("/testPost", AuthInterceptor, TestPost1Handler)
	router.POST("/testPost2", TestPost2Handler)
	router.GET("/testGetEmpty", TestGetEmptyHandler)
	router.GET("/testGet", TestGetHandler)
	router.GET("/testGet1", AuthInterceptor, TestGet1Handler)

	apiRouter := router.Group("/api/")
	apiRouter.GET("/test", TestPost2Handler)
}

func main() {
	boot.Start("8088", "/", webRouter)
}

空文件

简介

暂无描述 展开 收起
Go 等 2 种语言
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/xiaojun207/gin-boot.git
git@gitee.com:xiaojun207/gin-boot.git
xiaojun207
gin-boot
gin-boot
master

搜索帮助