-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
96 lines (90 loc) · 2.09 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var Db *gorm.DB
type Todo struct {
ID int `json:"id"`
Title string `json:"title"`
Status bool `json:"status"`
}
func lmysql() (err error) {
dsn := "dbuser:password@tcp(127.0.0.1:3306)/dbnam?charset=utf8mb4&parseTime=True&loc=Local"
Db, err = gorm.Open("mysql", dsn)
if err != nil {
return err
}
err = Db.DB().Ping()
if err != nil {
return err
}
return
}
func main() {
err := lmysql()
if err != nil {
panic(err)
}
defer Db.Close()
Db.AutoMigrate(&Todo{})
r := gin.Default()
r.Static("/static", "static")
r.LoadHTMLGlob("templates/*")
r.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", nil)
})
v := r.Group("v1")
{
v.POST("/todo", func(c *gin.Context) {
var todo Todo
c.BindJSON(&todo)
if err = Db.Create(&todo).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, todo)
}
})
v.GET("/todo", func(c *gin.Context) {
var todolist []Todo
if err = Db.Find(&todolist).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, todolist)
}
})
v.PUT("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无此ID"})
return
}
var todo Todo
if err = Db.Where("id=?", id).First(&todo).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
return
}
c.BindJSON(&todo)
if err = Db.Save(&todo).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, todo)
}
})
v.DELETE("/todo/:id", func(c *gin.Context) {
id, ok := c.Params.Get("id")
if !ok {
c.JSON(http.StatusOK, gin.H{"error": "无此ID"})
return
}
if err = Db.Where("id=?", id).Delete(Todo{}).Error; err != nil {
c.JSON(http.StatusOK, gin.H{"error": err.Error()})
} else {
c.JSON(http.StatusOK, gin.H{id: "delete"})
}
})
}
r.Run(":3389")
}