-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
95 lines (77 loc) · 1.82 KB
/
server.go
File metadata and controls
95 lines (77 loc) · 1.82 KB
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
package browser
import (
"context"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"github.com/sirupsen/logrus"
)
// Server defines an APK Repository UI & API HTTP server
type Server struct {
*Config
tmpl *template.Template
router http.Handler
}
// NewServer creates a new APK Repository server
func NewServer(cfgPath string) *Server {
cfg, err := loadConfig(cfgPath)
if err != nil {
logrus.WithError(err).Fatal("newServerFailed")
}
srv := &Server{
Config: cfg,
}
return srv
}
// Init initlializes the required subsystem necessary for an APK Repository server based on its current configuration
func (s *Server) Init() (err error) {
// Init storage
if err = s.Storage.Init(); err != nil {
return err
}
// Init templator
if s.tmpl, err = template.New("").ParseGlob(path.Join(s.HTTP.Templates, "*.html.tmpl")); err != nil {
return err
}
// Init router
if err = s.initRouter(); err != nil {
return err
}
return nil
}
// Run starts the HTTP Server
func (s *Server) Run() error {
logrus.Info("serverInit")
if err := s.Init(); err != nil {
logrus.WithError(err).Fatal("serverInitFailed")
return err
}
h := http.Server{
Addr: s.HTTP.Addr,
Handler: s.router,
}
proto := "http"
if h.TLSConfig != nil {
proto = "https"
}
errCh := make(chan error, 1)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() { errCh <- h.ListenAndServe() }()
logrus.WithField("addr", fmt.Sprintf("%s://%s", proto, s.HTTP.Addr)).Info("serverRunning")
select {
case err := <-errCh:
logrus.WithError(err).Error("serverStoppedWithError")
return err
case sig := <-sigs:
logrus.WithField("signal", sig).Info("serverInterupt")
}
logrus.Info("serverStopping")
err := h.Shutdown(context.Background())
logrus.Info("serverStopped")
return err
}