swagger

package
v1.10.3 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 8 Imported by: 0

README

swagger

gin swagger library.


Example of use

package main

import (
    "net/http"

    "github.com/zhufuyi/sponge/pkg/gin/validator"

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

func main() {
	r := gin.Default()
	binding.Validator = validator.Init()
	
	r.Run(":8080")
}

type createUserRequest struct {
	Name  string `json:"name" form:"name" binding:"required"`
	Password string `json:"password" form:"password" binding:"required"`
	Age   int    `json:"age" form:"age" binding:"gte=0,lte=120"`
	Email string `json:"email" form:"email" binding:"email"`
}

func CreateUser(c *gin.Context) {
	form := &createUserRequest{}
	err := c.ShouldBindJSON(form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		return
	}
	c.JSON(http.StatusOK, gin.H{"msg": "ok"})
}

type getUserRequest struct {
	Page int    `json:"page" form:"page" binding:"gte=0"`
	Limit int    `json:"limit" form:"limit" binding:"gte=1"`
	Sort string `json:"sort" form:"sort" binding:"-"`
}

func GetUsers(c *gin.Context) {
	form := &getUserRequest{}
	err := c.ShouldBindQuery(form)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
		return
	}

	users, err := getUsers(form)
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
        return
    }

	c.JSON(http.StatusOK, gin.H{"users": users})
}

func getUsers(req *getUserRequest) ([]User,error){}

Documentation

Overview

Package swagger is gin swagger library.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRouter

func CustomRouter(r *gin.Engine, name string, jsonContent []byte)

CustomRouter custom swagger routing, request url is http://<ip:port>/<name>/swagger/index.html

func CustomRouterByFile

func CustomRouterByFile(r *gin.Engine, jsonFile string)

CustomRouterByFile custom swagger router from file, request url is http://<ip:port>/<filename prefix>/swagger/index.html

func DefaultRouter

func DefaultRouter(r *gin.Engine, jsonContent []byte)

DefaultRouter default swagger router, request url is http://<ip:port>/swagger/index.html

func DefaultRouterByFile

func DefaultRouterByFile(r *gin.Engine, jsonFile string)

DefaultRouterByFile default swagger router from file, request url is http://<ip:port>/swagger/index.html

Types

This section is empty.

Jump to

Keyboard shortcuts

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