Created response structs and a json response util.
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user