Use async middleware
This commit is contained in:
parent
8e30f3e7c0
commit
941820ea64
@ -32,18 +32,19 @@ func UserInfo(next aero.Handler) aero.Handler {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
updateUserInfo(ctx, user)
|
// Bind local variables and start a coroutine
|
||||||
|
ip := ctx.IP()
|
||||||
|
userAgent := ctx.Request().Header("User-Agent")
|
||||||
|
go updateUserInfo(ip, userAgent, user)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update browser and OS data
|
// Update browser and OS data
|
||||||
func updateUserInfo(ctx aero.Context, user *arn.User) {
|
func updateUserInfo(ip string, userAgent string, user *arn.User) {
|
||||||
newIP := ctx.IP()
|
if user.UserAgent != userAgent {
|
||||||
newUserAgent := ctx.Request().Header("User-Agent")
|
user.UserAgent = userAgent
|
||||||
|
|
||||||
if user.UserAgent != newUserAgent {
|
|
||||||
user.UserAgent = newUserAgent
|
|
||||||
|
|
||||||
// Parse user agent
|
// Parse user agent
|
||||||
parsed := user_agent.New(user.UserAgent)
|
parsed := user_agent.New(user.UserAgent)
|
||||||
@ -60,9 +61,11 @@ func updateUserInfo(ctx aero.Context, user *arn.User) {
|
|||||||
user.LastSeen = arn.DateTimeUTC()
|
user.LastSeen = arn.DateTimeUTC()
|
||||||
user.Save()
|
user.Save()
|
||||||
|
|
||||||
if user.IP != newIP {
|
if user.IP == ip {
|
||||||
go updateUserLocation(user, newIP)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
updateUserLocation(user, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Updates the location of the user.
|
// Updates the location of the user.
|
||||||
|
Loading…
Reference in New Issue
Block a user