router

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

gin-router

介绍
  1. 快速创建gin路由列表

  2. 使用client处理路由逻辑

安装

`go get -u gitee.com/white-ravens/gin-router

示例
package main

import(
    "gitee.com/white-ravens/gin-router"
    "github.com/gin-gonic/gin"
    "time"
)

var routerList = []*router.RouterItem{
    {
        Path:    "/getFood",
        Method:  "GET",
        Client:  router.JsonClient,
        Handler: handler,
        Middle:  nil
    },
}

type food struct {
    Name string `json:"name"`
    Id   int    `json:"id"`
}

func handler(*gin.Context) (any, any) {
    s := time.Now().Second()

    if s%2 == 0 {
        return &food{"香蕉", 7}, nil
    }

    return nil, router.NewJsonErr(400, "params err")
}

func main() {
    router := router.NewRouter(gin.Default())
    router.AddRouterList(routerList)
    router.Run(":5600")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JsonClient

func JsonClient(handler func(*gin.Context) (any, any)) gin.HandlerFunc

JsonClient 处理json响应的client

Types

type Client

type Client func(handler func(*gin.Context) (any, any)) gin.HandlerFunc

Client 处理handler的客户端。 handler 逻辑处理函数 返回值: (成功的响应内容, 失败的响应内容)。 返回值需要client处理

type Jcr

type Jcr struct {
	Errcode int    `json:"errcode"`
	Msg     string `json:"msg"`
	Data    any    `json:"data"`
}

Jcr JsonClientRes json响应结构体

func NewJsonErr

func NewJsonErr(errcode int, msg string) *Jcr

NewJsonErr 创建失败的Jcr类型响应

type Router

type Router struct {
	Engine     *gin.Engine
	RouterList []*RouterItem
}

Router 路由实例

func NewRouter

func NewRouter(e *gin.Engine) *Router

NewRouter 新建路由实例

func (*Router) AddRouterList

func (r *Router) AddRouterList(list []*RouterItem)

AddRouterList 设置路由列表

func (*Router) Run

func (r *Router) Run(addr string) error

Run 运行服务

type RouterItem

type RouterItem struct {
	Path    string                        // url
	Method  string                        // GET POST PUT DELETE OPTIONS HEAD
	Client  Client                        // 处理Handler的客户端
	Handler func(*gin.Context) (any, any) // 具体的逻辑处理函数
	Middle  []gin.HandlerFunc             // 中间件
}

RouterItem 单个路由

Directories

Path Synopsis
test
main command

Jump to

Keyboard shortcuts

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