/
server.go
126 lines (109 loc) · 2.68 KB
/
server.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
125
126
package server
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"
"github.com/gin-gonic/gin"
"github.com/ihexxa/gocfg"
"github.com/ihexxa/quickshare/src/depidx"
"github.com/ihexxa/quickshare/src/fs"
"github.com/ihexxa/quickshare/src/kvstore"
)
type Server struct {
server *http.Server
cfg gocfg.ICfg
deps *depidx.Deps
signalChan chan os.Signal
}
func NewServer(cfg gocfg.ICfg) (*Server, error) {
if !cfg.BoolOr("Server.Debug", false) {
gin.SetMode(gin.ReleaseMode)
}
initer := NewIniter(cfg)
deps := initer.InitDeps()
router, err := initer.InitHandlers(deps)
if err != nil {
return nil, fmt.Errorf("init handlers error: %w", err)
}
port := cfg.GrabInt("Server.Port")
portStr, ok := cfg.String("ENV.PORT")
if ok && portStr != "" {
port, err = strconv.Atoi(portStr)
if err != nil {
deps.Log().Fatalf("invalid port: %s", portStr)
}
cfg.SetInt("Server.Port", port)
}
srv := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.GrabString("Server.Host"), port),
Handler: router,
ReadTimeout: time.Duration(cfg.GrabInt("Server.ReadTimeout")) * time.Millisecond,
WriteTimeout: time.Duration(cfg.GrabInt("Server.WriteTimeout")) * time.Millisecond,
MaxHeaderBytes: cfg.GrabInt("Server.MaxHeaderBytes"),
}
return &Server{
server: srv,
deps: deps,
cfg: cfg,
}, nil
}
func (s *Server) Start() error {
s.signalChan = make(chan os.Signal, 4)
signal.Notify(s.signalChan, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-s.signalChan
if sig != nil {
s.deps.Log().Infow(
fmt.Sprintf("received signal %s: shutting down", sig.String()),
)
}
s.Shutdown()
}()
s.deps.Log().Infow(
"quickshare is starting",
"hostname:port",
fmt.Sprintf(
"%s:%d",
s.cfg.GrabString("Server.Host"),
s.cfg.GrabInt("Server.Port"),
),
)
err := s.server.ListenAndServe()
if err != http.ErrServerClosed {
return fmt.Errorf("listen error: %w", err)
}
return nil
}
func (s *Server) Shutdown() error {
// TODO: add timeout
err := s.deps.FileIndex().WriteTo(fileIndexPath)
if err != nil {
s.deps.Log().Errorf("failed to persist file index: %s", err)
}
s.deps.Workers().Stop()
err = s.deps.FS().Close()
if err != nil {
s.deps.Log().Errorf("failed to close file system: %s", err)
}
err = s.deps.DB().Close()
if err != nil {
s.deps.Log().Errorf("failed to close database: %s", err)
}
err = s.server.Shutdown(context.Background())
if err != nil {
s.deps.Log().Errorf("failed to shutdown server: %s", err)
}
s.deps.Log().Sync()
return nil
}
func (s *Server) depsFS() fs.ISimpleFS {
return s.deps.FS()
}
func (s *Server) depsKVStore() kvstore.IKVStore {
return s.deps.KV()
}