chore(tfstated): use a struct{} as context.Context key
This commit is contained in:
parent
3d8812fbd0
commit
478f42f8a9
3 changed files with 5 additions and 2 deletions
|
@ -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)
|
||||
|
|
|
@ -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))
|
||||
})
|
||||
}
|
||||
|
|
|
@ -2,6 +2,8 @@ package model
|
|||
|
||||
import "time"
|
||||
|
||||
type AccountContextKey struct{}
|
||||
|
||||
type Account struct {
|
||||
Id int
|
||||
Username string
|
||||
|
|
Loading…
Add table
Reference in a new issue