-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
73 lines (63 loc) · 1.32 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
package main
import (
"embed"
"github.com/gin-gonic/gin"
"github.com/go-co-op/gocron"
"go-message-pusher/model"
"go-message-pusher/router"
"go-message-pusher/util"
"html/template"
"log"
"os"
"strconv"
"time"
)
//go:embed view
var fs embed.FS
func main() {
if os.Getenv("GIN_MODE") != "debug" {
gin.SetMode(gin.ReleaseMode)
}
var err error
err = util.ReadConfig()
if err != nil {
panic(err)
}
log.Println("Config loaded.")
err = model.InitDatabase()
if err != nil {
panic(err)
}
log.Println("Database initialized.")
err = model.InitCache(util.EnableRedis)
if err != nil {
panic(err)
}
log.Println("Cache initialized.")
server := gin.Default()
templ := template.Must(template.New("").ParseFS(fs, "view/**/*"))
server.SetHTMLTemplate(templ)
router.SetHtmlRouters(server)
router.SetMessageRouter(server)
router.SetUserRouter(server)
log.Println("Router set.")
s := gocron.NewScheduler(time.UTC)
_, err = s.Every(100).Minutes().Do(func() {
err := model.UpdateAllAccessTokens()
if err != nil {
log.Println("Failed to update accessTokens.")
return
}
log.Println("AccessTokens updated.")
})
if err != nil {
panic(err)
}
log.Println("Scheduler set.")
s.StartAsync()
log.Println("Server started.")
err = server.Run(":" + strconv.Itoa(util.ServerPort))
if err != nil {
panic(err)
}
}