-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (65 loc) · 1.6 KB
/
main.go
File metadata and controls
76 lines (65 loc) · 1.6 KB
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
package main
import (
"context"
"embed"
"fmt"
"net/http"
"os"
"os/signal"
"pear-admin-go/app/core/config"
"pear-admin-go/app/core/db"
"pear-admin-go/app/core/log"
"pear-admin-go/app/core/redis"
"pear-admin-go/app/router"
"pear-admin-go/app/util/gconv"
"pear-admin-go/app/util/validate"
"syscall"
"time"
)
//go:embed template
var templateFs embed.FS
//go:embed static
var staticFs embed.FS
func main() {
if err := validate.InitTrans("zh"); err != nil {
fmt.Println("init trans failed, err:", err)
}
config.InitConfig("./config.toml")
log.InitLog()
db.InitConn()
redis.InitRedis()
r := router.InitRouter(staticFs, templateFs)
s := &http.Server{
Addr: fmt.Sprintf(":%d", config.Instance().App.HttpPort),
Handler: r,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1 << 20,
}
fmt.Printf(` 欢迎使用 Pear Admin Go
程序运行地址:http://127.0.0.1:%s
`, gconv.String(config.Instance().App.HttpPort))
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Instance().Error(err.Error())
os.Exit(0)
}
}()
shutDown(s)
}
func shutDown(s *http.Server) {
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Println("Shutdown Server ...")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := db.Instance().Close()
if err != nil {
log.Instance().Fatal("Close DB error:" + err.Error())
}
if err := s.Shutdown(ctx); err != nil {
log.Instance().Fatal("Server Shutdown:" + err.Error())
}
fmt.Println("Server exiting")
}