-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.go
125 lines (103 loc) · 2.73 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
package slaxy
import (
"context"
"fmt"
"net"
"net/http"
"regexp"
"time"
"github.com/nlopes/slack"
"github.com/pkg/errors"
)
type handler func(l net.Listener)
// Config holds all config values
type Config struct {
GracePeriod time.Duration `mapstructure:"grace-period"`
Addr string
Token string `mapstructure:"token"`
ExcludedFields []string `mapstructure:"excluded-fields"`
}
// server types
type server struct {
cfg Config
logger Logger
done chan struct{}
srv *http.Server
errChan chan error
slack *slack.Client
excludedFields []*regexp.Regexp
}
// Server represents a server instance
type Server interface {
Start() error
Stop() error
Errors() <-chan error
}
// New creates a new server instance
func New(cfg Config, logger Logger) Server {
return &server{
cfg: cfg,
logger: logger,
done: make(chan struct{}, 1),
errChan: make(chan error, 100),
}
}
// Start starts up the server
func (s *server) Start() error {
return s.setup(s.cfg.Addr, s.handleWeb)
}
// Stop gracefully shuts down the server
func (s *server) Stop() error {
s.done <- struct{}{}
ctx, cancel := context.WithTimeout(context.Background(), s.cfg.GracePeriod)
err := s.srv.Shutdown(ctx)
cancel()
return err
}
// Errors returns the error channel
func (s *server) Errors() <-chan error {
return s.errChan
}
// setup starts up a server with its own listener and handler function
func (s *server) setup(addr string, handler handler) error {
// pre-compile regexes
excludedFields := make([]*regexp.Regexp, 0, len(s.cfg.ExcludedFields))
for _, regex := range s.cfg.ExcludedFields {
excludedFields = append(excludedFields, regexp.MustCompile(regex))
}
s.excludedFields = excludedFields
// connect to slack
client := slack.New(s.cfg.Token)
_, err := client.AuthTest()
if err != nil {
return err
}
s.slack = client
// start tcp listener
l, err := net.Listen("tcp", addr)
if err != nil {
return errors.Wrapf(err, "failed to listen on %s", addr)
}
s.logger.Info(fmt.Sprintf("Listening on %s", addr))
go s.handleListener(l, addr, handler)
return nil
}
// handleListener handles a listener using the specified handler function
func (s *server) handleListener(l net.Listener, addr string, handler handler) {
defer s.logger.Info(fmt.Sprintf("Listener %s shutdown", addr))
handler(l)
}
// handleWeb handles all incoming connections to the webhook server
func (s *server) handleWeb(l net.Listener) {
mux := http.NewServeMux()
mux.HandleFunc("/", s.handleWebhook)
s.srv = &http.Server{
Handler: mux,
}
err := s.srv.Serve(l)
// server closed abnormally
if err != nil && err != http.ErrServerClosed {
err = errors.Wrap(err, "server failed")
s.errChan <- err
}
}