-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
89 lines (74 loc) · 1.68 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
// @title Moss Backend
// @version 0.0.1
// @description Moss Backend
// @contact.name Maintainer Chen Ke
// @contact.url https://danxi.fduhole.com/about
// @contact.email dev@fduhole.com
// @license.name Apache 2.0
// @license.url https://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8000
// @BasePath /api
package main
import (
"MOSS_backend/apis"
"MOSS_backend/apis/record"
"MOSS_backend/config"
_ "MOSS_backend/docs"
"MOSS_backend/middlewares"
"MOSS_backend/models"
"MOSS_backend/utils"
"MOSS_backend/utils/auth"
"MOSS_backend/utils/kong"
"encoding/json"
"github.com/gofiber/fiber/v2"
"github.com/robfig/cron/v3"
"log"
"os"
"os/signal"
"syscall"
)
func main() {
config.InitConfig()
models.InitDB()
auth.InitCache()
// connect to kong
err := kong.Ping()
if err != nil {
panic(err)
}
app := fiber.New(fiber.Config{
AppName: config.AppName,
ErrorHandler: utils.MyErrorHandler,
JSONDecoder: json.Unmarshal,
JSONEncoder: json.Marshal,
DisableStartupMessage: true,
})
middlewares.RegisterMiddlewares(app)
apis.RegisterRoutes(app)
startTasks()
go func() {
err = app.Listen("0.0.0.0:8000")
if err != nil {
log.Println(err)
}
}()
interrupt := make(chan os.Signal, 1)
// wait for CTRL-C interrupt
signal.Notify(interrupt, syscall.SIGINT, syscall.SIGTERM)
<-interrupt
// close app
err = app.Shutdown()
if err != nil {
log.Println(err)
}
_ = utils.Logger.Sync()
}
func startTasks() {
c := cron.New()
_, err := c.AddFunc("CRON_TZ=Asia/Shanghai 0 0 * * *", models.ActiveStatusTask) // run every day 00:00 +8:00
if err != nil {
panic(err)
}
go c.Start()
go record.UserLockCheck()
}