forked from gnimli/go-web-mini
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
144 lines (113 loc) · 3.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"context"
"embed"
"fmt"
"github.com/sjqzhang/gdi"
"go-web-mini/config"
"go-web-mini/global"
"go-web-mini/routes"
"io"
"io/fs"
"net/http"
"os"
"os/signal"
"syscall"
"time"
)
// @title Swagger Example API
// @version v1
// @description This is a sample server Petstore server.
// @termsOfService http://localhost
// @BasePath /api
// @schemes http https
// @securityDefinitions.apikey JWT
// @in header
// @name Authorization
func main() {
//生成依赖
gdi.GenGDIRegisterFile(true)
// 加载配置文件到全局配置结构体
config.InitConfig()
// 初始化日志
global.InitLogger()
// 初始化数据库(mysql)
global.InitMysql()
global.InitRedis()
// 初始化casbin策略管理器
global.InitCasbinEnforcer()
// 初始化Validator数据校验
global.InitValidate()
// 初始化mysql数据
global.InitData()
//bs,err:=gdiEmbededFiles.ReadFile("config/config.go")
//fmt.Println(string(bs),err)
// 注册所有路由
r := routes.InitRoutes()
// 操作日志中间件处理日志时没有将日志发送到rabbitmq或者kafka中, 而是发送到了channel中
// 这里开启3个goroutine处理channel将日志记录到数据库
//logRepository := gdi.Get(&repository.OperationLogRepository{}).(*repository.OperationLogRepository)
//for i := 0; i < 3; i++ {
// go logRepository.SaveOperationLogChannel(nil, middleware.OperationLogChan)
//}
host := "localhost"
port := config.Conf.System.Port
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", host, port),
Handler: r,
}
// Initializing the server in a goroutine so that
// it won't block the graceful shutdown handling below
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
global.Log.Fatalf("listen: %s\n", err)
}
}()
global.Log.Info(fmt.Sprintf("Server is running at %s:%d/%s", host, port, config.Conf.System.UrlPathPrefix))
// Wait for interrupt signal to gracefully shutdown the server with
// a timeout of 5 seconds.
quit := make(chan os.Signal)
// kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT
// kill -9 is syscall.SIGKILL but can't be catch, so don't need add it
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
global.Log.Info("Shutting down server...")
// The context is used to inform the server it has 5 seconds to finish
// the request it is currently handling
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
global.Log.Fatal("Server forced to shutdown:", err)
}
global.Log.Info("Server exiting!")
}
//go:embed conf
var confFs embed.FS
func init() {
extractFilesIfNotExists(confFs, "conf", "./conf")
}
func extractFilesIfNotExists(fs2 embed.FS, sourceDir, targetDir string) error {
// 检查目标目录是否存在
return fs.WalkDir(fs2, sourceDir, func(path string, d fs.DirEntry, err error) error {
// 如果是目录,则创建目录
if d.IsDir() {
return os.MkdirAll(path, 0755)
}
// 打开嵌入的文件
ff, err := fs2.Open(path)
if err != nil {
return err
}
defer ff.Close()
// 创建目标文件
tf, err := os.Create(path)
if err != nil {
return err
}
defer tf.Close()
// 复制内容
_, err = io.Copy(tf, ff)
return err
})
}