Created response structs and a json response util.

This commit is contained in:
2026-05-01 07:50:31 +02:00
parent 85b83a0741
commit 6f9e37b69a
4 changed files with 71 additions and 49 deletions
+16
View File
@@ -2,9 +2,25 @@ package web
import (
"context"
"encoding/json"
"log"
"net/http"
"strconv"
)
// Server will respond with an expected struct of information.
// Otherwise will return the correct error status code.
// Client responsibility to check status then unmarshal to
// error response or data
func WriteJSON[T any](w http.ResponseWriter, status int, data T) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("JSON encode error %v", err)
}
}
func userIDFromContext(ctx context.Context) (int64, bool) {
val := ctx.Value(userIDKey)
if val == nil {