diff --git a/context.go b/context.go index a134dca..9d23f22 100644 --- a/context.go +++ b/context.go @@ -5,6 +5,7 @@ import ( . "code.hoteas.com/golang/hotime/db" "encoding/json" "net/http" + "strings" "time" ) @@ -74,6 +75,16 @@ func (that *Context) View() { if that.Session("user_id").Data != nil { that.Log["user_id"] = that.Session("user_id").ToCeilInt() } + ipStr := Substr(that.Req.RemoteAddr, 0, strings.Index(that.Req.RemoteAddr, ":")) + //负载均衡优化 + if ipStr == "127.0.0.1" { + if that.Req.Header.Get("X-Forwarded-For") != "" { + ipStr = that.Req.Header.Get("X-Forwarded-For") + } else if that.Req.Header.Get("X-Real-IP") != "" { + ipStr = that.Req.Header.Get("X-Real-IP") + } + } + that.Log["ip"] = ipStr that.Db.Insert("logs", that.Log) }