-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (103 loc) · 2.24 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
package main
import (
"context"
"io"
"log"
"lualoader/internal/config"
"lualoader/internal/golua"
"lualoader/internal/plugins"
"lualoader/internal/utils"
"net/http"
"os"
"os/signal"
"sync"
)
var (
cfg config.Config
elog *log.Logger
)
const (
VERSION_MAJOR uint = 1
VERSION_MINOR uint = 1
VERSION_PATCH uint = 0
PLUGINS_PATH string = "plugins"
)
func init() {
log.Printf("lualoader v%d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH)
log.Println("Init...")
eLogFile, err := os.Create("errlog.txt")
if err != nil {
log.Fatalln("Init: cannot create log file: ", err)
}
elog = log.New(io.MultiWriter(eLogFile, os.Stdout), "[ERR] ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
err = config.CheckConfig()
if err != nil {
elog.Fatalln("Init: CheckConfig: ", err)
}
err = config.ReadConfigTo(&cfg)
if err != nil {
elog.Fatalln("Init: ReadConfigTo: ", err)
}
err = utils.CheckDir(PLUGINS_PATH)
if err != nil {
elog.Fatalln("Init: CheckDir: ", err)
}
// 注册路由
golua.Mux = http.NewServeMux()
// Enable Plugins
err = plugins.EnablePlugins(PLUGINS_PATH)
if err != nil {
elog.Fatalln(err)
}
log.Println("Init Done.")
}
// waitgroup
var wg sync.WaitGroup
// 终止信号用channel
var (
signalListener = make(chan os.Signal)
stopSignal = make(chan struct{})
httpDone = make(chan struct{})
)
// stop信号广播
func sl() {
<-signalListener
close(signalListener)
close(stopSignal)
log.Println("Stopping...")
wg.Done()
}
func main() {
// 监听终止信号
signal.Notify(signalListener, os.Interrupt)
wg.Add(3)
go sl()
var srv = new(http.Server)
srv.Addr = cfg.Port
srv.Handler = golua.Mux
go func() {
<-stopSignal
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
elog.Printf("HTTP server Shutdown: %v\n", err)
}
log.Println("HTTP server Shutdown.")
close(httpDone)
wg.Done()
}()
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// Error starting or closing listener:
elog.Fatalf("HTTP server ListenAndServe: %v\n", err)
}
go func() {
<-stopSignal
<-httpDone
err := plugins.DisablePlugins()
if err != nil {
elog.Fatalln(err)
}
wg.Done()
}()
wg.Wait()
log.Println("Stopped.")
}