Skip to content

Commit a72b59d

Browse files
committed
Play with session cache in memory
1 parent d761112 commit a72b59d

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

internal/app/handlers/interface.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"ctf01d/internal/app/server"
55
"database/sql"
66
"net/http"
7+
"sync"
78

89
openapi_types "github.com/oapi-codegen/runtime/types"
910
)
@@ -12,7 +13,6 @@ type Handlers struct {
1213
DB *sql.DB
1314
}
1415

15-
// ServerInterfaceWrapper wraps Handlers to conform to the generated interface
1616
type ServerInterfaceWrapper struct {
1717
handlers *Handlers
1818
}
@@ -21,6 +21,26 @@ func NewServerInterfaceWrapper(handlers *Handlers) *ServerInterfaceWrapper {
2121
return &ServerInterfaceWrapper{handlers: handlers}
2222
}
2323

24+
type SessionCache struct {
25+
cache sync.Map
26+
}
27+
28+
func NewSessionCache() *SessionCache {
29+
return &SessionCache{}
30+
}
31+
32+
type SessionServerInterfaceWrapper struct {
33+
handlers *Handlers
34+
sessionCache *SessionCache
35+
}
36+
37+
func NewSessionServerInterfaceWrapper(handlers *Handlers) *SessionServerInterfaceWrapper {
38+
return &SessionServerInterfaceWrapper{
39+
handlers: handlers,
40+
sessionCache: NewSessionCache(),
41+
}
42+
}
43+
2444
func (siw *ServerInterfaceWrapper) ListGames(w http.ResponseWriter, r *http.Request) {
2545
siw.handlers.ListGames(w, r)
2646
}
@@ -49,7 +69,7 @@ func (siw *ServerInterfaceWrapper) PostApiV1AuthSignOut(w http.ResponseWriter, r
4969
siw.handlers.PostApiV1AuthSignOut(w, r)
5070
}
5171

52-
func (siw *ServerInterfaceWrapper) ValidateSession(w http.ResponseWriter, r *http.Request) {
72+
func (siw *SessionServerInterfaceWrapper) ValidateSession(w http.ResponseWriter, r *http.Request) {
5373
siw.handlers.ValidateSession(w, r)
5474
}
5575

internal/app/handlers/sessions.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,31 @@ import (
1010
api_helpers "ctf01d/internal/app/utils"
1111
"ctf01d/internal/app/view"
1212

13+
"github.com/google/uuid"
1314
openapi_types "github.com/oapi-codegen/runtime/types"
1415
)
1516

17+
type SessionHandler struct {
18+
*Handlers
19+
SessionCache *SessionCache
20+
}
21+
22+
func (sc *SessionCache) GetSession(sessionID string) (openapi_types.UUID, bool) {
23+
val, ok := sc.cache.Load(sessionID)
24+
if !ok {
25+
return uuid.Nil, false
26+
}
27+
return val.(openapi_types.UUID), true
28+
}
29+
30+
func (sc *SessionCache) SetSession(sessionID string, userID uuid.UUID) {
31+
sc.cache.Store(sessionID, userID)
32+
}
33+
34+
func (sc *SessionCache) DeleteSession(sessionID string) {
35+
sc.cache.Delete(sessionID)
36+
}
37+
1638
func (h *Handlers) PostApiV1AuthSignIn(w http.ResponseWriter, r *http.Request) {
1739
var req server.PostApiV1AuthSignInJSONBody
1840
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
@@ -72,13 +94,20 @@ func (h *Handlers) PostApiV1AuthSignOut(w http.ResponseWriter, r *http.Request)
7294
api_helpers.RespondWithJSON(w, http.StatusOK, map[string]string{"data": "User logout successful"})
7395
}
7496

75-
func (h *Handlers) ValidateSession(w http.ResponseWriter, r *http.Request) {
97+
func (h *SessionHandler) ValidateSession(w http.ResponseWriter, r *http.Request) {
7698
cookie, err := r.Cookie("session_id")
7799
if err != nil {
78100
slog.Warn(err.Error(), "handler", "ValidateSession")
79101
api_helpers.RespondWithJSON(w, http.StatusUnauthorized, map[string]string{"error": "No session found"})
80102
return
81103
}
104+
105+
if userId, ok := h.SessionCache.GetSession(cookie.Value); ok {
106+
slog.Debug("ValidateSession user.Id " + openapi_types.UUID(userId).String())
107+
h.respondWithUserDetails(w, r, userId)
108+
return
109+
}
110+
82111
slog.Debug("cookie.Value, " + cookie.Value)
83112
repo := repository.NewSessionRepository(h.DB)
84113
var userId openapi_types.UUID
@@ -88,12 +117,17 @@ func (h *Handlers) ValidateSession(w http.ResponseWriter, r *http.Request) {
88117
api_helpers.RespondWithJSON(w, http.StatusUnauthorized, map[string]string{"error": "No user or session found"})
89118
return
90119
}
120+
121+
h.SessionCache.SetSession(cookie.Value, userId)
91122
slog.Debug("ValidateSession user.Id " + openapi_types.UUID(userId).String())
123+
h.respondWithUserDetails(w, r, userId)
124+
}
92125

126+
func (h *SessionHandler) respondWithUserDetails(w http.ResponseWriter, r *http.Request, userId openapi_types.UUID) {
93127
userRepo := repository.NewUserRepository(h.DB)
94128
user, err := userRepo.GetById(r.Context(), userId)
95129
if err != nil {
96-
slog.Warn(err.Error(), "handler", "ValidateSession")
130+
slog.Warn(err.Error(), "handler", "respondWithUserDetails")
97131
api_helpers.RespondWithJSON(w, http.StatusInternalServerError, map[string]string{"error": "Could not find user by user id"})
98132
return
99133
}

0 commit comments

Comments
 (0)