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 b3308d6a90
Some checks failed
Build Docker Image / Build-Docker-Image (push) Failing after 40s
feat(security): #328 delete old sessions for change and forgot password
2024-12-18 22:53:28 +01:00

38 lines
860 B
Go

package utils
import (
"me-fit/log"
"fmt"
"net/http"
"time"
)
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"
}