summaryrefslogtreecommitdiff
path: root/api/utils.go
blob: 0fc7d35ed3d21f79abfc9d94190b3b321b7abdae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package api

import (
	"encoding/json"
	"fmt"
	"io"
	"log"
	"net/http"
)

func unmarshal(r io.Reader, v any) error {
	body, err := io.ReadAll(r)
	if err != nil {
		return err
	}

	err = json.Unmarshal(body, v)
	if err != nil {
		return err
	}

	return nil
}

func handleError(err error, rw http.ResponseWriter, status int) {
	log.Println(err)

	rw.WriteHeader(status)
	json := fmt.Sprintf(`{"Error": "%v"}`, http.StatusText(status))
	rw.Write([]byte(json))
}