package api import ( "encoding/json" "fmt" "log" "net/http" "os" "time" ) type ( JSONBase struct { Status int `json:"status"` Timestamp time.Time `json:"timestamp"` } JSONError struct { JSONBase Message string } ) var ( errLog = log.New(os.Stderr, log.Prefix(), log.Flags()) ) func internalServerErrorJSON(w http.ResponseWriter, v any) { w.WriteHeader(http.StatusInternalServerError) body, err := json.Marshal(JSONError{ JSONBase: JSONBase{ Status: http.StatusInternalServerError, Timestamp: time.Now(), }, Message: fmt.Sprintf("%v", v), }) if err != nil { errLog.Println("[ERROR]", err) } w.Write([]byte(body)) } func notFoundJSON(w http.ResponseWriter) { w.WriteHeader(http.StatusNotFound) body, err := json.Marshal(JSONError{ JSONBase: JSONBase{ Status: http.StatusNotFound, Timestamp: time.Now(), }, Message: "404 page not found", }) if err != nil { errLog.Println("[ERROR]", err) } w.Write([]byte(body)) }