shelves/backend/httpx/httpx.go
2024-11-15 13:34:42 -05:00

40 lines
895 B
Go

package httpx
import (
"log"
"net/http"
"net/url"
"shelves/backend/errorsx"
"shelves/backend/urls"
)
func SeeOther(w http.ResponseWriter, location string) {
w.Header().Add("Location", location)
w.WriteHeader(http.StatusSeeOther)
}
func TemporaryRedirect(w http.ResponseWriter, location string) {
w.Header().Add("Location", location)
w.WriteHeader(http.StatusTemporaryRedirect)
}
func Unauthorized(w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
}
func BadRequest(w http.ResponseWriter) {
w.WriteHeader(http.StatusBadRequest)
}
func InternalServerError(w http.ResponseWriter, err error) {
log.Printf("Internal server error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(errorsx.String(err)))
}
func LoginRedirect(w http.ResponseWriter, redirectTo url.URL) {
location := urls.Login(redirectTo.Path)
TemporaryRedirect(w, location)
}