-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
46 lines (40 loc) · 1.21 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package teseo
import (
"fmt"
"html"
"io"
"math/rand"
"net/http"
"time"
)
// GenerateUniqueKey generates a unique key using math/rand.
func GenerateUniqueKey() string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var seededRand = rand.New(rand.NewSource(time.Now().UnixNano()))
b := make([]byte, 16)
for i := range b {
b[i] = charset[seededRand.Intn(len(charset))]
}
return string(b)
}
// GetFullURL constructs the full URL from the http.Request object.
func GetFullURL(r *http.Request) string {
// Determine the scheme. If r.TLS is non-nil, the scheme is https, otherwise, it's http.
scheme := "http"
if r.TLS != nil {
scheme = "https"
}
// Construct the full URL using the scheme, host, and path.
return fmt.Sprintf("%s://%s%s", scheme, r.Host, r.URL.Path)
}
// WriteMetaTag writes a single HTML meta tag to the provided writer.
func WriteMetaTag(w io.Writer, property, content string) error {
if content == "" {
return nil
}
_, err := io.WriteString(w, fmt.Sprintf(`<meta property="%s" content="%s" />`, html.EscapeString(property), html.EscapeString(content)))
if err != nil {
return fmt.Errorf("failed to write %s meta tag: %w", property, err)
}
return nil
}