controller/blog.go
package controller
import (
"bili/model"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
)
func Login(c *gin.Context) {
var login model.AccountLogin
err := c.ShouldBind(&login)
if err != nil {
// fmt.Println(err.Error())
c.JSON(http.StatusOK, gin.H{
"msg": err.Error(),
})
return
}
c.JSON(http.StatusOK, gin.H{
"msg": fmt.Sprintf("%s-%s", login.AccountName, login.Password),
})
}
func FileAll(c *gin.Context) {
all := c.Param("all")
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": all,
})
}
func List(c *gin.Context) {
title := c.DefaultQuery("title", "")
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": []model.Blog{
{
Id: 1,
Title: title,
Content: "郭德纲的笔记",
},
},
})
}
func Info(c *gin.Context) {
id := c.Param("id")
idInt, _ := strconv.Atoi(id)
if id == "" {
c.JSON(http.StatusBadRequest, gin.H{
"code": -1,
"data": "",
})
} else {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": model.Blog{
Id: uint64(idInt),
Title: "过得刚好",
Content: "郭德纲的笔记",
},
})
}
}
func Add(c *gin.Context) {
var p model.Blog
err := c.ShouldBindUri(&p)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": -1,
"data": "输入有误",
})
} else {
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": p,
})
}
}
func Edit(c *gin.Context) {
title := c.DefaultPostForm("title", "")
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": title,
})
}
func EditJson(c *gin.Context) {
var p model.Blog
err := c.ShouldBindJSON(&p)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"code": -1,
"data": "输入有误",
})
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"data": p,
})
}
model/blog.go
package model
type Blog struct {
Id uint64 `uri:"id" json:"id" binding:required`
Title string `uri:"title" json:"title" binding:required`
Content string `uri:"content" json:"content" binding:required`
}
type AccountLogin struct {
AccountName string `json:"accountName" binding:"required,min=8,max=32"`
Password string `json:"password" binding:"required"`
}
main.go
package main
import (
"bili/controller"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
)
func Buy() {
fmt.Println("去买菜")
time.Sleep(1 * time.Second)
fmt.Println("买菜回来")
}
func Cook() {
fmt.Println("去做饭")
time.Sleep(2 * time.Second)
fmt.Println("做好饭了")
}
func Eat() {
fmt.Println("去吃饭")
time.Sleep(1 * time.Second)
fmt.Println("吃完饭了")
}
func Wash() {
fmt.Println("去洗碗")
time.Sleep(2 * time.Second)
fmt.Println("洗完碗了")
}
func CustomMiddleWare(c *gin.Context) {
now := time.Now()
c.Next()
expired := time.Since(now)
fmt.Println(expired)
}
func CustomMiddleWare2() gin.HandlerFunc {
return func(c *gin.Context) {
now := time.Now()
c.Next()
expired := time.Since(now)
fmt.Printf("耗时:%v\n", expired)
}
}
func CustomMiddleWare3(c *gin.Context) {
now := time.Now()
name := c.DefaultQuery("name", "")
if name == "" {
// 后面控制器方法依然执行
// return
// 后面控制器犯法不执行
c.Abort()
}
fmt.Println(name)
c.Next()
expired := time.Since(now)
fmt.Println(expired)
}
func main() {
r := gin.Default()
r.Use(CustomMiddleWare3)
// r.Use(CustomMiddleWare2())
// r.GET("/middleWare1", func(c *gin.Context) {
// Buy()
// Cook()
// Eat()
// Wash()
// })
r.GET("/middleWare3", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"msg": "ok",
})
})
blog := r.Group("/blog")
{
blog.GET("/file/*all", controller.FileAll)
blog.GET("/list", controller.List)
blog.GET("/info/:id", controller.Info)
blog.GET("/add/:id/:title/:content", controller.Add)
blog.POST("/edit", controller.Edit)
blog.POST("/editJson", controller.EditJson)
blog.POST("/login", controller.Login)
}
go func() {
r.Run()
}()
exit := make(chan os.Signal)
signal.Notify(exit, syscall.SIGINT, syscall.SIGTERM)
<-exit
fmt.Println("程序优雅退出,结束。。。")
}