Golang Gin 框架 Route备注
路由
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 main
import (
"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: <About><Title>关于我们</Title></About>
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("<h1>title</h1>"))
// OUT "<h1>title</h1>"
// 格式化输出
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 main
import (
"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")
}