Add TO Golang access log for proxied requests

Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/9d24e39f
Tree: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/9d24e39f
Diff: 
http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/9d24e39f

Branch: refs/heads/master
Commit: 9d24e39fb3a7e6f4d6b71d5817697b7da70607e3
Parents: d50d93e
Author: Robert Butts <robert.o.bu...@gmail.com>
Authored: Wed Jul 26 17:49:38 2017 -0600
Committer: Dewayne Richardson <dewr...@apache.org>
Committed: Thu Aug 10 09:46:03 2017 -0600

----------------------------------------------------------------------
 traffic_ops/traffic_ops_golang/routes.go   |  4 +++-
 traffic_ops/traffic_ops_golang/wrappers.go | 19 +++++++++++++------
 2 files changed, 16 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/9d24e39f/traffic_ops/traffic_ops_golang/routes.go
----------------------------------------------------------------------
diff --git a/traffic_ops/traffic_ops_golang/routes.go 
b/traffic_ops/traffic_ops_golang/routes.go
index e744086..8ad994e 100644
--- a/traffic_ops/traffic_ops_golang/routes.go
+++ b/traffic_ops/traffic_ops_golang/routes.go
@@ -46,7 +46,9 @@ func getRootHandler(d ServerData) http.Handler {
        }
        rp := httputil.NewSingleHostReverseProxy(d.TOURL)
        rp.Transport = tr
-       return rp
+
+       loggingProxyHandler := wrapAccessLog(d.TOSecret, rp)
+       return loggingProxyHandler
 }
 
 // GetRoutes returns the map of regex routes, and a catchall route for when no 
regex matches.

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/9d24e39f/traffic_ops/traffic_ops_golang/wrappers.go
----------------------------------------------------------------------
diff --git a/traffic_ops/traffic_ops_golang/wrappers.go 
b/traffic_ops/traffic_ops_golang/wrappers.go
index 17f3e1c..5a5cfce 100644
--- a/traffic_ops/traffic_ops_golang/wrappers.go
+++ b/traffic_ops/traffic_ops_golang/wrappers.go
@@ -90,15 +90,22 @@ func wrapAuth(h RegexHandlerFunc, noAuth bool, secret 
string, privLevelStmt *sql
 
 const AccessLogTimeFormat = "02/Jan/2006:15:04:05 -0700"
 
-func wrapLogTime(h RegexHandlerFunc) RegexHandlerFunc {
-       return func(w http.ResponseWriter, r *http.Request, p ParamMap) {
-               start := time.Now()
+func wrapAccessLog(secret string, h http.Handler) http.HandlerFunc {
+       return func(w http.ResponseWriter, r *http.Request) {
                iw := &Interceptor{w: w}
+               user := "-"
+               cookie, err := r.Cookie(tocookie.Name)
+               if err == nil && cookie != nil {
+                       cookie, err := tocookie.Parse(secret, cookie.Value)
+                       if err == nil {
+                               user = cookie.AuthData
+                       }
+               }
+               start := time.Now()
                defer func() {
-                       user := "-" // TODO fix
-                       log.EventfRaw(`%s - %s [%s] "%v %v HTTP/1.1" %v 0 0 
"%v"\n`, r.RemoteAddr, user, time.Now().Format(AccessLogTimeFormat), r.Method, 
r.URL.Path, iw.code, time.Now().Sub(start)/time.Millisecond, iw.byteCount, 
r.UserAgent())
+                       log.EventfRaw(`%s - %s [%s] "%v %v HTTP/1.1" %v %v %v 
"%v"`, r.RemoteAddr, user, time.Now().Format(AccessLogTimeFormat), r.Method, 
r.URL.Path, iw.code, iw.byteCount, int(time.Now().Sub(start)/time.Millisecond), 
r.UserAgent())
                }()
-               h(iw, r, p)
+               h.ServeHTTP(iw, r)
        }
 }
 

Reply via email to