ge

package module
v2.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2024 License: MIT Imports: 7 Imported by: 0

README

gin-ex

介绍

gin 常用方式封装

软件架构

软件架构说明

package ge

import (
	"fmt"
	"io"
	"net/http"
	"strings"
	"testing"
	"time"

	"github.com/gin-gonic/gin"
)

func TestGet(t *testing.T) {
	go func() {
		time.Sleep(3 * time.Second)
		t.Run("testGet", func(t *testing.T) {
			rsp, err := http.Get(`http://localhost:9000/testGet?pageInfo={"pageNo":1,"pageSize":10}&qry={"TmplID":"01001","DepartID":"华北5"}&append="active='1'"`)
			bs, _ := io.ReadAll(rsp.Body)
			fmt.Println(string(bs), err)
		})
	}()
	e := gin.Default()
	e.GET("/testGet", CreateHandler(func(ctx *gin.Context) (data any, data2 any, errRtn error) {
		p, where, append := GetQryParams(ctx)
		data2 = *p
		data = where
		fmt.Println("append", append)
		return
	}))
	e.Run(":9000")
}

func TestPut(t *testing.T) {
	go func() {
		time.Sleep(3 * time.Second)
		t.Run("testPut", func(t *testing.T) {
			rsp, err := http.NewRequest("PUT", `http://localhost:9000/testPut`, strings.NewReader(`{"where": {"TmplID":"01001","DepartID":"华北5"},"set":{"active": "1"}}`))
			bs, _ := io.ReadAll(rsp.Body)
			fmt.Println(string(bs), err)
		})
	}()
	e := gin.Default()
	e.PUT("/testPut", CreateHandler(func(ctx *gin.Context) (data any, data2 any, errRtn error) {
		var append string
		data, data2, append = GetPutParams(ctx)
		fmt.Println("append", append)
		return
	}))
	e.Run(":9000")
}
func TestPost(t *testing.T) {
	go func() {
		time.Sleep(3 * time.Second)
		t.Run("testPost", func(t *testing.T) {
			rsp, err := http.Post(`http://localhost:9000/testPost`, "application/json", strings.NewReader(`{"TmplID":"01001","DepartID":"华北5"}`))
			bs, _ := io.ReadAll(rsp.Body)
			fmt.Println(string(bs), err)
		})
		t.Run("testPosts", func(t *testing.T) {
			rsp, err := http.Post(`http://localhost:9000/testPost`, "application/json", strings.NewReader(`[{"TmplID":"01001","DepartID":"华北5"},{"TmplID":"01002","DepartID":"华北3"}]`))
			bs, _ := io.ReadAll(rsp.Body)
			fmt.Println(string(bs), err)
		})
	}()
	e := gin.Default()
	e.POST("/testPost", CreateHandler(func(ctx *gin.Context) (data any, data2 any, errRtn error) {
		data = GetPostParams(ctx)
		return
	}))
	e.Run(":9000")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Cors

func Cors(ctx *gin.Context)

gin 处理跨域访问

func CreateQryHandler

func CreateQryHandler(h QryHandler) gin.HandlerFunc

CreateQryHandler 查询

用法: r.GET("/users", CreateQryHandler(HandlerForUserList))

@param h
@return gin.HandlerFunc handler 函数

func CreateUpHandler

func CreateUpHandler(h UpdateHandler) gin.HandlerFunc

func Logger

func Logger(c *gin.Context)

func PostGetParams added in v2.0.3

func PostGetParams(ctx *gin.Context) (where map[string]any, current int, pageSize int, sqlAppend string, err error)

PostGetParams Post 方式获取 get 数据

@param ctx
@return where
@return current
@return pageSize
@return sqlAppend
@return err

func PostParams

func PostParams(ctx *gin.Context) (data map[string]any, err error)

PostParams 取gin的post参数(json)

@param ctx 示例:{"TmplID": "01001", "DepartID": "华北5"}
@return data
@return err

func PostParamsList added in v2.0.2

func PostParamsList(ctx *gin.Context) (data []map[string]any, err error)

PostParamsList (多)取gin的post参数(json)

@param ctx
@return data []map[string]any
@return err

func PutParams

func PutParams(ctx *gin.Context) (where map[string]any, set map[string]any, err error)

PutParams 取gin的put参数(json)

@param ctx 示例:{"where": {"Grade": 5, "Age": {">=", 3}}, "set": {"Age": 18}}
@return where 查询参数(以 struct 更新时, 内容为空)
@return set 设置参数(以 struct 更新时, 内容struct)
@return err

func QryParams

func QryParams(ctx *gin.Context) (where map[string]string, current int, pageSize int, sqlAppend string, err error)

QryParams //

@param ctx
@return where
@return current
@return pageSize
@return sqlAppend
@return err

func ResponseLog

func ResponseLog(ctx *gin.Context)

ResponseLog gin响应的日志中间件

@param ctx

Types

type QryHandler

type QryHandler func(ctx *gin.Context) (data any, total int, errRtn error)

QryHandler

@param ctx
@return data
@return total 不传会使用 data 的长度, 如果是分页一定要传
@return errRtn

type Response

type Response struct {
	Success bool `json:"success"`
	Total   int  `json:"total"`
	// 查询的结果集
	Data any `json:"data"`
}

Response 响应体

type UpdateHandler

type UpdateHandler func(ctx *gin.Context) (errRtn error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL