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)
|
_ = errorResponse(w, http.StatusBadRequest, err)
|
||||||
return
|
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, err := db.SetState(r.URL.Path, account.Id, data, id); err != nil {
|
||||||
if idMismatch {
|
if idMismatch {
|
||||||
_ = errorResponse(w, http.StatusConflict, err)
|
_ = errorResponse(w, http.StatusConflict, err)
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
"git.adyxax.org/adyxax/tfstated/pkg/database"
|
||||||
|
"git.adyxax.org/adyxax/tfstated/pkg/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func Middleware(db *database.DB) func(http.Handler) http.Handler {
|
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)
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
ctx := context.WithValue(r.Context(), "account", account)
|
ctx := context.WithValue(r.Context(), model.AccountContextKey{}, account)
|
||||||
next.ServeHTTP(w, r.WithContext(ctx))
|
next.ServeHTTP(w, r.WithContext(ctx))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,8 @@ package model
|
||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
type AccountContextKey struct{}
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
Id int
|
Id int
|
||||||
Username string
|
Username string
|
||||||
|
|
Loading…
Add table
Reference in a new issue