-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernel.go
124 lines (100 loc) · 2.91 KB
/
kernel.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
package cosy
import (
"context"
"errors"
"fmt"
"github.com/gin-gonic/gin"
"github.com/go-gormigrate/gormigrate/v2"
"github.com/uozi-tech/cosy/cron"
"github.com/uozi-tech/cosy/kernel"
"github.com/uozi-tech/cosy/logger"
"github.com/uozi-tech/cosy/model"
"github.com/uozi-tech/cosy/redis"
"github.com/uozi-tech/cosy/router"
"github.com/uozi-tech/cosy/settings"
"github.com/uozi-tech/cosy/sonyflake"
"net"
"net/http"
"os/signal"
"syscall"
"time"
)
var (
TCPAddr *net.TCPAddr
listener net.Listener
)
// SetListener Set the listener
func SetListener(l net.Listener) {
listener = l
}
// Boot the server
func Boot(confPath string) {
// Create a context that listens for the interrupt signal from the OS.
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
defer stop()
// Initialize settings package
settings.Init(confPath)
// Set gin mode
gin.SetMode(settings.ServerSettings.RunMode)
// Initialize logger package
logger.Init(settings.ServerSettings.RunMode)
defer logger.Sync()
// If redis settings addr is not empty, init redis
if settings.RedisSettings.Addr != "" {
redis.Init()
}
// Initialize sonyflake
sonyflake.Init()
// Start cron
cron.Start()
// Gin router initialization
router.Init()
// Kernel boot
kernel.Boot()
addr := fmt.Sprintf("%s:%d", settings.ServerSettings.Host, settings.ServerSettings.Port)
srv := &http.Server{
Addr: addr,
Handler: router.GetEngine(),
}
// If the listener is nil, create a new listener, otherwise use the preset listener.
if listener == nil {
var err error
listener, err = net.Listen("tcp", addr)
if err != nil {
logger.Fatalf("listen: %s\n", err)
}
}
// Start the gin server
go func() {
if err := srv.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.Fatalf("listen: %s\n", err)
}
}()
TCPAddr = listener.Addr().(*net.TCPAddr)
logger.Info("Server listening on", TCPAddr.String())
// Listen for the interrupt signal.
<-ctx.Done()
// Restore default behavior on the interrupt signal and notify user of shutdown.
stop()
logger.Info("shutting down gracefully, press Ctrl+C again to force")
// 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 {
logger.Fatal("Server forced to shutdown: ", err)
}
logger.Info("Server exited")
}
// RegisterInitFunc Register init functions, this function should be called before kernel boot.
func RegisterInitFunc(f ...func()) {
kernel.RegisterInitFunc(f...)
}
// RegisterGoroutine Register syncs functions, this function should be called before kernel boot.
func RegisterGoroutine(f ...func()) {
kernel.RegisterGoroutine(f...)
}
// RegisterMigration Register a migration
func RegisterMigration(m []*gormigrate.Migration) {
model.RegisterMigration(m)
}