ge

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2024 License: MIT Imports: 8 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 added in v0.0.7

func Cors(ctx *gin.Context)

gin 处理跨域访问

func CreateHandler

func CreateHandler(h Handler) gin.HandlerFunc

CreateHandler handler 工厂

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

@param Handler handler 业务处理函数
@return gin.HandlerFunc handler 函数

func GetPostParams

func GetPostParams(ctx *gin.Context) (data []map[string]any)

GetPostParams 取gin的post参数(json)

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

func GetPutParams

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

GetPutParams 取gin的put参数(json)

@param ctx 示例:{"where": {"Grade": 5, "Age": {">=", 3}}, "set": {"Age": 18}}
@return where 查询参数
@return set 设置参数

func Logger added in v0.0.7

func Logger(c *gin.Context)

func PostParams added in v0.0.7

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

PostParams 取gin的post参数(json)

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

func PutParams added in v0.0.7

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)

func QryParams added in v0.0.7

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

QryParams

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

func ResponseLog added in v0.0.7

func ResponseLog(ctx *gin.Context)

ResponseLog gin响应的日志中间件

@param ctx

Types

type Handler

type Handler func(ctx *gin.Context) (data any, data2 any, errRtn error)

type PageInfo

type PageInfo struct {
	PageNo     int `json:"pageNo,omitempty"`
	PageSize   int `json:"pageSize,omitempty"`
	TotalCount int `json:"totalCount,omitempty"`
}

PageInfo 分页数据

func GetQryParams

func GetQryParams(ctx *gin.Context) (p *PageInfo, where map[string]any, sqlAppend string)

GetQryParams 取gin的请求参数(query, web不支持body)

@param ctx 示例: ?pageInfo={"pageNo":1,"pageSize":10}&qry={"TmplID":"01001","DepartID":"华北5"}&append=active='1'
@return p *PageInfo 分页
@return where 查询参数
@return sqlAppend 追加(order group....)

type Response

type Response struct {
	Status string `json:"status"` // success 或 failure
	Msg    string `json:"msg"`
	// 查询的结果集, put/post 行数
	Data any `json:"data"`
	// GetObjects 函数返回 PageInfo
	Data2 any `json:"data2"`
}

Response 响应体

Jump to

Keyboard shortcuts

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