This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/utils/http.go
Tim Wundenberg 33281cb876
All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 1m20s
chore: #174 make into template
2024-12-31 12:21:47 +01:00

38 lines
870 B
Go

package utils
import (
"fmt"
"net/http"
"time"
"web-app-template/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"
}