- refactor jxcontext

This commit is contained in:
gazebo
2019-03-25 14:28:12 +08:00
parent 4e2cd5b26c
commit 82cf7cdb22
2 changed files with 64 additions and 32 deletions

View File

@@ -0,0 +1,57 @@
package auth2
import (
"net/http"
"strings"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/globals"
)
type Context struct {
accessUUID string
w http.ResponseWriter
r *http.Request
// mapData map[interface{}]interface{}
}
func NewContext(w http.ResponseWriter, r *http.Request) (ctx *Context) {
ctx = &Context{
w: w,
r: r,
accessUUID: utils.GetUUID(),
}
globals.SugarLogger.Debugf("NewContext ip:%s", ctx.GetRealRemoteIP())
return ctx
}
func (ctx *Context) GetResponseWriter() http.ResponseWriter {
return ctx.w
}
func (ctx *Context) GetRequest() *http.Request {
return ctx.r
}
func (ctx *Context) GetTrackInfo() string {
return ctx.accessUUID
}
// 待删除
func (ctx *Context) GetAccessUUID() string {
return ctx.accessUUID
}
func (ctx *Context) GetRealRemoteIP() (ip string) {
r := ctx.r
if r != nil {
ip = r.Header.Get("X-Forwarded-For")
if ip == "" {
ip = r.Header.Get("X-real-ip")
}
if ip == "" {
ip = strings.Split(r.RemoteAddr, ":")[0]
}
}
return ip
}