-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
120 lines (100 loc) · 2.6 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
120
// @title Open Tree Hole Auth
// @version 2.0
// @description Next Generation of Auth microservice integrated with kong for registration and issuing tokens
// @contact.name Maintainer Chen Ke
// @contact.url https://danxi.fduhole.com/about
// @contact.email [email protected]
// @license.name Apache 2.0
// @license.url https://www.apache.org/licenses/LICENSE-2.0.html
// @host localhost:8000
// @BasePath /api
package main
import (
"context"
"os"
"os/signal"
"syscall"
"time"
_ "time/tzdata"
"github.com/goccy/go-json"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/recover"
"github.com/opentreehole/go-common"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
"auth_next/apis"
"auth_next/config"
_ "auth_next/docs"
"auth_next/models"
"auth_next/utils/auth"
"auth_next/utils/kong"
)
func init() {
// set default time zone
loc, _ := time.LoadLocation("Asia/Shanghai")
time.Local = loc
}
func main() {
config.InitConfig()
auth.InitVerificationCodeCache()
models.InitDB()
apis.Init()
// connect to kong
if !config.Config.Standalone {
err := kong.Ping()
if err != nil {
log.Fatal().Err(err).Msg("kong ping failed")
}
}
app := fiber.New(fiber.Config{
ErrorHandler: common.ErrorHandler,
JSONEncoder: json.Marshal,
JSONDecoder: json.Unmarshal,
DisableStartupMessage: true,
BodyLimit: 128 * 1024 * 1024,
})
RegisterMiddlewares(app)
apis.RegisterRoutes(app)
cancel := startTasks()
go func() {
err := app.Listen("0.0.0.0:8000")
if err != nil {
log.Fatal().Err(err).Msg("app listen failed")
}
}()
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.Err(err).Msg("error shutdown app")
}
// stop tasks
cancel()
}
func startTasks() context.CancelFunc {
_, cancel := context.WithCancel(context.Background())
c := cron.New()
_, err := c.AddFunc("CRON_TZ=Asia/Shanghai 0 0 * * *", models.ActiveStatusTask) // run every day 00:00 +8:00
if err != nil {
log.Fatal().Err(err).Msg("cron add func failed")
}
go c.Start()
return cancel
}
func RegisterMiddlewares(app *fiber.App) {
app.Use(recover.New(recover.Config{
EnableStackTrace: true,
StackTraceHandler: common.StackTraceHandler,
}))
app.Use(common.MiddlewareGetUserID)
if config.Config.Mode != "bench" {
app.Use(common.MiddlewareCustomLogger)
}
if config.Config.Mode == "dev" {
app.Use(cors.New(cors.Config{AllowOrigins: "*"})) // for swag docs
}
}