Files
spend-sparrow/utils/http.go
Tim Wundenberg fa7119ed33
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 7m40s
Build and Push Docker Image / Build-And-Push-Docker-Image (push) Successful in 3m55s
rename template
2025-03-31 21:50:59 +02:00

38 lines
867 B
Go

package utils
import (
"fmt"
"net/http"
"time"
"spend-sparrow/log"
)
func TriggerToast(w http.ResponseWriter, r *http.Request, class string, message string, statusCode int) {
if isHtmx(r) {
w.Header().Set("HX-Trigger", fmt.Sprintf(`{"toast": "%v|%v"}`, class, message))
w.WriteHeader(statusCode)
} else {
log.Error("Trying to trigger toast in non-HTMX request")
}
}
func DoRedirect(w http.ResponseWriter, r *http.Request, url string) {
if isHtmx(r) {
w.Header().Add("HX-Redirect", url)
} else {
http.Redirect(w, r, url, http.StatusSeeOther)
}
}
func WaitMinimumTime[T interface{}](waitTime time.Duration, function func() (T, error)) (T, error) {
start := time.Now()
result, err := function()
time.Sleep(waitTime - time.Since(start))
return result, err
}
func isHtmx(r *http.Request) bool {
return r.Header.Get("HX-Request") == "true"
}