-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpserver.go
More file actions
228 lines (191 loc) · 4.84 KB
/
httpserver.go
File metadata and controls
228 lines (191 loc) · 4.84 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package httpserver
import (
"context"
"errors"
"fmt"
"io/fs"
"log/slog"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path"
"path/filepath"
"strings"
"time"
"github.com/rjeczalik/notify"
)
type Mode string
const (
ModeProduction Mode = "production"
ModeDevelopment Mode = "development"
)
type Server struct {
shutdownTimeout time.Duration
listenAddress string
mode Mode
watchEnabled bool
HTTPServer *http.Server
log *slog.Logger
ctx context.Context
stopCtx context.CancelFunc
shutdownHooks []func(context.Context) error
}
func New(handler http.Handler, options ...Option) *Server {
srv := &Server{
HTTPServer: &http.Server{
Addr: "",
Handler: handler,
},
shutdownTimeout: defaultShutdownTimeout,
}
for _, option := range options {
option(srv)
}
if srv.mode != ModeProduction {
srv.mode = ModeDevelopment
}
if srv.log == nil {
setDefaultLogger(srv)
}
if srv.listenAddress == "" {
switch srv.mode {
case ModeProduction:
srv.listenAddress = "0.0.0.0:80"
case ModeDevelopment:
srv.listenAddress = "localhost:8080"
}
}
serverCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
srv.ctx = serverCtx
srv.stopCtx = stop
return srv
}
func (s *Server) AddShutdownHook(hook func(context.Context) error) {
s.shutdownHooks = append(s.shutdownHooks, hook)
}
func (s *Server) Run() error {
defer s.stopCtx()
s.log.Info("Starting HTTP server",
"mode", s.mode,
"listenAddress", formatListenAddress(s.listenAddress),
)
s.HTTPServer.BaseContext = func(_ net.Listener) context.Context { return s.ctx }
s.HTTPServer.Addr = s.listenAddress
srvErr := make(chan error, 1)
go func() {
srvErr <- s.HTTPServer.ListenAndServe()
}()
if s.mode == ModeProduction {
// Wait for interruption.
select {
case err := <-srvErr:
return err
case <-s.ctx.Done():
// Wait for first CTRL+C.
// Stop receiving signal notifications as soon as possible.
s.stopCtx()
}
} else {
fileChangesChan := watchForFileChanges(s.log)
defer notify.Stop(fileChangesChan)
loop:
for {
select {
case err := <-srvErr:
return err
case <-s.ctx.Done():
// Wait for first CTRL+C.
// Stop receiving signal notifications as soon as possible.
s.stopCtx()
break loop
case changeEvent := <-fileChangesChan:
s.handleFileChange(changeEvent)
}
}
}
// When Shutdown is called, ListenAndServe immediately returns ErrServerClosed.
return s.startGracefulShutdown()
}
func (s *Server) Context() context.Context {
return s.ctx
}
func (s *Server) startGracefulShutdown() error {
timeoutContext, doCancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer doCancel()
// We received an interrupt signal, shut down.
s.log.Info("Shutting down ..")
s.HTTPServer.SetKeepAlivesEnabled(false)
if err := s.HTTPServer.Shutdown(timeoutContext); err != nil {
// Error from closing listeners, or context timeout.
return err
}
var err error
for _, hook := range s.shutdownHooks {
err = errors.Join(err, hook(timeoutContext)) // TODO use multierrors
}
return err
}
func (s *Server) handleFileChange(event notify.EventInfo) {
if !s.watchEnabled {
return
}
isGoFile := strings.HasSuffix(event.Path(), ".go")
if !isGoFile {
return
}
moduleRoot := modulePath()
pathToGenerate := strings.Replace(path.Dir(event.Path()), moduleRoot, ".", 1)
s.log.Info("file changed", "event", event.Event(), "path", pathToGenerate)
genCmd := exec.Command("go", "generate", pathToGenerate)
genCmd.Dir = moduleRoot
genCmd.Stdout = os.Stdout
genCmd.Stderr = os.Stderr
err := genCmd.Run()
if err != nil {
s.log.Error("go generate failed", "error", err)
return
}
os.Getwd()
}
func watchForFileChanges(logger *slog.Logger) (c chan notify.EventInfo) {
// Make the channel buffered to ensure no event is dropped. Notify will drop
// an event if the receiver is not able to keep up the sending pace.
c = make(chan notify.EventInfo, 1)
// Set up a watchpoint listening on events within current working directory.
// Dispatch each create and remove events separately to c.
watchPath := modulePath()
if err := notify.Watch(path.Join(watchPath, "..."), c, notify.All); err != nil {
panic(fmt.Sprintf("Error with watching for file changes, exiting ... %v", err))
}
logger.Info("Watching for file changes", "path", watchPath)
return
}
func modulePath() (roots string) {
dir, err := os.Getwd()
if err != nil {
panic(err)
}
dir = filepath.Clean(dir)
// Look for enclosing go.mod.
for {
ffs := os.DirFS(dir)
info, err := fs.Stat(ffs, "go.mod")
if err == nil && !info.IsDir() {
return dir
}
d := filepath.Dir(dir)
if d == dir {
break
}
dir = d
}
panic(errors.New("couldn't find go.mod!"))
}
func formatListenAddress(addr string) string {
if strings.Index(addr, ":") == 0 {
addr = "0.0.0.0" + addr
}
return fmt.Sprintf("http://%s", addr)
}