61 lines
1.0 KiB
Go
61 lines
1.0 KiB
Go
package util
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
)
|
|
|
|
func Redirect(w http.ResponseWriter, r *http.Request, path string, status int, forceRedirect bool) error {
|
|
w.Header().Set("HX-Redirect", path)
|
|
|
|
if forceRedirect {
|
|
http.Redirect(w, r, path, status)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func AddValuesToRequestContext(r *http.Request, values map[any]any) *http.Request {
|
|
req := r
|
|
for k, v := range values {
|
|
req = req.WithContext(context.WithValue(req.Context(), k, v))
|
|
}
|
|
|
|
return req
|
|
}
|
|
|
|
func GetBasePath() string {
|
|
dir, err := os.Executable()
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
basePath, err := filepath.Abs(path.Dir(dir))
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
return basePath
|
|
}
|
|
|
|
// TODO: refactor this. I went too hard
|
|
func GetFullyQualifiedPath(subject string) string {
|
|
fqp, err := filepath.Abs(GetBasePath() + prefixPath(subject))
|
|
if err != nil {
|
|
log.Fatal(err.Error())
|
|
}
|
|
|
|
return fqp
|
|
}
|
|
|
|
func prefixPath(s string) string {
|
|
if s[0:1] != "/" {
|
|
return s + "/"
|
|
}
|
|
return s
|
|
}
|