ITPub博客

首页 > IT职业 > IT生活 > Golang Gin 框架 Route备注

Golang Gin 框架 Route备注

IT生活 作者:bigstarr 时间:2021-09-09 09:35:50 0 删除 编辑

路由

package main

import (    "github.com/gin-gonic/gin"
    "time"
    "log")

func main()  {    // 初始化
    router := gin.New();    // 常规操作
    router.POST("/", func(context *gin.Context) {
        context.JSON(200,"POST")
    })
    router.GET("/", func(context *gin.Context) {
        context.JSON(200,"GET")
    })
    router.DELETE("/", func(context *gin.Context) {
        context.JSON(200,"DELETE")
    })
    router.PATCH("/", func(context *gin.Context) {
        context.JSON(200,"PATCH")
    })
    router.PUT("/", func(context *gin.Context) {
        context.JSON(200,"PUT")
    })
    router.OPTIONS("/", func(context *gin.Context) {
        context.JSON(200,"OPTIONS")
    })
    router.HEAD("/", func(context *gin.Context) {
        context.JSON(200,"HEAD")
    })    // 多方式匹配
    // 包括 GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE
    router.Any("/", func(context *gin.Context) {
        context.JSON(200,"Any")
    })    // 参数 GET, POST, PUT, PATCH, DELETE
    router.Handle("GET","/", func(context *gin.Context) {
        context.JSON(200,"GET")
    })    // 未知路由处理
    router.NoRoute(func(context *gin.Context) {
        context.String(404,"Not router")
    })    // 未知调用方式
    router.NoMethod(func(context *gin.Context) {
        context.String(404,"Not method")
    })    
    // 静态资源返回
    router.StaticFile("favicon.ico", "./resources/favicon.ico")    // 静态资源目录
    router.Static("/css", "/var/www/css")    // GROUP
    // 接口版本前缀
    group := router.Group("/v1")
    group.GET("user/info", func(context *gin.Context) {
        context.String(200,"username")
    })    // Middleware
    middleware := router.Group("", func(context *gin.Context) {
        name := context.Query("name")        if(name=="admin"){
            context.Redirect(302,"/home")
        }
        context.String(403,"Unauthorized")
    })
    middleware.GET("/home", func(context *gin.Context) {
        context.String(200,"Api home")
    })    // 全局监控
    loger := router.Use(func(context *gin.Context) {
        t := time.Now()
        context.Set("username", "admin")        // before request

        context.Next()        // after request
        latency := time.Since(t)
        log.Print(latency)        // access the status we are sending
        status := context.Writer.Status()
        log.Println(status)
    })
    loger.GET("/update", func(context *gin.Context) {
        context.String(200,"Success")
    })    // 启动服务
    router.Run(":8080")
}

输出

package mainimport (    "github.com/gin-gonic/gin")func main()  {    // 初始化
    router := gin.New();
    router.GET("about", func(context *gin.Context) {
        type About struct {
            Title string
        }
        about := About{Title:"关于我们"}
        context.JSON(200,about)        // OUT: {"Title":"关于我们"}
        context.XML(200,about)        // OUT: 关于我们
        context.YAML(200,about)        // Download YAML

                // 模板路径
        router.LoadHTMLGlob("tmp/*")                // 渲染模板 g.H map[string] 变量带入
        context.HTML(200,"about.html",gin.H{            "title": "Posts",
        })        // OUT about.html

        context.String(200,"关于我们%s","!")        // OUT "关于我们!"
        // 跳转
        context.Redirect(302,"/home")        // 指定ContentType
        context.Data(200,"text/html;charset=utf-8",[]byte("

title

"))        // OUT "

title

"         // 格式化输出         context.IndentedJSON(200,about)        /* OUT:         {             "Title": "关于我们"         }         */     })    // 启动服务     router.Run(":8080") }

变量

curl -X POST -i -d "type=3"  "/admin/head?id=1&type=2"
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Thu, 19 Apr 2018 13:02:31 GMT
Content-Length: 43

OUT: path param name:admin attr:/head id:1 type:3%

package mainimport (    "github.com/gin-gonic/gin")func main()  {    // 初始化
    router := gin.New();
    router.POST("/:name/*attr", func(context *gin.Context) {
        context.String(200,            "path param name:%s attr:%s id:%s type:%s",
            context.Param("name"),
            context.Param("attr"),
            context.Query("id"),
            context.PostForm("type"))
    })    // 启动服务
    router.Run(":8080")
}



作者:保儿洁
链接:https://www.jianshu.com/p/d4b52187d233

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3330/viewspace-2805026/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 无人驾驶车
请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    83
  • 访问量
    1109