- refactor jxcontext
This commit is contained in:
57
business/auth2/auth_ctx.go
Normal file
57
business/auth2/auth_ctx.go
Normal 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
|
||||
}
|
||||
@@ -19,12 +19,9 @@ type IAuther interface {
|
||||
}
|
||||
|
||||
type Context struct {
|
||||
token string
|
||||
accessUUID string
|
||||
userInfo IAuther //*auth.LoginInfo
|
||||
w http.ResponseWriter
|
||||
r *http.Request
|
||||
mapData map[interface{}]interface{}
|
||||
*auth2.Context
|
||||
token string
|
||||
userInfo IAuther //*auth.LoginInfo
|
||||
}
|
||||
|
||||
const (
|
||||
@@ -41,22 +38,16 @@ func init() {
|
||||
|
||||
func NewWithUserName(notUsed interface{}, userName string, w http.ResponseWriter, r *http.Request) (ctx *Context) {
|
||||
ctx = &Context{
|
||||
token: userName,
|
||||
w: w,
|
||||
r: r,
|
||||
mapData: make(map[interface{}]interface{}),
|
||||
accessUUID: utils.GetUUID(),
|
||||
token: userName,
|
||||
Context: auth2.NewContext(w, r),
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func New(notUsed interface{}, token string, w http.ResponseWriter, r *http.Request) (ctx *Context, errCode string, err error) {
|
||||
ctx = &Context{
|
||||
token: token,
|
||||
w: w,
|
||||
r: r,
|
||||
mapData: make(map[interface{}]interface{}),
|
||||
accessUUID: utils.GetUUID(),
|
||||
token: token,
|
||||
Context: auth2.NewContext(w, r),
|
||||
}
|
||||
globals.SugarLogger.Debugf("jxcontext New, token:%s", token)
|
||||
if auth2.IsV2Token(token) {
|
||||
@@ -118,10 +109,6 @@ func (ctx *Context) GetToken() string {
|
||||
return ctx.token
|
||||
}
|
||||
|
||||
func (ctx *Context) GetAccessUUID() string {
|
||||
return ctx.accessUUID
|
||||
}
|
||||
|
||||
func (ctx *Context) GetLoginInfo() IAuther {
|
||||
return ctx.userInfo
|
||||
}
|
||||
@@ -134,15 +121,3 @@ func (ctx *Context) GetV2AuthInfo() (authInfo *auth2.AuthInfo, err error) {
|
||||
}
|
||||
return nil, auth2.ErrNeedV2Token
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user