This repository was archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
114 lines (92 loc) · 2.5 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
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
package main
import (
"context"
"log"
"net/http"
"sync/atomic"
"time"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/pressly/lg"
"github.com/sirupsen/logrus"
)
func main() {
// Setup the logger
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
lg.RedirectStdlogOutput(logger)
lg.DefaultLogger = logger
lg.Infoln("Welcome")
serverCtx := context.Background()
serverCtx = lg.WithLoggerContext(serverCtx, logger)
lg.Log(serverCtx).Infof("Booting up server, %s", "v1.0")
r := chi.NewRouter()
r.Use(middleware.RequestID)
r.Use(lg.RequestLogger(logger))
// r.Use(lg.PrintPanics)
r.Use(Counter)
r.Get("/", Index)
r.Route("/articles", func(r chi.Router) {
r.Use(ArticleCtx)
r.With(PaginateCtx).Get("/", List)
r.Get("/search", Search)
})
r.Get("/stdlog", Stdlog)
r.Get("/fatal", Fatal)
r.Get("/panic", Panic)
go func() {
for {
time.Sleep(1 * time.Second)
lg.Log(serverCtx).Infof("tick")
}
}()
service := chi.ServerBaseContext(serverCtx, r)
http.ListenAndServe(":3333", service)
}
var counter = uint64(0)
func Counter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
atomic.AddUint64(&counter, 1)
lg.SetEntryField(r.Context(), "count", counter)
next.ServeHTTP(w, r)
})
}
func ArticleCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lg.RequestLog(r).Warnf("inside ArticleCtx middleware")
lg.SetRequestEntryField(r, "article", 123)
next.ServeHTTP(w, r)
})
}
func PaginateCtx(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
lg.RequestLog(r).Warnf("inside PaginateCtx middleware")
lg.SetEntryField(r.Context(), "paginate", true)
next.ServeHTTP(w, r)
})
}
func Index(w http.ResponseWriter, r *http.Request) {
log := lg.Log(r.Context())
log.Info("index")
w.Write([]byte("index"))
}
func List(w http.ResponseWriter, r *http.Request) {
log := lg.RequestLog(r)
log.Info("articles list")
w.Write([]byte("list"))
}
func Search(w http.ResponseWriter, r *http.Request) {
log := lg.RequestLog(r)
log.Info("articles search")
w.Write([]byte("search"))
}
func Stdlog(w http.ResponseWriter, r *http.Request) {
log.Println("logging from stdlib log to logrus")
w.Write([]byte("piping from the stdlib log pkg"))
}
func Fatal(w http.ResponseWriter, r *http.Request) {
lg.RequestLog(r).Fatal("boom")
}
func Panic(w http.ResponseWriter, r *http.Request) {
panic("oh no")
}