golang gin 中间件,返回结果

package main

import (
    "net/http"
    "github.com/gin-gonic/gin"
)

func response() gin.HandlerFunc {
    return func(c *gin.Context) {
        c.Next()
        if c.Writer.Written() {
            return
        }

        params := c.Keys
        if len(params) == 0 {
            return
        }
        c.JSON(http.StatusOK, params)
    }
}

func main() {
    r := gin.Default()
    r.Use(response())

    r.GET("/ping", func(c *gin.Context) {
        c.String(http.StatusOK, "PONG")
    })

    r.GET("/status", func(c *gin.Context) {
        c.Status(http.StatusOK)
    })

    r.GET("/hello", func(c *gin.Context) {
        c.Set("message", "hello, world")
    })

    r.Run()
}

  

你可能感兴趣的:(golang,json)