/
main.go
59 lines (46 loc) · 1.25 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
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
nlogrus "github.com/meatballhat/negroni-logrus"
"github.com/phyber/negroni-gzip/gzip"
"github.com/sirupsen/logrus"
"github.com/unrolled/render"
nsecure "github.com/unrolled/secure"
"github.com/urfave/negroni"
)
func main() {
renderer := render.New(render.Options{
Directory: "templates",
})
mux := http.NewServeMux()
mux.HandleFunc("GET /health", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "healthy")
})
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
_ = renderer.HTML(w, http.StatusNotFound, "404", nil)
})
level := logrus.InfoLevel
n := negroni.New()
n.Use(negroni.NewRecovery())
n.Use(nlogrus.NewCustomMiddleware(level, &logrus.JSONFormatter{}, "web"))
n.Use(gzip.Gzip(gzip.DefaultCompression))
n.Use(negroni.HandlerFunc(nsecure.New().HandlerFuncWithNext))
n.UseHandler(mux)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer cancel()
server := &http.Server{Addr: ":8080", Handler: n}
go func() {
_ = server.ListenAndServe()
}()
<-ctx.Done()
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
os.Exit(0)
}