summaryrefslogblamecommitdiff
path: root/api/utils.go
blob: 0fc7d35ed3d21f79abfc9d94190b3b321b7abdae (plain) (tree)
1
2
3
4
5
6
7
8



                       
             
            

                  















                                          

                                                                 
 


                                                                       
 
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))
}