增加字段:用户是否点赞
This commit is contained in:
24
bin/main.go
24
bin/main.go
@@ -63,7 +63,29 @@ func LogComponent(startTime int64, r *http.Request) {
|
||||
func LogRequest(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer LogComponent(time.Now().UnixNano(), r) // 最后打印日志
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
var user_id int
|
||||
if token := r.Header.Get("token"); token != "" {
|
||||
fmt.Println("token:", token)
|
||||
rows, err := mysqlConnection.Database.Query("SELECT user_id FROM web_auth WHERE token = ? LIMIT 1", token)
|
||||
if err != nil {
|
||||
log.Println("查询失败:", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
if rows.Next() {
|
||||
err = rows.Scan(&user_id)
|
||||
if err != nil {
|
||||
log.Println("扫描失败:", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), "user_id", user_id)
|
||||
fmt.Println("user_id:", user_id)
|
||||
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user