Skip to content

Gin 框架

基本路由

go
r := gin.Default()

r.GET("/ping", func(c *gin.Context) {
    c.JSON(200, gin.H{"message": "pong"})
})

r.Run(":8080")

路由分组

go
api := r.Group("/api/v1")
{
    api.GET("/users", getUsers)
    api.POST("/users", createUser)
    api.PUT("/users/:id", updateUser)
    api.DELETE("/users/:id", deleteUser)
}

中间件

go
func AuthMiddleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        token := c.GetHeader("Authorization")
        if token == "" {
            c.AbortWithStatusJSON(401, gin.H{"error": "未授权"})
            return
        }
        c.Next()
    }
}

基于 VitePress 构建