-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
60 lines (50 loc) · 1.26 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
package main
import (
"context"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
"github.com/exyb/harbor-hook-to-mail/routes"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func main() {
mainDir, _ := os.Getwd()
os.Setenv("config_file_path", filepath.Join(mainDir, "config.yaml"))
ctx := context.Background()
ctx = context.WithValue(ctx, "work_path", mainDir)
ctx = context.WithValue(ctx, "config_file_path", filepath.Join(mainDir, "config.yaml"))
// 设置信号处理器
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-signalChan
fmt.Println("Received an interrupt, saving map to file...")
if err := routes.SaveMapToFile(); err != nil {
fmt.Printf("Error saving map to file: %v\n", err)
}
os.Exit(0)
}()
// 处理 web 请求
r := gin.Default()
routes.SetupRouter(r)
r.Use(func(c *gin.Context) {
c.Set("ctx", ctx)
c.Next()
})
config := viper.New()
config.SetConfigFile("config.yaml")
config.SetConfigType("yaml")
if err := config.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
log.Println("config file not found")
} else {
log.Fatalln(err)
}
}
port := ":" + config.GetString("server.port")
r.Run(port)
}