summaryrefslogtreecommitdiff
path: root/srv/api/utils.go
diff options
context:
space:
mode:
Diffstat (limited to 'srv/api/utils.go')
-rw-r--r--srv/api/utils.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/srv/api/utils.go b/srv/api/utils.go
index 7662e17..2cf40b6 100644
--- a/srv/api/utils.go
+++ b/srv/api/utils.go
@@ -2,7 +2,11 @@ package api
import (
"context"
+ "crypto/rand"
+ "encoding/hex"
"encoding/json"
+ "errors"
+ "fmt"
"net/http"
"strconv"
@@ -66,3 +70,22 @@ func strToInt(str string, defaultVal int) (int, error) {
}
return strconv.Atoi(str)
}
+
+func getCookie(r *http.Request, cookieName, defaultVal string) (string, error) {
+ c, err := r.Cookie(cookieName)
+ if errors.Is(err, http.ErrNoCookie) {
+ return defaultVal, nil
+ } else if err != nil {
+ return "", fmt.Errorf("reading cookie %q: %w", cookieName, err)
+ }
+
+ return c.Value, nil
+}
+
+func randStr(numBytesEntropy int) string {
+ b := make([]byte, numBytesEntropy)
+ if _, err := rand.Read(b); err != nil {
+ panic(err)
+ }
+ return hex.EncodeToString(b)
+}