32 lines
No EOL
813 B
Go
32 lines
No EOL
813 B
Go
package globaleuserdata
|
|
|
|
import (
|
|
"github.com/flamego/flamego"
|
|
"github.com/flamego/session"
|
|
"github.com/flamego/template"
|
|
"github.com/jmoiron/sqlx"
|
|
"github.com/rs/zerolog/log"
|
|
"reflect"
|
|
)
|
|
|
|
func GlobalUserData(db *sqlx.DB) flamego.Handler {
|
|
log.Debug().Msg("Handler")
|
|
return flamego.ContextInvoker(func(c flamego.Context) {
|
|
r := c.Value(reflect.TypeOf(template.Data{})).Interface().(template.Data)
|
|
s := c.Value(reflect.TypeOf((*session.Session)(nil)).Elem()).Interface().(session.Session)
|
|
|
|
userId := s.Get("user_id")
|
|
r["g_user_id"] = userId
|
|
if userId != nil {
|
|
r["g_loggedin"] = true
|
|
log.Debug().Msg("User Data Query")
|
|
|
|
re := []struct{
|
|
Username string
|
|
}{}
|
|
db.Select(&re, "SELECT username FROM users WHERE id = ?", userId)
|
|
r["g_userName"] = re[0].Username
|
|
}
|
|
c.Next()
|
|
})
|
|
} |