-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
46 lines (36 loc) · 1.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
package main
import (
"GoCommunityAPI/config"
"GoCommunityAPI/controllers"
"GoCommunityAPI/database"
"GoCommunityAPI/database/entities"
_ "GoCommunityAPI/docs"
"github.com/gin-gonic/gin"
swaggerFiles "github.com/swaggo/files"
ginSwagger "github.com/swaggo/gin-swagger"
)
func init() {
config.LoadEnvVariables()
database.ConnectToDB()
migrate()
}
func main() {
goEngin := gin.Default()
apiRouteGroup := goEngin.Group("/api")
controllers.RegisterUserRoutes(apiRouteGroup.Group("/user"))
controllers.RegisterArticleRoutes(apiRouteGroup.Group("/article"))
controllers.RegisterCommentRoutes(apiRouteGroup.Group("/comment"))
goEngin.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
goEngin.Run()
}
func migrate() {
if !database.DB.Migrator().HasTable(&entities.UserEntity{}) {
database.DB.AutoMigrate(&entities.UserEntity{})
}
if !database.DB.Migrator().HasTable(&entities.ArticleEntity{}) {
database.DB.AutoMigrate(&entities.ArticleEntity{})
}
if !database.DB.Migrator().HasTable(&entities.CommentEntity{}) {
database.DB.AutoMigrate(&entities.CommentEntity{})
}
}