-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (54 loc) · 1.16 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
package main
import (
"context"
"net/http"
"os"
"path"
"time"
"github.com/codemodus/sigmon/v2"
_ "github.com/codemodus/termsrv/statik"
)
func main() {
if err := run(); err != nil {
cmd := path.Base(os.Args[0])
logError(cmd, err)
os.Exit(1)
}
}
func run() error {
sm := sigmon.New(nil)
sm.Start()
defer sm.Stop()
es, err := newElements()
if err != nil {
return err
}
defer es.close()
sm.Set(func(s *sigmon.State) {
scp := "system signal handling abnormal"
sc, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := es.srv.Shutdown(sc); err != nil {
logError(scp, err)
}
sm.Stop()
})
go func() {
if err := es.mq.Feed(es.t); err != nil {
scp := "feed goroutine abnormal"
logError(scp, err)
sm.Stop()
sc, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()
if err := es.srv.Shutdown(sc); err != nil {
logError(scp, err)
}
}
}()
logInfof("serving on %s\n", es.srv.Addr)
defer logInfof("server on %s is closed\n", es.srv.Addr)
if err := es.srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
return err
}
return nil
}