forked from gothinkster/golang-gin-realworld-example-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.go
73 lines (58 loc) · 1.55 KB
/
hello.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
package main
import (
"fmt"
"gopkg.in/gin-gonic/gin.v1"
"github.com/jinzhu/gorm"
"github.com/wangzitian0/golang-gin-starter-kit/articles"
"github.com/wangzitian0/golang-gin-starter-kit/common"
"github.com/wangzitian0/golang-gin-starter-kit/users"
)
func Migrate(db *gorm.DB) {
users.AutoMigrate()
db.AutoMigrate(&articles.ArticleModel{})
db.AutoMigrate(&articles.TagModel{})
db.AutoMigrate(&articles.FavoriteModel{})
db.AutoMigrate(&articles.ArticleUserModel{})
db.AutoMigrate(&articles.CommentModel{})
}
func main() {
db := common.Init()
Migrate(db)
defer db.Close()
r := gin.Default()
v1 := r.Group("/api")
users.UsersRegister(v1.Group("/users"))
v1.Use(users.AuthMiddleware(false))
articles.ArticlesAnonymousRegister(v1.Group("/articles"))
articles.TagsAnonymousRegister(v1.Group("/tags"))
v1.Use(users.AuthMiddleware(true))
users.UserRegister(v1.Group("/user"))
users.ProfileRegister(v1.Group("/profiles"))
articles.ArticlesRegister(v1.Group("/articles"))
testAuth := r.Group("/api/ping")
testAuth.GET("/", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
// test 1 to 1
tx1 := db.Begin()
userA := users.UserModel{
Username: "AAAAAAAAAAAAAAAA",
Email: "[email protected]",
Bio: "hehddeda",
Image: nil,
}
tx1.Save(&userA)
tx1.Commit()
fmt.Println(userA)
//db.Save(&ArticleUserModel{
// UserModelID:userA.ID,
//})
//var userAA ArticleUserModel
//db.Where(&ArticleUserModel{
// UserModelID:userA.ID,
//}).First(&userAA)
//fmt.Println(userAA)
r.Run() // listen and serve on 0.0.0.0:8080
}