我遇到了一些麻烦后,杜松子酒gonic书,这有助于到目前为止顺利移动杜松子酒。当我到了会议的一部分,似乎我击中了一个拦网。
我正在使用:
- macOS ventura上的macbook pro m1
- 转到1.21.1
- 杜松子酒1.9.1
- gin-contrib/sessions v0.0.5
我从gin-contrib/session开始使用以下代码
//main.go
import (
"context"
"github.com/gin-gonic/contrib/sessions"
//redisStore "github.com/gin-contrib/sessions/redis"
"github.com/gin-gonic/gin"
)
router := gin.Default()
store, _ := sessions.NewRedisStore(10, "tcp", "localhost:6379", "", []byte("secret"))
//store, _ := redisStore.NewStore(10, "tcp", "localhost:6379", "", []byte("secret"))
router.Use(sessions.Sessions("recipes_api", store))
//auth handler
import (
//...
"crypto/sha256"
"github.com/dgrijalva/jwt-go"
//"github.com/gin-contrib/sessions"
sessions2 "github.com/gin-gonic/contrib/sessions"
//...
)
func (handler *AuthHandler) SignInHandler(c *gin.Context) {
//...
if cur.Err() != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid username or password"})
return
}
sessionToken := xid.New().String()
session := sessions2.Default(c)
session.Set("username", user.Username)
session.Set("token", sessionToken)
session.Save()
c.JSON(http.StatusOK, gin.H{"message": "User signed in"})
}
上面的代码似乎在session.Default(c)或session2.Default(c)中中断了,所以无论我使用的是来自"github.com/gin-gonic/contrib/sessions"
还是来自"github.com/gin-contrib/sessions"
的session,它总是找不到键。它抛出如下错误
Key "github.com/gin-gonic/contrib/sessions" does not exist
~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:273 (0x10484b67f)
(*Context).MustGet: panic("Key \"" + key + "\" does not exist")
~/go/pkg/mod/github.com/gin-gonic/[email protected]/sessions/sessions.go:146 (0x10484b61c)
Default: return c.MustGet(DefaultKey).(Session)
~/Tutorials/distributed-golang-app-gin/handlers/auth.go:59 (0x104995dc7)
(*AuthHandler).SignInHandler: session := sessions2.Default(c)
~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104845c5f)
(*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0x104845c44)
CustomRecoveryWithWriter.func1: c.Next()
~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104844fff)
(*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:240 (0x104844fd0)
LoggerWithConfig.func1: c.Next()
~/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:174 (0x104844133)
(*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:620 (0x104843e5c)
(*Engine).handleHTTPRequest: c.Next()
~/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:576 (0x104843baf)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/opt/homebrew/opt/go/libexec/src/net/http/server.go:2938 (0x1046d95fb)
serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/opt/homebrew/opt/go/libexec/src/net/http/server.go:2009 (0x1046d6777)
(*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/opt/homebrew/opt/go/libexec/src/runtime/asm_arm64.s:1197 (0x104520b63)
goexit: MOVD R0, R0 // NOP
我在这里做错了一些事情,如果有人能为我提供一些帮助,我会很感激,这样我就可以向前推进了。谢谢
1条答案
按热度按时间wqlqzqxt1#
问题已解决。我的IDE自动导入了错误的会话引用。我已经手动删除了整个项目中对“github.com/gin-gonic/contrib/sessions“的引用。