/
server.go
76 lines (61 loc) · 1.67 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
package lfgw
import (
"context"
"errors"
"fmt"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"syscall"
"time"
)
// serve starts a web server and ensures graceful shutdown
func (app *application) serve() error {
// Just to make sure our logging calls are always safe
if app.logger == nil {
app.configureLogging()
}
app.proxy = httputil.NewSingleHostReverseProxy(app.UpstreamURL)
// TODO: somehow pass more context to ErrorLog (unsafe?)
app.proxy.ErrorLog = app.errorLog
app.proxy.FlushInterval = time.Millisecond * 200
// TODO: somehow pass more context to ErrorLog
//#nosec G112 -- false positive, may be removed after gosec v2.12.0+ is released
srv := &http.Server{
Addr: fmt.Sprintf(":%d", app.Port),
ErrorLog: app.errorLog,
Handler: app.routes(),
IdleTimeout: time.Minute,
ReadTimeout: app.ReadTimeout,
WriteTimeout: app.WriteTimeout,
}
shutdownError := make(chan error)
go func() {
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
s := <-quit
app.logger.Info().Caller().
Msgf("Caught %s signal, waiting for all connections to be closed within %s", s, app.GracefulShutdownTimeout)
ctx, cancel := context.WithTimeout(context.Background(), app.GracefulShutdownTimeout)
defer cancel()
err := srv.Shutdown(ctx)
if err != nil {
shutdownError <- err
}
shutdownError <- nil
}()
app.logger.Info().Caller().
Msgf("Starting server on %d", app.Port)
err := srv.ListenAndServe()
if !errors.Is(err, http.ErrServerClosed) {
return err
}
err = <-shutdownError
if err != nil {
return err
}
app.logger.Info().Caller().
Msg("Successfully stopped server")
return nil
}