-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
56 lines (48 loc) · 1.23 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
package main
import (
"flag"
"net/http"
"os"
"time"
"curator/injection"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/rs/zerolog"
"github.com/rs/zerolog/hlog"
"github.com/rs/zerolog/log"
)
// Command line flags.
var (
addr = flag.String("addr", ":8080", "bind address")
)
func main() {
flag.Parse()
log.Logger = zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).With().
Timestamp().Logger().Level(zerolog.InfoLevel)
router, err := injection.Inject()
configureLogHandler(router)
if err != nil {
log.Error().Err(err)
}
if *addr == "" {
log.Error().Msg("Please provide -addr")
os.Exit(1)
}
log.Info().Msg("Startup complete. Serving requests on port " + *addr)
http.ListenAndServe(*addr, handlers.RecoveryHandler()(router))
}
func configureLogHandler(r *mux.Router) {
logHandler := hlog.NewHandler(log.Logger)
r.Use(logHandler)
r.Use(hlog.AccessHandler((func(r *http.Request, status, size int, duration time.Duration) {
hlog.FromRequest(r).Info().
Str("method", r.Method).
Str("url", r.URL.String()).
Int("status", status).
Dur("duration", duration).
Msg("")
})))
r.Use(hlog.RemoteAddrHandler("ip"))
r.Use(hlog.UserAgentHandler("user_agent"))
r.Use(hlog.RefererHandler("referer"))
}