forked from friveroscl/interview-devops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
80 lines (66 loc) · 1.86 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
package main
import (
"log"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"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()
// Config CORS
corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
corsConfig.AddAllowHeaders("Authorization")
r.Use(cors.New(corsConfig))
// v1 := r.Group("")
v1 := r
users.UsersRegister(v1.Group("/users"))
v1.Use(users.AuthMiddleware(false))
articles.ArticlesAnonymousRegister(v1.Group("/articles"))
articles.TagsAnonymousRegister(v1.Group("/tags"))
users.ProfileAnonymousRegister(v1.Group("/profiles"))
v1.Use(users.AuthMiddleware(true))
users.UserRegister(v1.Group("/user"))
articles.ArticlesRegister(v1.Group("/articles"))
users.ProfileRegister(v1.Group("/profiles"))
// 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)
log.Fatalln(r.Run()) // listen and serve on 0.0.0.0:8080
}