/
main.go
96 lines (84 loc) · 2.48 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
package main
import (
"fmt"
"log"
"net/http"
"time"
logrus "github.com/Sirupsen/logrus"
"github.com/doloopwhile/logrusltsv"
"github.com/go-zoo/bone"
"github.com/rs/xhandler"
"golang.org/x/net/context"
log15 "gopkg.in/inconshreveable/log15.v2"
)
func normalLoggingMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
next.ServeHTTP(w, r)
t2 := time.Now()
log.Printf("access [%s] %q %v\n", r.Method, r.URL.String(), t2.Sub(t1))
}
return http.HandlerFunc(fn)
}
func log15LoggingMiddleware(next http.Handler) http.Handler {
srvlog := log15.New("module", "app/server")
fn := func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
next.ServeHTTP(w, r)
t2 := time.Now()
srvlog.Info("access", "method", r.Method, "path", r.URL.String(), "req_time", t2.Sub(t1))
}
return http.HandlerFunc(fn)
}
func logrusLoggingMiddleware(next http.Handler) http.Handler {
logrus.SetFormatter(&logrusltsv.Formatter{})
fn := func(w http.ResponseWriter, r *http.Request) {
t1 := time.Now()
next.ServeHTTP(w, r)
t2 := time.Now()
logrus.WithFields(logrus.Fields{
"method": r.Method,
"path": r.URL.String(),
"req_time": t2.Sub(t1),
}).Info("access")
}
return http.HandlerFunc(fn)
}
func recoverMiddleware(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Println("panic: %+v", err)
http.Error(w, http.StatusText(500), 500)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func account(ctx context.Context, rw http.ResponseWriter, req *http.Request) {
accountId := bone.GetValue(req, "id")
fmt.Fprintf(rw, "accountId: %s", accountId)
}
func note(ctx context.Context, rw http.ResponseWriter, req *http.Request) {
noteId := bone.GetValue(req, "id")
fmt.Fprintf(rw, "noteId: %s", noteId)
}
func simple(ctx context.Context, rw http.ResponseWriter, req *http.Request) {
fmt.Fprintf(rw, "Hello, world!!")
}
func main() {
c := xhandler.Chain{}
c.Use(recoverMiddleware)
c.Use(normalLoggingMiddleware)
c.Use(log15LoggingMiddleware)
c.Use(logrusLoggingMiddleware)
simpleHandler := xhandler.HandlerFuncC(simple)
accountHandler := xhandler.HandlerFuncC(account)
noteHandler := xhandler.HandlerFuncC(note)
mux := bone.New()
mux.Get("/account/:id", c.Handler(accountHandler))
mux.Get("/note/:id", c.Handler(noteHandler))
mux.Get("/simple", c.Handler(simpleHandler))
http.ListenAndServe(":8080", mux)
}