All checks were successful
Build Docker Image / Build-Docker-Image (push) Successful in 47s
38 lines
870 B
Go
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"
|
|
}
|