-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
95 lines (80 loc) · 2.13 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
package main
import (
"fmt"
"first/articles"
"first/common"
"first/communities"
"first/users"
"first/websocket"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/gin-contrib/cors"
"github.com/jinzhu/gorm"
)
func init() {
// Log error if .env file does not exist
if err := godotenv.Load(); err != nil {
fmt.Printf("No .env file found")
}
}
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{})
db.AutoMigrate(&users.CommunityModel{})
}
func main() {
db := common.Init()
Migrate(db)
defer db.Close()
r := gin.Default()
r.Use(cors.Default())
v1 := r.Group("/api")
// v1.Use(static.Serve("/public", static.LocalFile("./public", true)))
v1.Static("/public", "./public")
go websocket.Manager.Start()
v1.GET("/ws/:id", func(c *gin.Context) {
id := c.Param("id")
websocket.WsPage(c.Writer, c.Request, id)
})
users.UsersRegister(v1.Group("/users"))
v1.Use(users.AuthMiddleware(false))
articles.ArticlesAnonymousRegister(v1.Group("/articles"))
articles.TagsAnonymousRegister(v1.Group("/tags"))
communities.CommunitiesAnonymousRegister(v1.Group("/communities"))
v1.Use(users.AuthMiddleware(true))
users.UserRegister(v1.Group("/user"))
users.ProfileRegister(v1.Group("/profiles"))
communities.CommunitiesRegister(v1.Group("/communities"))
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: "aaaa@g.cn",
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)
fmt.Println("Hello go")
r.Run(":8080") // listen and serve on 0.0.0.0:8080
}