zoukankan      html  css  js  c++  java
  • gin使用中间件

    原文:https://www.cnblogs.com/jachin/p/9966839.html

    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()
    }
    

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    ➜  src  go run main.go
    [GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
     
    [GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
     - using env:   export GIN_MODE=release
     - using code:  gin.SetMode(gin.ReleaseMode)
     
    [GIN-debug] GET    /ping                     --> main.main.func1 (4 handlers)
    [GIN-debug] GET    /status                   --> main.main.func2 (4 handlers)
    [GIN-debug] GET    /hello                    --> main.main.func3 (4 handlers)
    [GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
    [GIN-debug] Listening and serving HTTP on :8080
    [GIN] 2018/11/15 - 23:23:23 | 200 |     128.732µs |       127.0.0.1 | GET      /ping
    [GIN] 2018/11/15 - 23:23:26 | 200 |       1.764µs |       127.0.0.1 | GET      /status
    [GIN] 2018/11/15 - 23:23:31 | 200 |      135.48µs |       127.0.0.1 | GET      /hello

      

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    ➜  ~  http http://127.0.0.1:8080/ping
    HTTP/1.1 200 OK
    Content-Length: 4
    Content-Type: text/plain; charset=utf-8
    Date: Thu, 15 Nov 2018 15:23:23 GMT
     
    PONG
     
    ➜  ~  http http://127.0.0.1:8080/status
    HTTP/1.1 200 OK
    Content-Length: 0
    Date: Thu, 15 Nov 2018 15:23:26 GMT
     
     
     
    ➜  ~  http http://127.0.0.1:8080/hello
    HTTP/1.1 200 OK
    Content-Length: 26
    Content-Type: application/json; charset=utf-8
    Date: Thu, 15 Nov 2018 15:23:31 GMT
     
    {
        "message""hello, world"
    }

      

  • 相关阅读:
    如何入门深度学习
    机器学习之激活函数
    轻量化模型之SqueezeNet
    聚类算法之MeanShift
    目标检测之RefineDet
    语义分割之RefineNet
    数学基础之高斯核函数
    目标检测之人脸识别
    梯度下降算法及优化方法
    机器学习高阶训练营知识点一览
  • 原文地址:https://www.cnblogs.com/oxspirt/p/15388469.html
Copyright © 2011-2022 走看看