2023-12-29 19:10:31 +01:00
|
|
|
package handler
|
2023-12-16 12:11:16 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2023-12-16 14:45:38 +01:00
|
|
|
"log/slog"
|
2023-12-16 12:11:16 +01:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-12-29 19:10:31 +01:00
|
|
|
type ContextKey string
|
|
|
|
|
|
|
|
const (
|
|
|
|
MovieKey = ContextKey("movie")
|
|
|
|
)
|
|
|
|
|
2023-12-16 12:11:16 +01:00
|
|
|
func Index(w http.ResponseWriter) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
fmt.Fprint(w, `{"message":"emdb index"}`)
|
|
|
|
}
|
|
|
|
|
2023-12-16 14:45:38 +01:00
|
|
|
func Error(w http.ResponseWriter, status int, message string, err error, logger *slog.Logger) {
|
|
|
|
logger.Error(message, "error", err)
|
|
|
|
|
2023-12-16 12:11:16 +01:00
|
|
|
w.WriteHeader(status)
|
|
|
|
|
|
|
|
var resBody []byte
|
|
|
|
res := struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
Error string `json:"error"`
|
|
|
|
}{
|
|
|
|
Message: message,
|
|
|
|
Error: err.Error(),
|
|
|
|
}
|
|
|
|
resBody, _ = json.Marshal(res)
|
|
|
|
|
|
|
|
fmt.Fprint(w, string(resBody))
|
|
|
|
}
|