forked from emicklei/go-restful
/
aspects.go
72 lines (64 loc) · 1.91 KB
/
aspects.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
package restful
import (
"log"
"net/http"
"net/http/pprof"
"strconv"
"strings"
"github.com/abhiunc/go-restful"
)
//Creates Logger before RouteFunction is executed via wrapping
func LoggingAspect(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
log.Printf("[WebserviceLogging] {%s}, \n%s \n%s \n%s \n%s \n%s\n",
strings.Split(r.Request.RemoteAddr, ":")[0],
"[Method]: "+r.Request.Method,
"[URL]: "+r.Request.URL.String(),
"[Protocol]: "+r.Request.Proto,
"[Status]: "+strconv.Itoa(w.StatusCode()),
"[Length]: "+strconv.Itoa(w.ContentLength()),
)
fn(r, w)
}
}
//Creates Authentication at RouteFunction level.
func AuthAspect(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
token := r.HeaderParameter("X-AUTH-TOKEN")
if token == "" {
http.Error(w, "missing auth token", http.StatusUnauthorized)
return
}
//Write own authentication mechanism here.
fn(r, w)
}
}
//runs Index profiling on wrapped RouteFunction
func IndexHandler(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
pprof.Index(w.ResponseWriter, r.Request)
fn(r, w)
}
}
//Trace profiler on wrapper RouteFunction
func TraceHandler(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
pprof.Trace(w.ResponseWriter, r.Request)
fn(r, w)
}
}
//General Profiling on RouteFunction
func ProfileHandler(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
pprof.Profile(w.ResponseWriter, r.Request)
fn(r, w)
}
}
//Symbol Profiling on RouteFunction
func SymbolHandler(fn restful.RouteFunction) restful.RouteFunction {
return func(r *restful.Request, w *restful.Response) {
//c := pprof.Symbol(w.ResponseWriter, r.Request)
log.Printf(" ")
fn(r, w)
}
}