-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
77 lines (66 loc) · 1.78 KB
/
main.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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"time"
"github.com/alecthomas/units"
"github.com/mozey/httprouter-util/internal/app"
"github.com/mozey/httprouter-util/pkg/config"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
func main() {
conf := config.New()
h, cleanup := app.CreateRouter(conf)
defer cleanup()
// Header to make app reloads more visible
fmt.Println(".")
fmt.Println("..")
fmt.Println("...")
fmt.Println("....")
fmt.Println(".....")
var srv http.Server
srv.Handler = h.HTTPHandler
srv.Addr = h.Config.Addr()
// Harden server for Internet exposure
// https://github.com/mozey/httprouter-util/issues/2
srv.ReadTimeout = 5 * time.Second
srv.WriteTimeout = 2 * srv.ReadTimeout
maxBytes, err := h.Config.FnMaxBytesKb().Int64()
if err != nil {
log.Error().Stack().Err(err).Msg("")
os.Exit(1)
}
srv.MaxHeaderBytes = int(maxBytes * int64(units.KiB))
shutdown := make(chan struct{})
go func() {
// "Shutdown gracefully shuts down the server without
// interrupting any active connections...
// does not attempt to close nor wait for... WebSockets"
// https://golang.org/pkg/net/http/#Server.Shutdown
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt)
<-sigint
log.Info().Msg("ctrl+c interrupt, shutting down...")
// Interrupt signal received, shut down.
err := srv.Shutdown(context.Background())
if err != nil {
// Error from closing listeners, or context timeout
log.Error().Stack().Err(err).Msg("")
os.Exit(1)
}
close(shutdown)
}()
log.Info().Msgf("listening on %s", h.Config.Addr())
err = errors.WithStack(srv.ListenAndServe())
if err.Error() != http.ErrServerClosed.Error() {
log.Error().Stack().Err(err).Msg("")
os.Exit(1)
}
<-shutdown
log.Info().Msg("bye!")
os.Exit(0)
}