Commit 34436d11e24884e190b5addb8fb575ad2676eb3f

Authored by Marko Tikvić
1 parent 932f814613
Exists in master and in 1 other branch v2

in-out http logs are written at the same time

Showing 2 changed files with 29 additions and 7 deletions   Show diff stats
... ... @@ -6,6 +6,24 @@ import (
6 6 "net/http"
7 7 )
8 8  
  9 +type ResponseWriterStatusRecorder struct {
  10 + http.ResponseWriter
  11 + status int
  12 +}
  13 +
  14 +func WrapWithStatusRecorder(w http.ResponseWriter) *ResponseWriterStatusRecorder {
  15 + return &ResponseWriterStatusRecorder{w, 0}
  16 +}
  17 +
  18 +func (r *ResponseWriterStatusRecorder) WriteHeader(code int) {
  19 + r.status = code
  20 + r.ResponseWriter.WriteHeader(code)
  21 +}
  22 +
  23 +func (r *ResponseWriterStatusRecorder) Status() int {
  24 + return r.status
  25 +}
  26 +
9 27 // NotFoundHandlerFunc writes HTTP error 404 to w.
10 28 func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
11 29 SetDefaultHeaders(w)
... ... @@ -19,11 +37,11 @@ func NotFoundHandlerFunc(w http.ResponseWriter, req *http.Request) {
19 37 func SetDefaultHeaders(w http.ResponseWriter) {
20 38 w.Header().Set("Access-Control-Allow-Origin", "*")
21 39 w.Header().Set("Access-Control-Allow-Methods", "POST, GET, PUT, DELETE, OPTIONS")
22   - w.Header().Set("Access-Control-Allow-Headers", `Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization`)
  40 + w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
23 41 w.Header().Set("Content-Type", "application/json; charset=utf-8")
24 42 }
25 43  
26   -func ReqLocale(req *http.Request, dflt string) string {
  44 +func GetLocale(req *http.Request, dflt string) string {
27 45 loc := req.FormValue("locale")
28 46 if loc == "" {
29 47 return dflt
... ...
... ... @@ -28,21 +28,25 @@ func ParseForm(h http.HandlerFunc) http.HandlerFunc {
28 28 }
29 29 }
30 30  
31   -var reqLogger *gologger.Logger
  31 +var trafficLogger *gologger.Logger
32 32  
33 33 func EnableLogging(log string) error {
34 34 var err error
35   - reqLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
  35 + trafficLogger, err = gologger.New(log, gologger.MaxLogSize5MB)
36 36 return err
37 37 }
38 38  
39 39 func Log(h http.HandlerFunc) http.HandlerFunc {
40 40 return func(w http.ResponseWriter, req *http.Request) {
41   - reqLogger.LogRequest(req, "")
  41 + in := trafficLogger.RequestLog(req, "")
  42 +
  43 + w2 := WrapWithStatusRecorder(w)
42 44 t1 := time.Now()
43   - h(w, req)
  45 + h(w2, req)
44 46 t2 := time.Now()
45   - reqLogger.LogResponse(w, req, t2.Sub(t1))
  47 +
  48 + out := trafficLogger.ResponseLog(w2.Status(), t2.Sub(t1))
  49 + trafficLogger.LogHTTPTraffic(in, out)
46 50 }
47 51 }
48 52  
... ...