forked from Away0x/goweibo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
119 lines (99 loc) · 2.71 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package main
import (
"fmt"
"html/template"
"net/http"
"github.com/lexkong/log"
"github.com/gin-gonic/gin"
"gin_weibo/app/helpers"
followerModel "gin_weibo/app/models/follower"
passwordResetModel "gin_weibo/app/models/password_reset"
statusModel "gin_weibo/app/models/status"
userModel "gin_weibo/app/models/user"
"gin_weibo/config"
"gin_weibo/database"
"gin_weibo/database/factory"
"gin_weibo/routes"
"gin_weibo/routes/named"
"github.com/spf13/pflag"
)
var (
// 需要 mock data,注意该操作会覆盖数据库;只在非 release 时生效
needMock = pflag.BoolP("mock", "m", false, "need mock data")
)
func main() {
// 解析命令行参数
pflag.Parse()
// 初始化配置
config.InitConfig()
// gin config
g := gin.New()
setupGin(g)
// db config
db := database.InitDB()
// db migrate
db.AutoMigrate(
&userModel.User{},
&passwordResetModel.PasswordReset{},
&statusModel.Status{},
&followerModel.Follower{},
)
// mock data
if do := factoryMake(); do {
return
}
defer db.Close()
// router register
routes.Register(g)
printRoute()
// 启动
fmt.Printf("\n\n-------------------------------------------------- Start to listening the incoming requests on http address: %s --------------------------------------------------\n\n", config.AppConfig.Addr)
if err := http.ListenAndServe(config.AppConfig.Addr, g); err != nil {
log.Fatal("http server 启动失败", err)
}
}
// 配置 gin
func setupGin(g *gin.Engine) {
// 启动模式配置
gin.SetMode(config.AppConfig.RunMode)
// 项目静态文件配置
g.Static("/"+config.AppConfig.PublicPath, config.AppConfig.PublicPath)
g.StaticFile("/favicon.ico", config.AppConfig.PublicPath+"/favicon.ico")
// 模板配置
// 注册模板函数
g.SetFuncMap(template.FuncMap{
// 根据 laravel-mix 的 public/mix-manifest.json 生成静态文件 path
"Mix": helpers.Mix,
// 生成项目静态文件地址
"Static": helpers.Static,
// 获取命名路由的 path
"Route": named.G,
"RelativeRoute": named.GR,
})
// 模板存储路径
g.LoadHTMLGlob(config.AppConfig.ViewsPath + "/**/*")
}
// 数据 mock
func factoryMake() (do bool) {
// 只有非 release 时才可用该函数
if config.AppConfig.RunMode == config.RunmodeRelease {
return false
}
status := *needMock
if !status {
return false
}
fmt.Print("\n\n-------------------------------------------------- MOCK --------------------------------------------------\n\n")
factory.UsersTableSeeder(true)
factory.StatusTableSeeder(true)
factory.FollowerTableSeeder(true)
return true
}
// 打印命名路由
func printRoute() {
// 只有非 release 时才可用该函数
if config.AppConfig.RunMode == config.RunmodeRelease {
return
}
named.PrintRoutes()
}