Use async middleware

This commit is contained in:
Eduard Urbach 2019-06-17 08:31:57 +09:00
parent 8e30f3e7c0
commit 941820ea64
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0

View File

@ -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.