From 478f42f8a9f070cef9e8c0b81dc7bd4c24bacde8 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 15 Nov 2024 23:48:35 +0100 Subject: chore(tfstated): use a struct{} as context.Context key --- cmd/tfstated/post.go | 2 +- pkg/basic_auth/middleware.go | 3 ++- pkg/model/account.go | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/cmd/tfstated/post.go b/cmd/tfstated/post.go index 718fed7..674eaba 100644 --- a/cmd/tfstated/post.go +++ b/cmd/tfstated/post.go @@ -25,7 +25,7 @@ func handlePost(db *database.DB) http.Handler { _ = errorResponse(w, http.StatusBadRequest, err) return } - account := r.Context().Value("account").(*model.Account) + account := r.Context().Value(model.AccountContextKey{}).(*model.Account) if idMismatch, err := db.SetState(r.URL.Path, account.Id, data, id); err != nil { if idMismatch { _ = errorResponse(w, http.StatusConflict, err) diff --git a/pkg/basic_auth/middleware.go b/pkg/basic_auth/middleware.go index 108124f..94cac56 100644 --- a/pkg/basic_auth/middleware.go +++ b/pkg/basic_auth/middleware.go @@ -6,6 +6,7 @@ import ( "time" "git.adyxax.org/adyxax/tfstated/pkg/database" + "git.adyxax.org/adyxax/tfstated/pkg/model" ) func Middleware(db *database.DB) func(http.Handler) http.Handler { @@ -32,7 +33,7 @@ func Middleware(db *database.DB) func(http.Handler) http.Handler { http.Error(w, "Internal Server Error", http.StatusInternalServerError) return } - ctx := context.WithValue(r.Context(), "account", account) + ctx := context.WithValue(r.Context(), model.AccountContextKey{}, account) next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/pkg/model/account.go b/pkg/model/account.go index cbb6407..86032b8 100644 --- a/pkg/model/account.go +++ b/pkg/model/account.go @@ -2,6 +2,8 @@ package model import "time" +type AccountContextKey struct{} + type Account struct { Id int Username string -- cgit v1.2.3