-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
43 lines (37 loc) · 879 Bytes
/
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
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/pkg/errors"
"github.com/zullin/volatile-cache/config"
"github.com/zullin/volatile-cache/server"
)
func main() {
cfg := config.GetInstance()
srv := server.NewServer(cfg.Host, cfg.Port)
_, err := os.Stat(cfg.StoreFileName)
if os.IsExist(err) {
r, err := os.Open(cfg.StoreFileName)
if err != nil {
log.Print(errors.Wrapf(err, "can't open file: %s", cfg.StoreFileName))
}
srv.FillServerCache(r)
}
idleConnsClosed := make(chan struct{})
go func() {
sigint := make(chan os.Signal)
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
<-sigint
w, err := os.Create(cfg.StoreFileName)
if err == nil {
srv.Stop(w)
} else {
log.Print(errors.Wrapf(err, "can't open file: %s", cfg.StoreFileName))
}
close(idleConnsClosed)
}()
log.Fatal(srv.Start())
<-idleConnsClosed
}